Skip to content
Merged
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
11 changes: 8 additions & 3 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::string::String;

#[cfg(feature = "arc")]
pub(crate) type DefaultStr = crate::backend::ArcStr;
#[cfg(not(feature = "arc"))]
Expand All @@ -8,15 +13,15 @@ pub type BoxedStr = Box<str>;
static_assertions::assert_eq_size!(DefaultStr, BoxedStr);

/// Cross-thread, O(1) clones
pub type ArcStr = std::sync::Arc<str>;
pub type ArcStr = alloc::sync::Arc<str>;
static_assertions::assert_eq_size!(DefaultStr, ArcStr);

/// O(1) clones
pub type RcStr = std::rc::Rc<str>;
pub type RcStr = alloc::rc::Rc<str>;
static_assertions::assert_eq_size!(DefaultStr, RcStr);

/// Abstract over different type of heap-allocated strings
pub trait HeapStr: std::fmt::Debug + Clone + private::Sealed {
pub trait HeapStr: core::fmt::Debug + Clone + private::Sealed {
fn from_str(other: &str) -> Self;
fn from_string(other: String) -> Self;
fn from_boxed_str(other: BoxedStr) -> Self;
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
//!
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))]
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::std_instead_of_core)]
#![warn(clippy::std_instead_of_alloc)]

#[cfg(not(feature = "std"))]
compile_error!("`std` feature is required; reserved for future `no_std` support");
#[allow(unused_extern_crates)]
extern crate alloc;

mod stack;
mod string;
Expand All @@ -60,20 +63,20 @@ mod test {
fn test_size() {
println!(
"String: {}",
std::mem::size_of::<crate::string::StdString>()
core::mem::size_of::<crate::string::StdString>()
);
println!(
"Box<str>: {}",
std::mem::size_of::<crate::backend::DefaultStr>()
core::mem::size_of::<crate::backend::DefaultStr>()
);
println!(
"Box<Box<str>>: {}",
std::mem::size_of::<Box<crate::backend::DefaultStr>>()
core::mem::size_of::<Box<crate::backend::DefaultStr>>()
);
println!("str: {}", std::mem::size_of::<&'static str>());
println!("str: {}", core::mem::size_of::<&'static str>());
println!(
"Cow: {}",
std::mem::size_of::<std::borrow::Cow<'static, str>>()
core::mem::size_of::<alloc::borrow::Cow<'static, str>>()
);
}
}
32 changes: 18 additions & 14 deletions src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::fmt;

pub(crate) type Len = u8;

Expand Down Expand Up @@ -257,7 +259,7 @@ impl<const CAPACITY: usize> Default for StackString<CAPACITY> {
}
}

impl<const CAPACITY: usize> std::ops::Deref for StackString<CAPACITY> {
impl<const CAPACITY: usize> core::ops::Deref for StackString<CAPACITY> {
type Target = str;

#[inline]
Expand Down Expand Up @@ -298,42 +300,42 @@ impl<const CAPACITY: usize> PartialEq<String> for StackString<CAPACITY> {

impl<const CAPACITY: usize> Ord for StackString<CAPACITY> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}

impl<const C1: usize, const C2: usize> PartialOrd<StackString<C1>> for StackString<C2> {
#[inline]
fn partial_cmp(&self, other: &StackString<C1>) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &StackString<C1>) -> Option<core::cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}

impl<const CAPACITY: usize> PartialOrd<str> for StackString<CAPACITY> {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &str) -> Option<core::cmp::Ordering> {
self.as_str().partial_cmp(other)
}
}

impl<const CAPACITY: usize> PartialOrd<&str> for StackString<CAPACITY> {
#[inline]
fn partial_cmp(&self, other: &&str) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &&str) -> Option<core::cmp::Ordering> {
self.as_str().partial_cmp(other)
}
}

impl<const CAPACITY: usize> PartialOrd<String> for StackString<CAPACITY> {
#[inline]
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &String) -> Option<core::cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}

impl<const CAPACITY: usize> std::hash::Hash for StackString<CAPACITY> {
impl<const CAPACITY: usize> core::hash::Hash for StackString<CAPACITY> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
Expand Down Expand Up @@ -366,21 +368,23 @@ impl<const CAPACITY: usize> AsRef<[u8]> for StackString<CAPACITY> {
}
}

#[cfg(feature = "std")]
impl<const CAPACITY: usize> AsRef<std::ffi::OsStr> for StackString<CAPACITY> {
#[inline]
fn as_ref(&self) -> &std::ffi::OsStr {
(**self).as_ref()
}
}

#[cfg(feature = "std")]
impl<const CAPACITY: usize> AsRef<std::path::Path> for StackString<CAPACITY> {
#[inline]
fn as_ref(&self) -> &std::path::Path {
std::path::Path::new(self)
}
}

impl<const CAPACITY: usize> std::borrow::Borrow<str> for StackString<CAPACITY> {
impl<const CAPACITY: usize> alloc::borrow::Borrow<str> for StackString<CAPACITY> {
#[inline]
fn borrow(&self) -> &str {
self.as_str()
Expand Down Expand Up @@ -414,14 +418,14 @@ impl<const CAPACITY: usize> StrBuffer<CAPACITY> {
#[cfg(not(feature = "unsafe"))]
pub(crate) fn as_str(&self, len: usize) -> &str {
let slice = self.0.get(..len).unwrap();
std::str::from_utf8(slice).unwrap()
core::str::from_utf8(slice).unwrap()
}

#[inline]
#[cfg(not(feature = "unsafe"))]
pub(crate) fn as_mut_str(&mut self, len: usize) -> &mut str {
let slice = self.0.get_mut(..len).unwrap();
std::str::from_utf8_mut(slice).unwrap()
core::str::from_utf8_mut(slice).unwrap()
}
}

Expand All @@ -443,14 +447,14 @@ impl<const CAPACITY: usize> StrBuffer<CAPACITY> {
#[cfg(feature = "unsafe")]
pub(crate) unsafe fn as_str_unchecked(&self, len: usize) -> &str {
let slice = self.0.get_unchecked(..len);
std::str::from_utf8_unchecked(slice)
core::str::from_utf8_unchecked(slice)
}

#[inline]
#[cfg(feature = "unsafe")]
pub(crate) unsafe fn as_mut_str_unchecked(&mut self, len: usize) -> &mut str {
let slice = self.0.get_unchecked_mut(..len);
std::str::from_utf8_unchecked_mut(slice)
core::str::from_utf8_unchecked_mut(slice)
}
}

Expand Down
Loading
Loading