diff --git a/src/prelude.rs b/src/prelude.rs index ff5f29b..a09c18e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,9 +1,9 @@ pub use crate::component::EntropyComponent; pub use crate::plugin::EntropyPlugin; pub use crate::resource::GlobalEntropy; -pub use crate::seed::GlobalRngSeed; +pub use crate::seed::{GlobalRngSeed, RngSeed}; pub use crate::traits::{ - ForkableAsRng, ForkableAsSeed, ForkableInnerRng, ForkableRng, ForkableSeed, + ForkableAsRng, ForkableAsSeed, ForkableInnerRng, ForkableRng, ForkableSeed, SeedSource }; #[cfg(feature = "wyrand")] #[cfg_attr(docsrs, doc(cfg(feature = "wyrand")))] diff --git a/src/resource.rs b/src/resource.rs index 3491d6f..0b30c93 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -5,7 +5,7 @@ use crate::{ seed::{GlobalRngSeed, RngSeed}, traits::{ EcsEntropySource, ForkableAsRng, ForkableAsSeed, ForkableInnerRng, ForkableRng, - ForkableSeed, + ForkableSeed, SeedSource, }, }; use bevy::{ @@ -91,7 +91,7 @@ where { fn from_world(world: &mut World) -> Self { if let Some(seed) = world.get_resource::>() { - Self::new(R::from_seed(seed.get_seed())) + Self::new(R::from_seed(seed.clone_seed())) } else { Self::from_entropy() } diff --git a/src/seed.rs b/src/seed.rs index a267287..dc95079 100644 --- a/src/seed.rs +++ b/src/seed.rs @@ -35,7 +35,7 @@ impl SeedSource for GlobalRngSeed where R::Seed: Sync + Send + Clone, { - /// Create a new instance of [`GlobalRngSeed`]. + /// Create a new instance of [`GlobalRngSeed`] from a given `seed` value. #[inline] #[must_use] fn from_seed(seed: R::Seed) -> Self { @@ -44,6 +44,16 @@ where rng: PhantomData, } } + + #[inline] + fn get_seed(&self) -> &R::Seed { + &self.seed + } + + #[inline] + fn clone_seed(&self) -> R::Seed { + self.seed.clone() + } } impl GlobalRngSeed @@ -63,12 +73,6 @@ impl GlobalRngSeed where R::Seed: Sync + Send + Clone, { - /// Returns a cloned instance of the seed value. - #[inline] - pub fn get_seed(&self) -> R::Seed { - self.seed.clone() - } - /// Set the global seed to a new value pub fn set_seed(&mut self, seed: R::Seed) { self.seed = seed; @@ -110,12 +114,25 @@ impl SeedSource for RngSeed where R::Seed: Sync + Send + Clone, { + /// Create a new instance of [`RngSeed`] from a given `seed` value. + #[inline] + #[must_use] fn from_seed(seed: R::Seed) -> Self { Self { seed, rng: PhantomData, } } + + #[inline] + fn get_seed(&self) -> &R::Seed { + &self.seed + } + + #[inline] + fn clone_seed(&self) -> R::Seed { + self.seed.clone() + } } impl Component for RngSeed @@ -183,6 +200,6 @@ mod tests { let recreated = GlobalRngSeed::::from_reflect(value.as_reflect()).unwrap(); - assert_eq!(val.get_seed(), recreated.get_seed()); + assert_eq!(val.clone_seed(), recreated.clone_seed()); } } diff --git a/src/traits.rs b/src/traits.rs index 31ef573..204df83 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -183,6 +183,12 @@ where /// Initialize a [`SeedSource`] from a given `seed` value. fn from_seed(seed: R::Seed) -> Self; + /// Returns a reference of the seed value. + fn get_seed(&self) -> &R::Seed; + + /// Returns a cloned instance of the seed value. + fn clone_seed(&self) -> R::Seed; + /// Initialize a [`SeedSource`] from a `seed` value obtained from a /// OS-level or user-space RNG source. fn from_entropy() -> Self diff --git a/tests/integration/determinism.rs b/tests/integration/determinism.rs index e757610..a1a5e7b 100644 --- a/tests/integration/determinism.rs +++ b/tests/integration/determinism.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use bevy_prng::{ChaCha12Rng, ChaCha8Rng, WyRand}; use bevy_rand::prelude::{ - EntropyComponent, EntropyPlugin, ForkableAsRng, ForkableRng, GlobalEntropy, GlobalRngSeed, + EntropyComponent, EntropyPlugin, ForkableAsRng, ForkableRng, GlobalEntropy, GlobalRngSeed, SeedSource }; use rand::prelude::Rng; @@ -88,7 +88,7 @@ fn setup_sources(mut commands: Commands, mut rng: ResMut>) { - assert_eq!(seed.get_seed(), [2; 32]); + assert_eq!(seed.get_seed(), &[2; 32]); } /// Entities having their own sources side-steps issues with parallel execution and scheduling diff --git a/tests/integration/reseeding.rs b/tests/integration/reseeding.rs index f926f86..c7374b3 100644 --- a/tests/integration/reseeding.rs +++ b/tests/integration/reseeding.rs @@ -8,7 +8,7 @@ use bevy_rand::{ prelude::EntropyComponent, resource::GlobalEntropy, seed::GlobalRngSeed, - traits::{ForkableAsSeed, ForkableSeed}, + traits::{ForkableAsSeed, ForkableSeed, SeedSource}, }; use rand_core::{RngCore, SeedableRng}; @@ -26,7 +26,7 @@ fn test_global_reseeding() { R::Seed: Sync + Send + Clone, { if seed.is_changed() && !seed.is_added() { - rng.reseed(seed.get_seed()); + rng.reseed(seed.clone_seed()); } }