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
11 changes: 9 additions & 2 deletions flexstr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ pub mod traits;

use alloc::rc::Rc;
use alloc::string::String;
#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;
use core::fmt::{Arguments, Write};
use core::mem;
use core::mem::ManuallyDrop;
use core::ops::Deref;

use static_assertions::{assert_eq_align, assert_eq_size, assert_impl_all, assert_not_impl_any};
use static_assertions::{assert_eq_align, assert_eq_size, assert_not_impl_any};

use crate::storage::heap::HeapStr;
use crate::storage::inline::InlineFlexStr;
Expand All @@ -120,8 +121,13 @@ mod test_readme {
}

assert_eq_size!(LocalStr, String);
assert_eq_size!(SharedStr, String);
assert_not_impl_any!(LocalStr: Send, Sync);

#[cfg(target_has_atomic = "ptr")]
use static_assertions::assert_impl_all;
#[cfg(target_has_atomic = "ptr")]
assert_eq_size!(SharedStr, String);
#[cfg(target_has_atomic = "ptr")]
assert_impl_all!(SharedStr: Send, Sync);

assert_eq_size!(HeapStr<PTR_SIZED_PAD, Rc<str>>, InlineFlexStr<STRING_SIZED_INLINE>);
Expand Down Expand Up @@ -172,6 +178,7 @@ pub type LocalStr = FlexStrBase<Rc<str>>;
///
/// # Note
/// Since this is just a type alias for a generic type, full documentation can be found here: [FlexStr]
#[cfg(target_has_atomic = "ptr")]
pub type SharedStr = FlexStrBase<Arc<str>>;

// *** Clone ***
Expand Down
13 changes: 12 additions & 1 deletion flexstr/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use core::mem::ManuallyDrop;
use core::ops::Deref;

use crate::{
FlexStr, HeapStr, LocalStr, SharedStr, StorageType, PTR_SIZED_PAD, STRING_SIZED_INLINE,
FlexStr, HeapStr, LocalStr, StorageType, PTR_SIZED_PAD, STRING_SIZED_INLINE,
};

#[cfg(target_has_atomic = "ptr")]
use crate::SharedStr;

// *** Repeat custom trait ***

/// Trait that can repeat a given [FlexStr] "n" times efficiently
Expand Down Expand Up @@ -560,11 +563,13 @@ impl_float_local_str!(f32, f64);
/// let a = "This is a heap allocated string!!!!!".to_shared_str();
/// assert!(a.is_heap());
/// ```
#[cfg(target_has_atomic = "ptr")]
pub trait ToSharedStr {
/// Converts the source to a [SharedStr] without consuming it
fn to_shared_str(&self) -> SharedStr;
}

#[cfg(target_has_atomic = "ptr")]
impl<HEAP> ToSharedStr for FlexStr<STRING_SIZED_INLINE, PTR_SIZED_PAD, PTR_SIZED_PAD, HEAP>
where
HEAP: Clone + Deref<Target = str>,
Expand All @@ -582,6 +587,7 @@ where
}
}

#[cfg(target_has_atomic = "ptr")]
impl ToSharedStr for str {
/// ```
/// use flexstr::ToSharedStr;
Expand All @@ -596,6 +602,7 @@ impl ToSharedStr for str {
}
}

#[cfg(target_has_atomic = "ptr")]
impl ToSharedStr for bool {
/// ```
/// use flexstr::ToSharedStr;
Expand All @@ -610,6 +617,7 @@ impl ToSharedStr for bool {
}
}

#[cfg(target_has_atomic = "ptr")]
impl ToSharedStr for char {
/// ```
/// use flexstr::ToSharedStr;
Expand Down Expand Up @@ -725,11 +733,13 @@ impl IntoLocalStr for String {
/// let a = shared_str!("This is a wrapped static string literal no matter how long it is!!!!!");
/// assert!(a.is_static());
/// ```
#[cfg(target_has_atomic = "ptr")]
pub trait IntoSharedStr {
/// Converts the source to an [SharedStr] while consuming the original
fn into_shared_str(self) -> SharedStr;
}

#[cfg(target_has_atomic = "ptr")]
impl<HEAP> IntoSharedStr for FlexStr<STRING_SIZED_INLINE, PTR_SIZED_PAD, PTR_SIZED_PAD, HEAP>
where
HEAP: Deref<Target = str>,
Expand All @@ -749,6 +759,7 @@ where
}
}

#[cfg(target_has_atomic = "ptr")]
impl IntoSharedStr for String {
/// ```
/// use flexstr::IntoSharedStr;
Expand Down