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 | |
| parent | 9dd5c191993aab6c2f1538eb8ab69afdc4b6e67a (diff) | |
| download | rust-1613fdae37df044f2c254d25d356b3a57eb61a50.tar.gz rust-1613fdae37df044f2c254d25d356b3a57eb61a50.zip | |
Add Rc::get_mut_unchecked, Arc::get_mut_unchecked
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/rc.rs | 31 | ||||
| -rw-r--r-- | src/liballoc/sync.rs | 31 |
2 files changed, 60 insertions, 2 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 diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 7d3b2656a7b..4cd8e1fd4dd 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -945,13 +945,42 @@ impl<T: ?Sized> Arc<T> { // the Arc itself to be `mut`, so we're returning the only possible // reference to the inner data. unsafe { - Some(&mut this.ptr.as_mut().data) + Some(Arc::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.Arc.html#method.get_mut + /// + /// # Safety + /// + /// There must be no other `Arc` or [`Weak`][weak] pointers to the same value. + /// This is the case for example immediately after `Rc::new`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let mut x = Arc::new(String::new()); + /// unsafe { + /// Arc::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().data + } + /// Determine whether this is the unique reference (including weak refs) to /// the underlying data. /// |
