diff options
| author | Mica White <botahamec@outlook.com> | 2026-10-26 10:52:04 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-10-26 10:58:46 -0400 |
| commit | ec40f6e3acc7874b82709ab81b254c741529fdff (patch) | |
| tree | e3dfdc315bd0c6f1d2efcf442c2cfb3317d6810f /platforms/tortuise-script-compiled/src/random.rs | |
| parent | e3c5839159903658a667f789963f20a31567b25c (diff) | |
Diffstat (limited to 'platforms/tortuise-script-compiled/src/random.rs')
| -rw-r--r-- | platforms/tortuise-script-compiled/src/random.rs | 70 |
1 files changed, 70 insertions, 0 deletions
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::<StdRng>().next_u64()) + } +} + +impl Default for TortuiseRng { + fn default() -> Self { + Self::new() + } +} + +impl SeedableRng for TortuiseRng { + type Seed = <SmallRng as SeedableRng>::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<R: Rng + ?Sized>(rng: &mut R) -> Self { + Self(SmallRng::from_rng(rng)) + } + + fn try_from_rng<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> { + 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<Self, <Self as TryRng>::Error> + where + Self: TryRng, + { + Ok(Self(self.0.try_fork()?)) + } +} + +impl TryRng for TortuiseRng { + type Error = <SmallRng as TryRng>::Error; + + fn try_next_u32(&mut self) -> Result<u32, Self::Error> { + self.0.try_next_u32() + } + + fn try_next_u64(&mut self) -> Result<u64, Self::Error> { + self.0.try_next_u64() + } + + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { + self.0.try_fill_bytes(dst) + } +} |
