@@ -358,6 +358,26 @@ impl AtomicBool {
358
358
unsafe { & mut * ( self . v . get ( ) as * mut bool ) }
359
359
}
360
360
361
+ /// Get atomic access to a `&mut bool`.
362
+ ///
363
+ /// # Examples
364
+ ///
365
+ /// ```
366
+ /// #![feature(atomic_from_mut)]
367
+ /// use std::sync::atomic::{AtomicBool, Ordering};
368
+ ///
369
+ /// let mut some_bool = true;
370
+ /// let a = AtomicBool::from_mut(&mut some_bool);
371
+ /// a.store(false, Ordering::Relaxed);
372
+ /// assert_eq!(some_bool, false);
373
+ /// ```
374
+ #[ inline]
375
+ #[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
376
+ pub fn from_mut ( v : & mut bool ) -> & Self {
377
+ // SAFETY: the mutable reference guarantees unique ownership.
378
+ unsafe { & * ( v as * mut bool as * mut Self ) }
379
+ }
380
+
361
381
/// Consumes the atomic and returns the contained value.
362
382
///
363
383
/// This is safe because passing `self` by value guarantees that no other threads are
@@ -914,6 +934,26 @@ impl<T> AtomicPtr<T> {
914
934
unsafe { & mut * self . p . get ( ) }
915
935
}
916
936
937
+ /// Get atomic access to a pointer.
938
+ ///
939
+ /// # Examples
940
+ ///
941
+ /// ```
942
+ /// #![feature(atomic_from_mut)]
943
+ /// use std::sync::atomic::{AtomicPtr, Ordering};
944
+ ///
945
+ /// let mut some_ptr = &mut 123 as *mut i32;
946
+ /// let a = AtomicPtr::from_mut(&mut some_ptr);
947
+ /// a.store(&mut 456, Ordering::Relaxed);
948
+ /// assert_eq!(unsafe { *some_ptr }, 456);
949
+ /// ```
950
+ #[ inline]
951
+ #[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
952
+ pub fn from_mut ( v : & mut * mut T ) -> & Self {
953
+ // SAFETY: the mutable reference guarantees unique ownership,
954
+ unsafe { & * ( v as * mut * mut T as * mut Self ) }
955
+ }
956
+
917
957
/// Consumes the atomic and returns the contained value.
918
958
///
919
959
/// This is safe because passing `self` by value guarantees that no other threads are
@@ -1358,6 +1398,29 @@ assert_eq!(some_var.load(Ordering::SeqCst), 5);
1358
1398
}
1359
1399
}
1360
1400
1401
+ doc_comment! {
1402
+ concat!( "Get atomic access to a `&mut " , stringify!( $int_type) , "`.
1403
+
1404
+ # Examples
1405
+
1406
+ ```
1407
+ #![feature(atomic_from_mut)]
1408
+ " , $extra_feature, "use std::sync::atomic::{" , stringify!( $atomic_type) , ", Ordering};
1409
+
1410
+ let mut some_int = 123;
1411
+ let a = " , stringify!( $atomic_type) , "::from_mut(&mut some_int);
1412
+ a.store(100, Ordering::Relaxed);
1413
+ assert_eq!(some_int, 100);
1414
+ ```
1415
+ " ) ,
1416
+ #[ inline]
1417
+ #[ unstable( feature = "atomic_from_mut" , issue = "none" ) ]
1418
+ pub fn from_mut( v: & mut $int_type) -> & Self {
1419
+ // SAFETY: the mutable reference guarantees unique ownership.
1420
+ unsafe { & * ( v as * mut $int_type as * mut Self ) }
1421
+ }
1422
+ }
1423
+
1361
1424
doc_comment! {
1362
1425
concat!( "Consumes the atomic and returns the contained value.
1363
1426
0 commit comments