Skip to content

Commit 3d4e686

Browse files
committed
Remove unsafe from from_seed
1 parent b21d1a9 commit 3d4e686

File tree

1 file changed

+3
-26
lines changed

1 file changed

+3
-26
lines changed

src/lib.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
#[cfg(feature="std")] extern crate std as core;
251251
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
252252

253-
use core::{marker, mem, slice};
253+
use core::{marker, mem};
254254
#[cfg(feature="std")] use std::cell::RefCell;
255255
#[cfg(feature="std")] use std::rc::Rc;
256256
#[cfg(all(feature="alloc", not(feature="std")))] use alloc::boxed::Box;
@@ -732,24 +732,6 @@ impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> {
732732
}
733733
}
734734

735-
mod private {
736-
pub trait Sealed {}
737-
impl Sealed for [u8; 4] {}
738-
impl Sealed for [u8; 8] {}
739-
impl Sealed for [u8; 12] {}
740-
impl Sealed for [u8; 16] {}
741-
impl Sealed for [u8; 24] {}
742-
impl Sealed for [u8; 32] {}
743-
}
744-
745-
/// The seed type is restricted to these types. This trait is sealed to prevent
746-
/// user-extension.
747-
///
748-
/// Use of byte-arrays avoids endianness issues. We may extend this to allow
749-
/// byte arrays of other lengths in the future.
750-
pub trait SeedRestriction: private::Sealed + Default + AsMut<[u8]> {}
751-
impl<S> SeedRestriction for S where S: private::Sealed + Default + AsMut<[u8]> {}
752-
753735
/// A random number generator that can be explicitly seeded.
754736
///
755737
/// Each pseudo-random number generator (PRNG) should implement this.
@@ -762,7 +744,7 @@ pub trait SeedableRng: Sized {
762744
/// RNG's with partially overlapping periods.
763745
///
764746
/// For cryptographic RNG's a seed of 256 bits is recommended, `[u8; 32]`.
765-
type Seed: SeedRestriction;
747+
type Seed: Sized + Default + AsMut<[u8]>;
766748

767749
/// Create a new PRNG using the given seed.
768750
///
@@ -806,12 +788,7 @@ pub trait SeedableRng: Sized {
806788
/// There are no reproducibility requirements like endianness conversion.
807789
fn from_rng<R: Rng>(mut rng: R) -> Result<Self, Error> {
808790
let mut seed = Self::Seed::default();
809-
let size = mem::size_of::<Self::Seed>() as usize;
810-
unsafe {
811-
let ptr = seed.as_mut().as_mut_ptr() as *mut u8;
812-
let slice = slice::from_raw_parts_mut(ptr, size);
813-
rng.try_fill_bytes(slice)?;
814-
}
791+
rng.try_fill_bytes(seed.as_mut())?;
815792
Ok(Self::from_seed(seed))
816793
}
817794
}

0 commit comments

Comments
 (0)