File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -1583,6 +1583,7 @@ symbols! {
1583
1583
unrestricted_attribute_tokens,
1584
1584
unsafe_block_in_unsafe_fn,
1585
1585
unsafe_cell,
1586
+ unsafe_cell_from_mut,
1586
1587
unsafe_no_drop_flag,
1587
1588
unsafe_pin_internals,
1588
1589
unsize,
Original file line number Diff line number Diff line change @@ -2030,6 +2030,27 @@ impl<T> UnsafeCell<T> {
2030
2030
}
2031
2031
2032
2032
impl < T : ?Sized > UnsafeCell < T > {
2033
+ /// Converts from `&mut T` to `&mut UnsafeCell<T>`.
2034
+ ///
2035
+ /// # Examples
2036
+ ///
2037
+ /// ```
2038
+ /// # #![feature(unsafe_cell_from_mut)]
2039
+ /// use std::cell::UnsafeCell;
2040
+ ///
2041
+ /// let mut val = 42;
2042
+ /// let uc = UnsafeCell::from_mut(&mut val);
2043
+ ///
2044
+ /// *uc.get_mut() -= 1;
2045
+ /// assert_eq!(*uc.get_mut(), 41);
2046
+ /// ```
2047
+ #[ inline( always) ]
2048
+ #[ unstable( feature = "unsafe_cell_from_mut" , issue = "111645" ) ]
2049
+ pub const fn from_mut ( value : & mut T ) -> & mut UnsafeCell < T > {
2050
+ // SAFETY: `UnsafeCell<T>` has the same memory layout as `T` due to #[repr(transparent)].
2051
+ unsafe { & mut * ( value as * mut T as * mut UnsafeCell < T > ) }
2052
+ }
2053
+
2033
2054
/// Gets a mutable pointer to the wrapped value.
2034
2055
///
2035
2056
/// This can be cast to a pointer of any kind.
You can’t perform that action at this time.
0 commit comments