rand_xoshiro: Add state() for serde-free state export#110
Merged
Conversation
956d473 to
76737e8
Compare
state() for serde-free state export (#109)state() for serde-free state export
dhardy
reviewed
Apr 27, 2026
dhardy
left a comment
Member
There was a problem hiding this comment.
A few comments on style, otherwise this looks fine.
You might as well bump the version number to prepare the a patch release (unless you'd prefer to make a new PR for that).
Member
|
Needs a rebase |
Add a `state()` method to all 15 RNG types in `rand_xoshiro`, returning the internal state as a value matching the type's `SeedableRng::Seed`. This lets callers persist and reload generator state without enabling the `serde` feature: `Self::from_seed(rng.state())` reconstructs an identical generator. Return type per RNG matches its `Seed`: | RNG | `state()` | | --------------------------------------------------------- | ----------- | | `SplitMix64`, `Xoroshiro64Star`, `Xoroshiro64StarStar` | `[u8; 8]` | | `Xoroshiro128*`, `Xoshiro128*` | `[u8; 16]` | | `Xoshiro256*` | `[u8; 32]` | | `Xoshiro512*` | `Seed512` | The all-zero state is unreachable from any non-zero seed for these algorithms, so the round-trip is exact for any generator obtained via the usual constructors. (`from_seed` remaps an all-zero input to `seed_from_u64(0)`; this is documented on each `state()`.) Implementation lives in four small macros in `common.rs` (`impl_state_scalar!`, `impl_state_pair!`, `impl_state_array!`, `impl_state_seed512!`); each RNG file gets one macro invocation and one `state_roundtrip` test. Fixes #109, #64.
- Simplify `state_roundtrip` tests to a single `assert_eq!(rng, clone)` - Inline `impl_state_scalar!` into `SplitMix64` - Drop redundant `$bytes` argument from `impl_state_pair!` and `impl_state_array_of_four!` and add a `const` size assertion against the type's own layout. - Rename `impl_state_array!` to `impl_state_array_of_four!` and unroll loop
dhardy
approved these changes
May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Disclaimer: This PR is mostly Claude generated.
Add a
state()method to all 15 RNG types inrand_xoshiro, returning the internal state as a value matching the type'sSeedableRng::Seed. This lets callers persist and reload generator state without enabling theserdefeature:Self::from_seed(rng.state())reconstructs an identical generator.Return type per RNG matches its
Seed:state()SplitMix64,Xoroshiro64Star,Xoroshiro64StarStar[u8; 8]Xoroshiro128*,Xoshiro128*[u8; 16]Xoshiro256*[u8; 32]Xoshiro512*Seed512The all-zero state is unreachable from any non-zero seed for these algorithms, so the round-trip is exact for any generator obtained via the usual constructors. (
from_seedremaps an all-zero input toseed_from_u64(0); this is documented on eachstate().)Implementation lives in four small macros in
common.rs(impl_state_scalar!,impl_state_pair!,impl_state_array!,impl_state_seed512!); each RNG file gets one macro invocation and onestate_roundtriptest.Fixes #109, #64.