clr_random 0.1.0

.NET CLR Random implementation in Rust
Documentation
//! Seed generation for the `CLRRandom` struct.
//!
//! This module provides the [`Seed`] struct used to initialize the random number generator.

/// A seed used to initialize [`CLRRandom`].
///
/// It can be created from an [`i32`] value directly or via the [`From`] trait.
#[derive(Default, Clone)]
pub struct Seed {
    pub(crate) seed: i32,
    bytes: [u8; 4],
}

impl Seed {
    /// Creates a new [`Seed`] from an [`i32`] value.
    ///
    /// # Example
    /// ```
    /// use clr_random::Seed;
    /// let seed = Seed::new(42);
    /// ```
    pub fn new(value: i32) -> Self {
        Self {
            seed: value,
            bytes: value.to_le_bytes(),
        }
    }
}

impl From<i32> for Seed {
    /// Allows creating a [`Seed`] from an [`i32`] using `.into()`.
    ///
    /// # Example
    /// ```
    /// use clr_random::Seed;
    /// let seed: Seed = 42.into();
    /// ```
    fn from(value: i32) -> Self {
        Self::new(value)
    }
}

impl AsRef<[u8]> for Seed {
    /// Provides a byte slice representation of the seed.
    ///
    /// The byte order is little-endian.
    fn as_ref(&self) -> &[u8] {
        &self.bytes
    }
}

impl AsMut<[u8]> for Seed {
    /// Provides a mutable byte slice representation of the seed.
    ///
    /// The byte order is little-endian.
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.bytes

    }
}