Skip to content

Commit 4843173

Browse files
committed
Document why as_mut_ptr is safe
1 parent 23c5e58 commit 4843173

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libcore/sync/atomic.rs

+12
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ impl AtomicBool {
809809
/// This method is mostly useful for FFI, where the function signature may use
810810
/// `*mut bool` instead of `&AtomicBool`.
811811
///
812+
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
813+
/// atomic types work with interior mutability. All modifications of an atomic change the value
814+
/// through a shared reference, and can do so safely as long as they use atomic operations. Any
815+
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
816+
/// restriction: operations on it must be atomic.
817+
///
812818
/// [`bool`]: ../../../std/primitive.bool.html
813819
///
814820
/// # Examples
@@ -1929,6 +1935,12 @@ Doing non-atomic reads and writes on the resulting integer can be a data race.
19291935
This method is mostly useful for FFI, where the function signature may use
19301936
`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`.
19311937
1938+
Returning an `*mut` pointer from a shared reference to this atomic is safe because the
1939+
atomic types work with interior mutability. All modifications of an atomic change the value
1940+
through a shared reference, and can do so safely as long as they use atomic operations. Any
1941+
use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
1942+
restriction: operations on it must be atomic.
1943+
19321944
# Examples
19331945
19341946
```ignore (extern-declaration)

0 commit comments

Comments
 (0)