Skip to content

Commit fcf1571

Browse files
committed
Add Atomic*::from_mut.
The atomic equivalent of Cell::from_mut.
1 parent a0c290f commit fcf1571

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

library/core/src/sync/atomic.rs

+63
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,26 @@ impl AtomicBool {
358358
unsafe { &mut *(self.v.get() as *mut bool) }
359359
}
360360

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+
361381
/// Consumes the atomic and returns the contained value.
362382
///
363383
/// This is safe because passing `self` by value guarantees that no other threads are
@@ -914,6 +934,26 @@ impl<T> AtomicPtr<T> {
914934
unsafe { &mut *self.p.get() }
915935
}
916936

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+
917957
/// Consumes the atomic and returns the contained value.
918958
///
919959
/// 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);
13581398
}
13591399
}
13601400

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+
13611424
doc_comment! {
13621425
concat!("Consumes the atomic and returns the contained value.
13631426

0 commit comments

Comments
 (0)