Skip to content

Commit fd4c68e

Browse files
committed
Replace AtomicUsize with AtomicPtr<T>
Raises the minimum supported Rust version to 1.32.0.
1 parent cb4e7c5 commit fd4c68e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@
7373
//! ```
7474
#![no_std]
7575

76-
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
76+
use core::sync::atomic::{AtomicPtr, Ordering};
7777
use core::marker::PhantomData;
7878
use core::fmt;
79+
use core::ptr::null_mut;
7980
use core::default::Default;
8081

8182
/// A mutable Option<&'a, T> type which can be safely shared between threads.
8283
#[repr(C)]
8384
pub struct AtomicRef<'a, T: 'a> {
84-
data: AtomicUsize,
85+
data: AtomicPtr<T>,
8586
// Make `AtomicRef` invariant over `'a` and `T`
8687
_marker: PhantomData<&'a mut &'a mut T>,
8788
}
@@ -101,7 +102,7 @@ pub struct AtomicRef<'a, T: 'a> {
101102
/// Please use `static_atomic_ref!` instead of this constant if you need to
102103
/// implement a static atomic reference variable.
103104
pub const ATOMIC_U8_REF_INIT: AtomicRef<'static, u8> = AtomicRef {
104-
data: ATOMIC_USIZE_INIT,
105+
data: AtomicPtr::new(null_mut()),
105106
_marker: PhantomData,
106107
};
107108

@@ -171,19 +172,19 @@ macro_rules! static_atomic_ref {
171172
() => ();
172173
}
173174

174-
/// An internal helper function for converting `Option<&'a T>` values to usize
175-
/// for storing in the `AtomicUsize`.
176-
fn from_opt<'a, T>(p: Option<&'a T>) -> usize {
175+
/// An internal helper function for converting `Option<&'a T>` values to
176+
/// `*mut T` for storing in the `AtomicUsize`.
177+
fn from_opt<'a, T>(p: Option<&'a T>) -> *mut T {
177178
match p {
178-
Some(p) => p as *const T as usize,
179-
None => 0,
179+
Some(p) => p as *const T as *mut T,
180+
None => null_mut(),
180181
}
181182
}
182183

183-
/// An internal helper function for converting `usize` values stored in the
184+
/// An internal helper function for converting `*mut T` values stored in the
184185
/// `AtomicUsize` back into `Option<&'a T>` values.
185-
unsafe fn to_opt<'a, T>(p: usize) -> Option<&'a T> {
186-
(p as *const T).as_ref()
186+
unsafe fn to_opt<'a, T>(p: *mut T) -> Option<&'a T> {
187+
p.as_ref()
187188
}
188189

189190
impl<'a, T> AtomicRef<'a, T> {
@@ -199,7 +200,7 @@ impl<'a, T> AtomicRef<'a, T> {
199200
/// ```
200201
pub fn new(p: Option<&'a T>) -> AtomicRef<'a, T> {
201202
AtomicRef {
202-
data: AtomicUsize::new(from_opt(p)),
203+
data: AtomicPtr::new(from_opt(p)),
203204
_marker: PhantomData,
204205
}
205206
}

0 commit comments

Comments
 (0)