Skip to content

Commit c1108bf

Browse files
Added an example of usage where an unsafe block is not required
1 parent aef7fbf commit c1108bf

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

library/core/src/sync/atomic.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -3402,11 +3402,8 @@ macro_rules! atomic_int {
34023402
/// This method is mostly useful for FFI, where the function signature may use
34033403
#[doc = concat!("`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`.")]
34043404
///
3405-
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
3406-
/// atomic types work with interior mutability. All modifications of an atomic change the value
3407-
/// through a shared reference, and can do so safely as long as they use atomic operations. Any
3408-
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
3409-
/// restriction: operations on it must not lead to data races.
3405+
/// All modifications of an atomic change the value through a shared reference, and can do so safely
3406+
/// as long as they use atomic operations.
34103407
///
34113408
/// # Examples
34123409
///
@@ -3420,12 +3417,24 @@ macro_rules! atomic_int {
34203417
///
34213418
#[doc = concat!("let atomic = ", stringify!($atomic_type), "::new(1);")]
34223419
///
3423-
/// // SAFETY: Safe as long as `my_atomic_op` does not lead to a data race.
3420+
/// // SAFETY: `my_atomic_op` only uses atomic operations so it will not lead to a data race.
34243421
/// unsafe {
34253422
/// my_atomic_op(atomic.as_ptr());
34263423
/// }
34273424
/// # }
34283425
/// ```
3426+
///
3427+
/// ```
3428+
#[doc = concat!($extra_feature, "use std::sync::atomic::", stringify!($atomic_type), ";")]
3429+
///
3430+
///
3431+
#[doc = concat!("let atomic_ref_1 = ", stringify!($atomic_type), "::new(1);")]
3432+
#[doc = concat!("let atomic_ref_2 = ", stringify!($atomic_type), "::new(2);")]
3433+
///
3434+
/// // Comparison of addresses is a safe operation and does not require an `unsafe` block.
3435+
/// let is_equal = atomic_ref_1 == atomic_ref_2;
3436+
/// assert!(is_equal || !is_equal);
3437+
/// ```
34293438
#[inline]
34303439
#[stable(feature = "atomic_as_ptr", since = "1.70.0")]
34313440
#[rustc_const_stable(feature = "atomic_as_ptr", since = "1.70.0")]

0 commit comments

Comments
 (0)