diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 1b9c24c0fa4..331ab30250a 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -388,6 +388,36 @@ impl RngCore for Box { } } +// Implement `RngCore` for Rc> references to a `RngCore`. +// Force inlining all functions, so that it is up to the `RngCore` +// implementation and the optimizer to decide on inlining. +// +// We do not actually use `Rc` here because it requires `std` but its +// `Deref` should make this suffice, and this can be used on the stack. +// #[cfg(feature="std")] +// impl RngCore for ::std::rc::Rc<::std::cell::RefCell> { +impl<'a,R: RngCore> RngCore for &'a ::core::cell::RefCell { + #[inline(always)] + fn next_u32(&mut self) -> u32 { + self.borrow_mut().next_u32() + } + + #[inline(always)] + fn next_u64(&mut self) -> u64 { + self.borrow_mut().next_u64() + } + + #[inline(always)] + fn fill_bytes(&mut self, dest: &mut [u8]) { + self.borrow_mut().fill_bytes(dest) + } + + #[inline(always)] + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + self.borrow_mut().try_fill_bytes(dest) + } +} + #[cfg(feature="std")] impl std::io::Read for RngCore { fn read(&mut self, buf: &mut [u8]) -> Result { diff --git a/src/lib.rs b/src/lib.rs index a0f46a7789d..49db76870a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1089,6 +1089,19 @@ mod test { let _c: u8 = Standard.sample(&mut r); } + #[test] + // #[cfg(feature="stc")] + fn test_rng_rc_refcell_trait() { + use std::ops::Deref; + use distributions::{Distribution, Standard}; + // let rng = rng(110); + let mut r = & *::std::rc::Rc::new(::std::cell::RefCell::new(rng(110))); + r.next_u32(); + r.gen::(); + assert_eq!(r.gen_range(0, 1), 0); + let _c: u8 = Standard.sample(&mut r); + } + #[test] #[cfg(feature="std")] fn test_random() {