Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl from collections for RpcValue #6

Merged

Conversation

j4r0u53k
Copy link
Contributor

@j4r0u53k j4r0u53k commented Jul 5, 2024

No description provided.

j4r0u53k added 4 commits July 5, 2024 22:14
Add blanket implementations of traits:

 - From<Vec<T>>
 - From<BTreeMap<String, T>>
 - From<BTreeMap<i32, T>>

for RpcValue where T: Into<RpcValue>

This allows to perform uniform conversion to RpcValue for
structs whose fields are either of a type convertible to
RpcValue or a collection of such type.

There is no specialization for T equals to `RpcValue` due
to limitations of Rust, so the conversion is unnecessarily
costly as it iterates through the collection instead of just
moving the inner data.
The compiler in release mode is able to generate optimized code
of `From<Vec<RpcValue>>`, but unfortunately not for `BTreeMap`.
Replace new() and sequence of insert() with more idiomatic
initialization from slice of tuples collected into a map.
If enabled, provides special `impl From<Collection<RpcValue>> for
RpcValue` for List, Map and IMap, so it just moves the inner data
without iterating the collection.

Requires nightly to enable `min_specialization` compiler feature.
@j4r0u53k j4r0u53k changed the title Impl from collections for rpcvalue Impl from collections for RpcValue Jul 5, 2024
@j4r0u53k j4r0u53k force-pushed the impl-from-collections-for-rpcvalue branch from 6f76f31 to e58df3b Compare July 5, 2024 23:23
@@ -15,5 +15,12 @@ chrono = "0.4.38"
hex = "0.4.3"
clap = { version = "4.5.7", features = ["derive"] }

[features]

# Enables feature `min_specialization` of nightly compiler and provides
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I which production project can we use unstable rust?

Copy link
Contributor Author

@j4r0u53k j4r0u53k Jul 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's entirely up to consumers of the lib whether they want to enable this feature or not. By default it's disabled.

The purpose of this feature is only to provide optimalization for converting a collection of RpcValues into RpcValue, where a user code decides to handle all the conversions in the uniform way - call .into() on any convertible data without special treatment of Collection<RpcValue> case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have only one consumer currently - us. So this code is effectively dead. We can keep it until min_specialization will be stabilized, hopefully there will be some external consumers this time. My concern was just to know, how can be this feature utilized now.

Copy link
Contributor Author

@j4r0u53k j4r0u53k Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether or not there are currently any external consumers shouldn't be the concern here. Anybody can start a project at any time utilizing the feature if they consider it meaningful.

The min_specialization has been around for a while and it's highly demanded. I don't think it will get removed. However, it might not get stabilized for another couple of years, which would mean we should either give up on this feature completely, or at least leave it there as an option, disabled by default, which is what I propose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we can keep it and revisit it when specialization will be stabilized even if it is far from finished rust-lang/rust#31844.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still wonder, why specialization generates necessity of macro used on lines rpcvalue.rs:208 - rpcvalue.rs:222, which wasn't needed before. My naive understanding is, that just special cases should be introduced, but the generic impl should remain the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cfg macros inside the impls are necessary because of the default keyword/marker in the definition of the generic implementations of from methods, which can appear only if the compiler feature is enabled.

@fvacek fvacek merged commit c5540a1 into silicon-heaven:master Jul 8, 2024
2 checks passed
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