Skip to content

Commit 5877e23

Browse files
authored
Make the crate no_std (#1)
* Implement an alternative solution for the variance issue not involving `Mutex` * Get rid of dependency on `std`
1 parent ce09654 commit 5877e23

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@
7171
//! assert!(res);
7272
//! }
7373
//! ```
74+
#![no_std]
7475

75-
use std::sync::Mutex;
76-
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
77-
use std::marker::PhantomData;
78-
use std::fmt;
79-
use std::default::Default;
76+
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
77+
use core::marker::PhantomData;
78+
use core::fmt;
79+
use core::default::Default;
8080

8181
/// A mutable Option<&'a, T> type which can be safely shared between threads.
8282
#[repr(C)]
8383
pub struct AtomicRef<'a, T: 'a> {
8484
data: AtomicUsize,
85-
_marker: PhantomData<Mutex<&'a T>>,
85+
// Make `AtomicRef` invariant over `'a` and `T`
86+
_marker: PhantomData<&'a mut &'a mut T>,
8687
}
8788

8889
/// You will probably never need to use this type. It exists mostly for internal
@@ -104,6 +105,11 @@ pub const ATOMIC_U8_REF_INIT: AtomicRef<'static, u8> = AtomicRef {
104105
_marker: PhantomData,
105106
};
106107

108+
/// Re-export `core` for `static_atomic_ref!` (which may be used in a
109+
/// non-`no_std` crate, where `core` is unavailable).
110+
#[doc(hidden)]
111+
pub use core::{mem as core_mem, ops as core_ops};
112+
107113
/// A macro to define a statically allocated `AtomicRef<'static, T>` which is
108114
/// initialized to `None`.
109115
///
@@ -134,12 +140,12 @@ macro_rules! static_atomic_ref {
134140
};
135141
(@$VIS:ident, $(#[$attr:meta])* static $N:ident : $T:ty; $($t:tt)*) => {
136142
static_atomic_ref!(@MAKE TY, $VIS, $(#[$attr])*, $N);
137-
impl ::std::ops::Deref for $N {
143+
impl $crate::core_ops::Deref for $N {
138144
type Target = $crate::AtomicRef<'static, $T>;
139145
#[allow(unsafe_code)]
140146
fn deref<'a>(&'a self) -> &'a $crate::AtomicRef<'static, $T> {
141147
static STORAGE: $crate::AtomicRef<'static, u8> = $crate::ATOMIC_U8_REF_INIT;
142-
unsafe { ::std::mem::transmute(&STORAGE) }
148+
unsafe { $crate::core_mem::transmute(&STORAGE) }
143149
}
144150
}
145151
static_atomic_ref!($($t)*);
@@ -403,7 +409,7 @@ impl<'a, T> Default for AtomicRef<'a, T> {
403409

404410
#[cfg(test)]
405411
mod tests {
406-
use std::sync::atomic::Ordering;
412+
use core::sync::atomic::Ordering;
407413

408414
static_atomic_ref! {
409415
static FOO: AtomicRef<i32>;

0 commit comments

Comments
 (0)