diff --git a/Cargo.toml b/Cargo.toml index e89e0e8..f856271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,13 @@ serde = { version = "1.0", features = ["derive"] } [features] default = ["std"] std = ["serde?/std", "borsh?/std"] +portable-atomic = [ + "dep:portable-atomic", + "dep:portable-atomic-util", +] + +[target.'cfg(not(target_has_atomic = "ptr"))'.dependencies] +portable-atomic = { version = "1", default-features = false, optional = true } +portable-atomic-util = { version = "0.2.4", features = [ + "alloc", +], optional = true } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index bf88f57..bb9435a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ extern crate alloc; -use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc}; +use alloc::{borrow::Cow, boxed::Box, string::String}; use core::{ borrow::Borrow, cmp::{self, Ordering}, @@ -12,6 +12,17 @@ use core::{ str::FromStr, }; +#[cfg(all(not(target_has_atomic = "ptr"), feature = "portable-atomic"))] +use portable_atomic_util::Arc; + +#[cfg(target_has_atomic = "ptr")] +use alloc::sync::Arc; + +#[cfg(all(not(target_has_atomic = "ptr"), not(feature = "portable-atomic")))] +compile_error!( + "Targets without support for atomic pointers must enable the `portable-atomic` feature" +); + /// A `SmolStr` is a string type that has the following properties: /// /// * `size_of::() == 24` (therefor `== size_of::()` on 64 bit platforms)