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

refactor: use deserialize_str instead #527

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

kariy
Copy link
Contributor

@kariy kariy commented Dec 13, 2023

Resolves #526

@xJonathanLEI
Copy link
Owner

Thanks! Just curious: for bincode though, wouldn't you want to use bytes instead of str for the repr? AFAIK serde doesn't play well with multiple formats.

On the serialization side, the only option is to use newtypes (e.g. struct SerializeAsBytes(FieldElement)).

On the deserialization side, either use the newtype option, or just go with deserialize_any and implement many visit_* (but then you run into the issue this PR tries to solve).

@xJonathanLEI
Copy link
Owner

Or, of couse, you can always mark all your FieldElement occurrences with #[serde(with = "...")], or even go as far as manual impls. I guess what the "official" Serialize/Deserialize impls here only serve as the default. Maybe starknet-ff itself can offer alternative modules to be used with #[serde(with = "...")], if that's helpful.

@xJonathanLEI xJonathanLEI merged commit f35f9de into xJonathanLEI:master Dec 14, 2023
23 of 24 checks passed
@kariy
Copy link
Contributor Author

kariy commented Dec 14, 2023

Thanks! Just curious: for bincode though, wouldn't you want to use bytes instead of str for the repr?

Ideally yes

Or, of couse, you can always mark all your FieldElement occurrences with #[serde(with = "...")], or even go as far as manual impls.

I could do this but i dont quite like having to tag #[serde(..)] on every types that use it or even a newtype struct.

I guess what the "official" Serialize/Deserialize impls here only serve as the default. Maybe starknet-ff itself can offer alternative modules to be used with #[serde(with = "...")], if that's helpful.

i just found out about Deserializer::is_human_readable() . Maybe we can conditionally call the preferred deserialize entry points like this ?

 if deserializer.is_human_readable() {
            deserializer.deserialize_any(FieldElementVisitor)
        } else {
            deserializer.deserialize_bytes(FieldElementVisitor)
        }
}        

deserializer like serde_json would return true while something like bincode would return false, so it would still maintain the same behaviour as before but should now work with non self-describing formats.

this is also what alloy is using for their primitive types

@xJonathanLEI
Copy link
Owner

xJonathanLEI commented Dec 14, 2023

Thanks! I didn't know that.

This is sorta a breaking change though. Non-human-readable self-describing formats used to be serialized as strings but now they use bytes. Files previously saved with such a format will become unreadable.

I guess we can do this with a breaking semver bump.

@kariy
Copy link
Contributor Author

kariy commented Dec 15, 2023

I'm happy to contribute on that if you're okay with the approach.

@xJonathanLEI
Copy link
Owner

Yeah that's certainly a good approach. We just need to bump a breaking version with this change.

@xJonathanLEI
Copy link
Owner

@kariy This from rust_decimal is an interesting approach too. One issue with this approach though: someone enabling this feature in the dependency tree would affect everyone else.

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.

FieldElement doesn't seem to support deserializing from non self-describing format
2 participants