diff --git a/Cargo.toml b/Cargo.toml index e89e0e8..c4792e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ rustdoc-args = ["--cfg", "docsrs"] all-features = true [dependencies] -serde = { version = "1.0", optional = true, default-features = false } +serde = { version = "1.0", optional = true, default-features = false, features = ["alloc"] } borsh = { version = "1.4.0", optional = true, default-features = false } arbitrary = { version = "1.3", optional = true } @@ -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/borsh.rs b/src/borsh.rs index 362c288..7cd261d 100644 --- a/src/borsh.rs +++ b/src/borsh.rs @@ -2,7 +2,7 @@ use crate::{Repr, SmolStr, INLINE_CAP}; use alloc::string::{String, ToString}; use borsh::io::{Error, ErrorKind, Read, Write}; use borsh::{BorshDeserialize, BorshSerialize}; -use core::intrinsics::transmute; +use core::mem::transmute; impl BorshSerialize for SmolStr { fn serialize(&self, writer: &mut W) -> borsh::io::Result<()> { diff --git a/src/lib.rs b/src/lib.rs index bf88f57..071dbee 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,18 @@ use core::{ str::FromStr, }; +#[cfg(target_has_atomic = "ptr")] +use alloc::sync::Arc; + +#[cfg(target_has_atomic = "ptr")] +type HeapPtr = Arc; + +#[cfg(all(not(target_has_atomic = "ptr"), feature = "portable-atomic"))] +type HeapPtr = portable_atomic_util::Arc; + +#[cfg(all(not(target_has_atomic = "ptr"), not(feature = "portable-atomic")))] +type HeapPtr = alloc::boxed::Box; + /// A `SmolStr` is a string type that has the following properties: /// /// * `size_of::() == 24` (therefor `== size_of::()` on 64 bit platforms) @@ -377,6 +389,7 @@ impl From> for SmolStr { } } +#[cfg(target_has_atomic = "ptr")] impl From> for SmolStr { #[inline] fn from(s: Arc) -> SmolStr { @@ -392,6 +405,7 @@ impl<'a> From> for SmolStr { } } +#[cfg(target_has_atomic = "ptr")] impl From for Arc { #[inline(always)] fn from(text: SmolStr) -> Self { @@ -483,7 +497,7 @@ enum Repr { buf: [u8; INLINE_CAP], }, Static(&'static str), - Heap(Arc), + Heap(HeapPtr), } impl Repr { @@ -524,7 +538,7 @@ impl Repr { } fn new(text: &str) -> Self { - Self::new_on_stack(text).unwrap_or_else(|| Repr::Heap(Arc::from(text))) + Self::new_on_stack(text).unwrap_or_else(|| Repr::Heap(HeapPtr::from(text))) } #[inline(always)] @@ -562,7 +576,8 @@ impl Repr { fn ptr_eq(&self, other: &Self) -> bool { match (self, other) { - (Self::Heap(l0), Self::Heap(r0)) => Arc::ptr_eq(l0, r0), + #[cfg(any(target_has_atomic = "ptr", feature = "portable-atomic"))] + (Self::Heap(l0), Self::Heap(r0)) => HeapPtr::ptr_eq(l0, r0), (Self::Static(l0), Self::Static(r0)) => core::ptr::eq(l0, r0), ( Self::Inline {