diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2019-07-06 18:17:24 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2019-08-16 17:11:18 +0200 |
| commit | 1613fdae37df044f2c254d25d356b3a57eb61a50 (patch) | |
| tree | bcc4b14c944b11153fe22bec296121f156e37722 /src/liballoc/rc.rs | |
| parent | 9dd5c191993aab6c2f1538eb8ab69afdc4b6e67a (diff) | |
Add Rc::get_mut_unchecked, Arc::get_mut_unchecked
Diffstat (limited to 'src/liballoc/rc.rs')
| -rw-r--r-- | src/liballoc/rc.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 0c406a92029..22d06180d8d 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -560,13 +560,42 @@ impl<T: ?Sized> Rc<T> { pub fn get_mut(this: &mut Self) -> Option<&mut T> { if Rc::is_unique(this) { unsafe { - Some(&mut this.ptr.as_mut().value) + Some(Rc::get_mut_unchecked(this)) } } else { None } } + /// Returns a mutable reference to the inner value, + /// without any check. + /// + /// See also [`get_mut`], which is safe and does appropriate checks. + /// + /// [`get_mut`]: struct.Rc.html#method.get_mut + /// + /// # Safety + /// + /// There must be no other `Rc` or [`Weak`][weak] pointers to the same value. + /// This is the case for example immediately after `Rc::new`. + /// + /// # Examples + /// + /// ``` + /// use std::rc::Rc; + /// + /// let mut x = Rc::new(String::new()); + /// unsafe { + /// Rc::get_mut_unchecked(&mut x).push_str("foo") + /// } + /// assert_eq!(*x, "foo"); + /// ``` + #[inline] + #[unstable(feature = "get_mut_unchecked", issue = "0")] + pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { + &mut this.ptr.as_mut().value + } + #[inline] #[stable(feature = "ptr_eq", since = "1.17.0")] /// Returns `true` if the two `Rc`s point to the same value (not |
