Skip to content

Commit e6b542a

Browse files
committed
rand_core: implement reborrow for UnwrapMut
1 parent ad72bbb commit e6b542a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

rand_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## Unreleased
88
### API changes
99
- Relax `Sized` bound on impls of `TryRngCore`, `TryCryptoRng` and `UnwrapMut` (#1593)
10+
- Add `UnwrapMut::re` to reborrow the inner rng with a tighter lifetime (#1595)
1011

1112
### Other
1213
- Fixup clippy warnings (#1601)

rand_core/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ impl<R: TryCryptoRng> CryptoRng for UnwrapErr<R> {}
321321
#[derive(Debug, Eq, PartialEq, Hash)]
322322
pub struct UnwrapMut<'r, R: TryRngCore + ?Sized>(pub &'r mut R);
323323

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+
324339
impl<R: TryRngCore + ?Sized> RngCore for UnwrapMut<'_, R> {
325340
#[inline]
326341
fn next_u32(&mut self) -> u32 {

0 commit comments

Comments
 (0)