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

feat: opt-in serialization of sequences as sets #66

Merged
merged 3 commits into from
Apr 2, 2023

Conversation

neoeinstein
Copy link
Contributor

I ran into a use case where I'd like to be able to serialize a set of values ergonomically. This relies on the with functionality from serde to make it completely opt in, enabling this use case without affecting any existing serialization or deserialization.

@neoeinstein
Copy link
Contributor Author

neoeinstein commented Mar 22, 2023

Before this merges, I do have a method that may allow this to compile down to zero-cost after monomorphization. I'm also considering the value splitting this into 3 modules instead having just one for all three set types and inferring from the first item. Any thoughts?

@bryanburgers
Copy link
Contributor

This is awesome. Thank you!

src/set/bytes.rs Outdated Show resolved Hide resolved
src/set/mod.rs Outdated Show resolved Hide resolved
src/set/bytes.rs Outdated Show resolved Hide resolved
src/set/bytes.rs Outdated
//!
//! #[derive(Serialize, Deserialize)]
//! struct MyStruct {
//! #[serde(with = "serde_dynamo::set::bytes")]
Copy link
Contributor

@bryanburgers bryanburgers Mar 23, 2023

Choose a reason for hiding this comment

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

I feel like

#[serde(with = "serde_dynamo::byte_set")]
#[serde(with = "serde_dynamo::number_set")]
#[serde(with = "serde_dynamo::string_set")]

feels a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can made that change; this was mainly just for organization and avoiding a bunch of modules at the crate base. I've been using bytes set (with the plural) since it's a set of byte arrays. If that's okay, I can rename and move these modules into the crate root.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been going off of the naming described here where they're called "number set", "string set" and "binary set".

@bryanburgers
Copy link
Contributor

bryanburgers commented Mar 27, 2023

For what it's worth, I did once have an implementation of this, but the key bit that was clunky was telling the Serializer that the next stuff is a set.

I looked deeply at std::any::Any and asked the rust core team about it because it doesn't quite work.

I also had something with thread locals that was kinda gross.

Your newtype struct thing makes a ton of sense and I like it.

In the mean time, I noticed that std::any::Provider now exists, which solves my original problem and doesn't require magic strings, so I wrote up a PR to serde to use it: serde-rs/serde#2420

That doesn't preclude this from merging for two reasons:

  • std::any::Provider is still nightly only
  • I have no idea if the serde team is interested in that direction.

@neoeinstein
Copy link
Contributor Author

Pull request has been updated to address the above comments on module naming.

@bryanburgers
Copy link
Contributor

@neoeinstein I have no concerns right now. I just need to find the time to dig in and hopefully merge. Thanks for your contribution!

bryanburgers added a commit that referenced this pull request Apr 2, 2023
feat: opt-in serialization of sequences as sets
@bryanburgers bryanburgers merged commit d27e12d into zenlist:main Apr 2, 2023
@bryanburgers
Copy link
Contributor

bryanburgers commented Apr 2, 2023

Released in 4.2.0, which is now up on crates.io. Thank you!

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