Skip to content

Use serde instead of specialization#579

Merged
sophiajt merged 7 commits intonushell:masterfrom
est31:serde_instead_of_specialization
Sep 3, 2019
Merged

Use serde instead of specialization#579
sophiajt merged 7 commits intonushell:masterfrom
est31:serde_instead_of_specialization

Conversation

@est31
Copy link
Contributor

@est31 est31 commented Sep 3, 2019

This is a continuation of #531 and #569 and concludes the series of PRs to get rid of specialization.

As #569 took care of Option<>, Vec<> and bare bools, we only had to cover the following types:

Tagged<i64>
Tagged<PathBuf>
Tagged<String>
Tagged<u64>
Tagged<Value>
value::Block

(list obtainable by the command rg "\(Deserialize\)][^}]*\}" -U src/commands/* | rg ": .*: " | sed 's/^.*: //' | sort | uniq | sed 's/\(Option\|Vec\)<\(.*\)>/\2/;s/(\(.*\), \(.*\))/\1\n\2/;s/,$//;/bool/d' | sort | uniq)

I chose an approach to first serialize the argument to json, to then deserialize it using serde. This way, the layout of the Tagged struct doesn't have to be hardcoded. The difficulty in this approach was recognizing the type that one has to serialize to: sometimes, the value has to be unwrapped to its contents, sometimes Value is expected or even an untagged value::Block. These were special cased with type name based checks.

With this PR, nushell is specialization free. 🎉🎉🎉
cc #362

The trait itself was kept as it's still useful. Also it's still used by the trim command.

@est31 est31 mentioned this pull request Sep 3, 2019
12 tasks
@sophiajt sophiajt merged commit 1464fea into nushell:master Sep 3, 2019
@sophiajt
Copy link
Contributor

sophiajt commented Sep 3, 2019

🎉🎉

est31 added a commit to est31/nushell that referenced this pull request Sep 9, 2019
Fixes nushell#627

Fixes a regression caused by nushell#579, specifically commit cc8872b .

The code was intended to perform a comparison between the wanted
output type and "Tagged<Value>" in order to be able to provide a
special-cased path for Tagged<Value>. When I wrote the code, I
used "name" as a variable name and only later realized that it
shadowed the "name" param to the function, so I renamed it to
type_name, but forgot to change the comparison.
This broke the special-casing, as the name param only contains
the name of the struct without generics (like "Tagged"), while
`std::any::type_name` (in the current implementation) contains
the full paths of the struct including all generic params
(like "nu::object::meta::Tagged<nu::object::base::Value>").
Hofer-Julian pushed a commit to Hofer-Julian/nushell that referenced this pull request Jan 27, 2023
* Add wrap-around merge

* Update working_with_tables.md

fixed code formatting

* better spacing around pipes

* Moved merging to cookbook

* grammar

* fixed command hyperlinks

* add page to vuepress config

Co-authored-by: Yethal <nosuchemail@email.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants