Skip to content

Commit

Permalink
feat: Fix prelude, improve SeedSource methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Jul 23, 2024
1 parent e5b35ca commit f198fe2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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")))]
Expand Down
4 changes: 2 additions & 2 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
seed::{GlobalRngSeed, RngSeed},
traits::{
EcsEntropySource, ForkableAsRng, ForkableAsSeed, ForkableInnerRng, ForkableRng,
ForkableSeed,
ForkableSeed, SeedSource,
},
};
use bevy::{
Expand Down Expand Up @@ -91,7 +91,7 @@ where
{
fn from_world(world: &mut World) -> Self {
if let Some(seed) = world.get_resource::<GlobalRngSeed<R>>() {
Self::new(R::from_seed(seed.get_seed()))
Self::new(R::from_seed(seed.clone_seed()))
} else {
Self::from_entropy()
}
Expand Down
33 changes: 25 additions & 8 deletions src/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<R: SeedableEntropySource> SeedSource<R> for GlobalRngSeed<R>
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 {
Expand All @@ -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<R: SeedableEntropySource> GlobalRngSeed<R>
Expand All @@ -63,12 +73,6 @@ impl<R: SeedableEntropySource> GlobalRngSeed<R>
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;
Expand Down Expand Up @@ -110,12 +114,25 @@ impl<R: SeedableEntropySource> SeedSource<R> for RngSeed<R>
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<R: SeedableEntropySource> Component for RngSeed<R>
Expand Down Expand Up @@ -183,6 +200,6 @@ mod tests {

let recreated = GlobalRngSeed::<WyRand>::from_reflect(value.as_reflect()).unwrap();

assert_eq!(val.get_seed(), recreated.get_seed());
assert_eq!(val.clone_seed(), recreated.clone_seed());
}
}
6 changes: 6 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/determinism.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -88,7 +88,7 @@ fn setup_sources(mut commands: Commands, mut rng: ResMut<GlobalEntropy<ChaCha8Rn
}

fn read_global_seed(seed: Res<GlobalRngSeed<ChaCha8Rng>>) {
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
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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());
}
}

Expand Down

0 comments on commit f198fe2

Please sign in to comment.