Comment by ~tomassedovic on ~icefox/oorandom
Doing it in oorandom is simpler than that even. You would put this in Cargo.toml:
[dependencies] serde = { version = "1.0", optional = true, features = ["derive"] } [features] serialization = ["serde"]And then in lib.rs:
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] struct Rand32 { ... }And then anyone using the lib can would add this to their
Cargo.toml
oorandom = { version = "11.2", features = ["serialization"] }And this in their code:
#[derive(Serialize, Deserialize)] struct MyStruct { rng: oorandom::Rand32 }The code from #1 is only necessary because
Rand32
does not implement the Serde traits (so you have to tell Serde in the usage code how to do the serialisation/deserialisation explicitly).
Comment by ~tomassedovic on ~icefox/oorandom
Okay, here's a little report: I've replaced Rand with oorandom without much trouble \o/.
Had to write a few extra things I was using in Rand (generating the initial seed,
choose
,choose_weighted
) but that was all pretty small stuff.I've tried to benchmark it but my mapgen is so fast I had to generate over 8000 worlds in a row before I could see some numerical differences. oorandom is about a third faster than Rand (I was using a csprng there so yea of course) in my use case, but the actual impact on the speed of the game is pretty much nil because it's all so fast anyway (yay rust & simple level gen).
The actual Serde implementation took me some time to figure out, but it's not too bad:
#[derive(Serialize, Deserialize)] pub struct Game { // a bunch of other fields #[serde(with = "Rand32Serde")] rng: Rand32, } fn rand32_get_state(r: &Rand32) -> u64 { r.state().0 } fn rand32_get_inc(r: &Rand32) -> u64 { r.state().1 } #[derive(Serialize, Deserialize)] #[serde(remote = "Rand32")] struct Rand32Serde { #[serde(getter = "rand32_get_state")] state: u64, #[serde(getter = "rand32_get_inc")] inc: u64, } impl From<Rand32Serde> for Rand32 { fn from(r: Rand32Serde) -> Rand32 { Rand32::from_state((r.state, r.inc)) } }This could be made a little easier by providing two separate getters for the
state
andinc
fields -- that would let me get rid of therand32_get_*
functions. Or of course implementing the Serde traits in oorandom.But yeah this is pretty okay anyway and honestly, I'm glad I can just put this in my
Cargo.toml
and forget about it breaking every release like Rand does.
Comment by ~tomassedovic on ~icefox/oorandom
Looks perfect, thanks!
It'll be some time before I can test this in my game. But I'm so down with a no questions asked simple seedable prng. Thanks for building this!