Skip to content

rand_xoshiro: Add state() for serde-free state export#110

Merged
vks merged 4 commits into
masterfrom
export-xoshiro-state
May 16, 2026
Merged

rand_xoshiro: Add state() for serde-free state export#110
vks merged 4 commits into
masterfrom
export-xoshiro-state

Conversation

@vks

@vks vks commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: This PR is mostly Claude generated.

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.

@vks vks force-pushed the export-xoshiro-state branch from 956d473 to 76737e8 Compare April 26, 2026 19:50
@vks vks changed the title rand_xoshiro: Add state() for serde-free state export (#109) rand_xoshiro: Add state() for serde-free state export Apr 26, 2026

@dhardy dhardy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Comment thread rand_xoshiro/src/splitmix64.rs Outdated
Comment thread rand_xoshiro/src/common.rs Outdated
Comment thread rand_xoshiro/src/common.rs Outdated
Comment thread rand_xoshiro/src/common.rs Outdated
Comment thread rand_xoshiro/src/common.rs Outdated
@vks vks requested a review from dhardy May 14, 2026 15:37
@dhardy

dhardy commented May 16, 2026

Copy link
Copy Markdown
Member

Needs a rebase

vks added 3 commits May 16, 2026 14:41
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
@vks vks force-pushed the export-xoshiro-state branch from 0e094a9 to d21eab0 Compare May 16, 2026 12:43
@vks vks merged commit be9aa09 into master May 16, 2026
14 checks passed
@vks vks deleted the export-xoshiro-state branch May 16, 2026 13:43
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.

Add state export for rand_xoshiro

2 participants