File tree Expand file tree Collapse file tree 2 files changed +60
-2
lines changed Expand file tree Collapse file tree 2 files changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -560,13 +560,42 @@ impl<T: ?Sized> Rc<T> {
560
560
pub fn get_mut ( this : & mut Self ) -> Option < & mut T > {
561
561
if Rc :: is_unique ( this) {
562
562
unsafe {
563
- Some ( & mut this . ptr . as_mut ( ) . value )
563
+ Some ( Rc :: get_mut_unchecked ( this ) )
564
564
}
565
565
} else {
566
566
None
567
567
}
568
568
}
569
569
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
+
570
599
#[ inline]
571
600
#[ stable( feature = "ptr_eq" , since = "1.17.0" ) ]
572
601
/// Returns `true` if the two `Rc`s point to the same value (not
Original file line number Diff line number Diff line change @@ -945,13 +945,42 @@ impl<T: ?Sized> Arc<T> {
945
945
// the Arc itself to be `mut`, so we're returning the only possible
946
946
// reference to the inner data.
947
947
unsafe {
948
- Some ( & mut this . ptr . as_mut ( ) . data )
948
+ Some ( Arc :: get_mut_unchecked ( this ) )
949
949
}
950
950
} else {
951
951
None
952
952
}
953
953
}
954
954
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
+
955
984
/// Determine whether this is the unique reference (including weak refs) to
956
985
/// the underlying data.
957
986
///
You can’t perform that action at this time.
0 commit comments