Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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 }
2 changes: 1 addition & 1 deletion src/borsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
Expand Down
23 changes: 19 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -12,6 +12,18 @@ use core::{
str::FromStr,
};

#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;

#[cfg(target_has_atomic = "ptr")]
type HeapPtr<T> = Arc<T>;

#[cfg(all(not(target_has_atomic = "ptr"), feature = "portable-atomic"))]
type HeapPtr<T> = portable_atomic_util::Arc<T>;

#[cfg(all(not(target_has_atomic = "ptr"), not(feature = "portable-atomic")))]
type HeapPtr<T> = alloc::boxed::Box<T>;

/// A `SmolStr` is a string type that has the following properties:
///
/// * `size_of::<SmolStr>() == 24` (therefor `== size_of::<String>()` on 64 bit platforms)
Expand Down Expand Up @@ -377,6 +389,7 @@ impl From<Box<str>> for SmolStr {
}
}

#[cfg(target_has_atomic = "ptr")]
impl From<Arc<str>> for SmolStr {
#[inline]
fn from(s: Arc<str>) -> SmolStr {
Expand All @@ -392,6 +405,7 @@ impl<'a> From<Cow<'a, str>> for SmolStr {
}
}

#[cfg(target_has_atomic = "ptr")]
impl From<SmolStr> for Arc<str> {
#[inline(always)]
fn from(text: SmolStr) -> Self {
Expand Down Expand Up @@ -483,7 +497,7 @@ enum Repr {
buf: [u8; INLINE_CAP],
},
Static(&'static str),
Heap(Arc<str>),
Heap(HeapPtr<str>),
}

impl Repr {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down