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/sync.rs | |
| 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/sync.rs')
| -rw-r--r-- | src/liballoc/sync.rs | 31 |
1 files changed, 30 insertions, 1 deletions
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. /// |
