@@ -321,6 +321,21 @@ impl<R: TryCryptoRng> CryptoRng for UnwrapErr<R> {}
321
321
#[ derive( Debug , Eq , PartialEq , Hash ) ]
322
322
pub struct UnwrapMut < ' r , R : TryRngCore + ?Sized > ( pub & ' r mut R ) ;
323
323
324
+ impl < ' r , R : TryRngCore + ?Sized > UnwrapMut < ' r , R > {
325
+ /// Reborrow with a new lifetime
326
+ ///
327
+ /// Rust allows references like `&T` or `&mut T` to be "reborrowed" through
328
+ /// coercion: essentially, the pointer is copied under a new, shorter, lifetime.
329
+ /// Until rfcs#1403 lands, reborrows on user types require a method call.
330
+ #[ inline( always) ]
331
+ pub fn re < ' b > ( & ' b mut self ) -> UnwrapMut < ' b , R >
332
+ where
333
+ ' r : ' b ,
334
+ {
335
+ UnwrapMut ( self . 0 )
336
+ }
337
+ }
338
+
324
339
impl < R : TryRngCore + ?Sized > RngCore for UnwrapMut < ' _ , R > {
325
340
#[ inline]
326
341
fn next_u32 ( & mut self ) -> u32 {
@@ -726,4 +741,31 @@ mod test {
726
741
727
742
assert ! ( my_api( & mut SomeRng ) ) ;
728
743
}
744
+
745
+ #[ test]
746
+ fn reborrow_unwrap_mut ( ) {
747
+ struct FourRng ;
748
+
749
+ impl TryRngCore for FourRng {
750
+ type Error = core:: convert:: Infallible ;
751
+ fn try_next_u32 ( & mut self ) -> Result < u32 , Self :: Error > {
752
+ Ok ( 4 )
753
+ }
754
+ fn try_next_u64 ( & mut self ) -> Result < u64 , Self :: Error > {
755
+ unimplemented ! ( )
756
+ }
757
+ fn try_fill_bytes ( & mut self , _: & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
758
+ unimplemented ! ( )
759
+ }
760
+ }
761
+
762
+ let mut rng = FourRng ;
763
+ let mut rng = rng. unwrap_mut ( ) ;
764
+
765
+ assert_eq ! ( rng. next_u32( ) , 4 ) ;
766
+ let mut rng2 = rng. re ( ) ;
767
+ assert_eq ! ( rng2. next_u32( ) , 4 ) ;
768
+ drop ( rng2) ;
769
+ assert_eq ! ( rng. next_u32( ) , 4 ) ;
770
+ }
729
771
}
0 commit comments