Skip to content

Commit 1613fda

Browse files
committed
Add Rc::get_mut_unchecked, Arc::get_mut_unchecked
1 parent 9dd5c19 commit 1613fda

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/liballoc/rc.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,42 @@ impl<T: ?Sized> Rc<T> {
560560
pub fn get_mut(this: &mut Self) -> Option<&mut T> {
561561
if Rc::is_unique(this) {
562562
unsafe {
563-
Some(&mut this.ptr.as_mut().value)
563+
Some(Rc::get_mut_unchecked(this))
564564
}
565565
} else {
566566
None
567567
}
568568
}
569569

570+
/// Returns a mutable reference to the inner value,
571+
/// without any check.
572+
///
573+
/// See also [`get_mut`], which is safe and does appropriate checks.
574+
///
575+
/// [`get_mut`]: struct.Rc.html#method.get_mut
576+
///
577+
/// # Safety
578+
///
579+
/// There must be no other `Rc` or [`Weak`][weak] pointers to the same value.
580+
/// This is the case for example immediately after `Rc::new`.
581+
///
582+
/// # Examples
583+
///
584+
/// ```
585+
/// use std::rc::Rc;
586+
///
587+
/// let mut x = Rc::new(String::new());
588+
/// unsafe {
589+
/// Rc::get_mut_unchecked(&mut x).push_str("foo")
590+
/// }
591+
/// assert_eq!(*x, "foo");
592+
/// ```
593+
#[inline]
594+
#[unstable(feature = "get_mut_unchecked", issue = "0")]
595+
pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
596+
&mut this.ptr.as_mut().value
597+
}
598+
570599
#[inline]
571600
#[stable(feature = "ptr_eq", since = "1.17.0")]
572601
/// Returns `true` if the two `Rc`s point to the same value (not

src/liballoc/sync.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,13 +945,42 @@ impl<T: ?Sized> Arc<T> {
945945
// the Arc itself to be `mut`, so we're returning the only possible
946946
// reference to the inner data.
947947
unsafe {
948-
Some(&mut this.ptr.as_mut().data)
948+
Some(Arc::get_mut_unchecked(this))
949949
}
950950
} else {
951951
None
952952
}
953953
}
954954

955+
/// Returns a mutable reference to the inner value,
956+
/// without any check.
957+
///
958+
/// See also [`get_mut`], which is safe and does appropriate checks.
959+
///
960+
/// [`get_mut`]: struct.Arc.html#method.get_mut
961+
///
962+
/// # Safety
963+
///
964+
/// There must be no other `Arc` or [`Weak`][weak] pointers to the same value.
965+
/// This is the case for example immediately after `Rc::new`.
966+
///
967+
/// # Examples
968+
///
969+
/// ```
970+
/// use std::sync::Arc;
971+
///
972+
/// let mut x = Arc::new(String::new());
973+
/// unsafe {
974+
/// Arc::get_mut_unchecked(&mut x).push_str("foo")
975+
/// }
976+
/// assert_eq!(*x, "foo");
977+
/// ```
978+
#[inline]
979+
#[unstable(feature = "get_mut_unchecked", issue = "0")]
980+
pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
981+
&mut this.ptr.as_mut().data
982+
}
983+
955984
/// Determine whether this is the unique reference (including weak refs) to
956985
/// the underlying data.
957986
///

0 commit comments

Comments
 (0)