From ec40f6e3acc7874b82709ab81b254c741529fdff Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 26 Oct 2026 10:52:04 -0400 Subject: Finish engine --- platforms/tortuise-script-compiled/src/random.rs | 70 ++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 platforms/tortuise-script-compiled/src/random.rs (limited to 'platforms/tortuise-script-compiled/src/random.rs') diff --git a/platforms/tortuise-script-compiled/src/random.rs b/platforms/tortuise-script-compiled/src/random.rs new file mode 100644 index 0000000..148f152 --- /dev/null +++ b/platforms/tortuise-script-compiled/src/random.rs @@ -0,0 +1,70 @@ +use rand::{ + Rng, SeedableRng, TryRng, + rngs::{SmallRng, StdRng}, +}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TortuiseRng(SmallRng); + +impl TortuiseRng { + pub fn new() -> Self { + puffin::profile_function!(); + Self::seed_from_u64(rand::make_rng::().next_u64()) + } +} + +impl Default for TortuiseRng { + fn default() -> Self { + Self::new() + } +} + +impl SeedableRng for TortuiseRng { + type Seed = ::Seed; + + fn from_seed(seed: Self::Seed) -> Self { + Self(SmallRng::from_seed(seed)) + } + + fn seed_from_u64(state: u64) -> Self { + Self(SmallRng::seed_from_u64(state)) + } + + fn from_rng(rng: &mut R) -> Self { + Self(SmallRng::from_rng(rng)) + } + + fn try_from_rng(rng: &mut R) -> Result { + Ok(Self(SmallRng::try_from_rng(rng)?)) + } + + fn fork(&mut self) -> Self + where + Self: Rng, + { + Self(self.0.fork()) + } + + fn try_fork(&mut self) -> Result::Error> + where + Self: TryRng, + { + Ok(Self(self.0.try_fork()?)) + } +} + +impl TryRng for TortuiseRng { + type Error = ::Error; + + fn try_next_u32(&mut self) -> Result { + self.0.try_next_u32() + } + + fn try_next_u64(&mut self) -> Result { + self.0.try_next_u64() + } + + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { + self.0.try_fill_bytes(dst) + } +} -- cgit v1.3.1