serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.06k stars 767 forks source link

Implement Ser+De for Saturating<T> #2709

Closed jbethune closed 5 months ago

jbethune commented 7 months ago

This implementation is heavily inspired by the existing trait implentation for std::num::Wrapping<T>.

fix #2708

jbethune commented 7 months ago

std::num::Saturating<T> was added in Rust version 1.74 which is above the minimum supported version of Serde. How should we handle this?

jbethune commented 6 months ago

I've added the rustversion crate to version-gate the implementation so that it also compiles for older Rust versions.

oli-obk commented 6 months ago

We do version detection in build.rs: https://github.com/serde-rs/serde/blob/74d06708ddff495161187ea490c4616291216346/serde/build.rs#L64 Please use that scheme to register a new cfg and put your new additions behind such a cfg

oli-obk commented 6 months ago

(also once done, please rebase and squash your commits)

jbethune commented 6 months ago

(also once done, please rebase and squash your commits)

Done. Thanks for the clarifications!

jbethune commented 6 months ago

your use case where these impls are going to be needed.

I'm making a game for a virtual 16 bit architecture with number semantics that saturate at 2^16-1 because that's the maximum level you can achieve for different stats.

jbethune commented 6 months ago

@oli-obk I have updated the deserialization code so that it maps exceedingly large or small values to the MAX and MIN values of the target type.

I will squash and rebase (if needed) after I have an updated approval from you.

Thanks!

jbethune commented 6 months ago

@oli-obk I have incorporated your requests and suggestions and squashed everything into one commit

oli-obk commented 5 months ago

Thanks!

dtolnay commented 5 months ago

Thanks — published in serde 1.0.198.