use crate::{Infallible, InfallibleResult, NotEnoughElements, RandQualities, RandTry};
#[doc = crate::_tags!(rand fake)]
#[doc = crate::_doc_meta!{
location("num/prob/rand"),
#[cfg(target_pointer_width = "32")]
test_size_of(__: RandFake<4> = 36),
#[cfg(target_pointer_width = "64")]
test_size_of(__: RandFake<4> = 40),
}]
#[derive(Clone, Copy, Debug)]
pub struct RandFake<const N: usize, const PANIC: bool = false> {
values: [u64; N],
index: usize,
}
impl<const N: usize, const PANIC: bool> RandFake<N, PANIC> {
pub const fn new(values: [u64; N]) -> Self {
Self { values, index: 0 }
}
pub const fn index(&self) -> usize {
self.index
}
pub const fn remaining(&self) -> usize {
N - self.index
}
pub const fn is_empty(&self) -> bool {
self.index == N
}
pub const fn reset(&mut self) {
self.index = 0;
}
}
impl<const N: usize> RandFake<N, false> {
pub const fn panicking(self) -> RandFake<N, true> {
RandFake { values: self.values, index: self.index }
}
}
impl<const N: usize> RandFake<N, true> {
pub const fn fallible(self) -> RandFake<N, false> {
RandFake { values: self.values, index: self.index }
}
}
impl<const N: usize> RandTry for RandFake<N, false> {
type Error = NotEnoughElements;
const RAND_OUTPUT_BITS: u32 = 64;
const RAND_STATE_BITS: u32 = usize::BITS;
const RAND_QUALITIES: RandQualities = RandQualities::WEAK_PRNG;
fn rand_try_next_u64(&mut self) -> Result<u64, Self::Error> {
if self.index < N {
let value = self.values[self.index];
self.index += 1;
Ok(value)
} else {
Err(NotEnoughElements(Some(1)))
}
}
}
impl<const N: usize> RandTry for RandFake<N, true> {
type Error = Infallible;
const RAND_OUTPUT_BITS: u32 = 64;
const RAND_STATE_BITS: u32 = usize::BITS;
const RAND_QUALITIES: RandQualities = RandQualities::WEAK_PRNG;
#[track_caller]
fn rand_try_next_u64(&mut self) -> InfallibleResult<u64> {
if self.index < N {
let value = self.values[self.index];
self.index += 1;
Ok(value)
} else {
panic!("RandFake exhausted")
}
}
}