From b627481d77c9da9a76bfd0c5509fc8ea43ab799c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Wed, 18 Oct 2023 07:20:05 +0200 Subject: [PATCH] docs: Update example and add code snippets for trait --- examples/turn_based_game.rs | 4 +-- src/traits.rs | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/examples/turn_based_game.rs b/examples/turn_based_game.rs index f07ebfe..d17acc0 100644 --- a/examples/turn_based_game.rs +++ b/examples/turn_based_game.rs @@ -74,7 +74,7 @@ fn setup_player(mut commands: Commands, mut rng: ResMut>) { + /// commands + /// .spawn(( + /// Source, + /// global.fork_rng(), + /// )); + /// } + /// ``` fn fork_rng(&mut self) -> Self::Output { Self::Output::from_rng(self).unwrap() } @@ -26,6 +42,22 @@ pub trait ForkableAsRng: EcsEntropySource { /// Fork the original instance to yield a new instance with a generated seed. /// This method allows one to specify the RNG algorithm to be used for the forked instance. + /// ``` + /// use bevy::prelude::*; + /// use bevy_rand::prelude::*; + /// use bevy_prng::{ChaCha8Rng, ChaCha12Rng}; + /// + /// #[derive(Component)] + /// struct Source; + /// + /// fn setup_source(mut commands: Commands, mut global: ResMut>) { + /// commands + /// .spawn(( + /// Source, + /// global.fork_as::(), + /// )); + /// } + /// ``` fn fork_as(&mut self) -> Self::Output { Self::Output::<_>::from_rng(self).unwrap() } @@ -40,6 +72,25 @@ pub trait ForkableInnerRng: EcsEntropySource { /// Fork the original instance to yield a new instance with a generated seed. /// This method yields the inner PRNG instance directly as a forked instance. + /// ``` + /// use bevy::prelude::*; + /// use bevy_rand::prelude::*; + /// use bevy_prng::ChaCha8Rng; + /// use rand_core::RngCore; + /// + /// #[derive(Component)] + /// struct Source; + /// + /// fn do_random_action(source: &mut ChaCha8Rng) { + /// println!("Random value: {}", source.next_u32()); + /// } + /// + /// fn access_source(mut global: ResMut>) { + /// let mut source = global.fork_inner(); + /// + /// do_random_action(&mut source); + /// } + /// ``` fn fork_inner(&mut self) -> Self::Output { Self::Output::from_rng(self).unwrap() }