diff --git a/CHANGELOG.md b/CHANGELOG.md index c6909d2..04e3986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Add `crossbeam` feature, enabled by default, to enable `crossbeam-utils` dependency and `AtomicLazy` struct. + ## [0.2.0] - 2025-02-13 ### Fixed diff --git a/Cargo.toml b/Cargo.toml index 91fda9b..4963209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ include = [ [dependencies] portable-atomic = "1" -crossbeam-utils = { version = "0.8.15", default-features = false } +crossbeam-utils = { version = "0.8.15", default-features = false, optional = true } serde = { version = "1.0.171", optional = true } [target.'cfg(loom)'.dependencies] @@ -26,5 +26,6 @@ loom = "0.7.0" serde_json = "1.0.104" [features] -default = [] -serde = ["dep:serde"] +default = ["crossbeam"] +crossbeam = ["dep:crossbeam-utils"] +serde = ["dep:serde"] diff --git a/README.md b/README.md index 06bd3d9..56ff612 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,10 @@ lock-free (if you know a way, please tell me). Both types can be used in a non-blocking way, but there are some blocking calls that should not be used from interrupt handlers or other contexts where blocking will lead to a deadlock. Blocking is -based on -[`crossbeam::utils::Backoff`](https://docs.rs/crossbeam/latest/crossbeam/utils/struct.Backoff.html), -and will be reduced to a spinlock in `#[no_std]` environments. +based on [`crossbeam::utils::Backoff`] and will be reduced to a spinlock +in `#[no_std]` environments. This is enabled via `crossbeam` feature. + +[`crossbeam::utils::Backoff`]: https://docs.rs/crossbeam/latest/crossbeam/utils/struct.Backoff.html, ### Examples #### `AtomicOnceCell` diff --git a/src/lib.rs b/src/lib.rs index aaa0e48..e74cebe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,10 @@ //! Both types can be used in a non-blocking way, but there are some //! blocking calls that should not be used from interrupt handlers or //! other contexts where blocking will lead to a deadlock. Blocking is -//! based on -//! [`crossbeam::utils::Backoff`](https://docs.rs/crossbeam/latest/crossbeam/utils/struct.Backoff.html), -//! and will be reduced to a spinlock in `#[no_std]` environments. +//! based on [`crossbeam::utils::Backoff`] and will be reduced to a spinlock +//! in `#[no_std]` environments. This is enabled via `crossbeam` feature. +//! +//! [`crossbeam::utils::Backoff`]: https://docs.rs/crossbeam/latest/crossbeam/utils/struct.Backoff.html, //! //! ## Examples //! ### `AtomicOnceCell` @@ -48,13 +49,13 @@ #![no_std] -use core::{ - cell::{Cell, UnsafeCell}, - fmt, - ops::Deref, - sync::atomic::Ordering, +use core::{cell::UnsafeCell, fmt, sync::atomic::Ordering}; + +#[cfg(feature = "crossbeam")] +use { + core::{cell::Cell, ops::Deref}, + crossbeam_utils::Backoff, }; -use crossbeam_utils::Backoff; #[cfg(not(loom))] use portable_atomic::AtomicU8; @@ -269,6 +270,7 @@ impl AtomicOnceCell { /// let value = cell.get_or_init(|| unreachable!()); /// assert_eq!(value, &92); /// ``` + #[cfg(feature = "crossbeam")] pub fn get_or_init(&self, f: F) -> &T where F: FnOnce() -> T, @@ -311,6 +313,7 @@ impl AtomicOnceCell { /// assert_eq!(value, Ok(&92)); /// assert_eq!(cell.get(), Some(&92)) /// ``` + #[cfg(feature = "crossbeam")] pub fn get_or_try_init(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result, @@ -456,12 +459,13 @@ impl Serialize for AtomicOnceCell { /// // asynchronously at some point /// // [...] /// } - +#[cfg(feature = "crossbeam")] pub struct AtomicLazy T> { cell: AtomicOnceCell, init: Cell>, } +#[cfg(feature = "crossbeam")] impl fmt::Debug for AtomicLazy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("AtomicLazy") @@ -471,6 +475,7 @@ impl fmt::Debug for AtomicLazy { } } +#[cfg(feature = "crossbeam")] impl Default for AtomicLazy { /// Creates a new lazy value using `Default` as the initializing function. fn default() -> AtomicLazy { @@ -478,6 +483,7 @@ impl Default for AtomicLazy { } } +#[cfg(feature = "crossbeam")] unsafe impl Sync for AtomicLazy where T: Send + Sync, @@ -485,6 +491,7 @@ where { } +#[cfg(feature = "crossbeam")] impl AtomicLazy { /// Creates a new lazy value with the given initializing function. /// @@ -518,6 +525,7 @@ impl AtomicLazy { } } +#[cfg(feature = "crossbeam")] impl AtomicLazy where F: FnOnce() -> T, @@ -585,6 +593,7 @@ where } } +#[cfg(feature = "crossbeam")] impl Deref for AtomicLazy where F: FnOnce() -> T,