diff --git a/thin-str/src/lib.rs b/thin-str/src/lib.rs index c2c09860191..2038ca2ef95 100644 --- a/thin-str/src/lib.rs +++ b/thin-str/src/lib.rs @@ -17,11 +17,22 @@ pub use crate::thin_string::*; /// mutable references. #[repr(C)] pub struct Storage { + /// The header stores the number of bytes used in the string. header: ThinHeader, - /// The bytes of the strings are stored here. They need to be a valid str. + /// The bytes of the strings are stored here. data: str, } +/// Represents a [Storage] with a known-at-compile-time len for the str. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct ConstStorage { + /// The header stores the number of bytes used in the string. + header: ThinHeader, + /// The bytes of the string are stored here, it must be a valid str. + data: [u8; N], +} + /// The alignment is so that tagged pointers can use the least significant bit /// for storing other things if they wish. However, it's intentionally minimal /// so that strings can be packed tightly into arenas. Wasting a single @@ -35,14 +46,6 @@ pub struct ThinHeader { size: [u8; mem::size_of::()], } -/// Represents a [Storage] with a known-at-compile-time len for the str. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct ConstStorage { - header: ThinHeader, - data: [u8; N], -} - pub static EMPTY: ConstStorage<0> = ConstStorage::from_str(""); impl Storage { diff --git a/thin-str/src/thin_string.rs b/thin-str/src/thin_string.rs index d02915a3cee..43218a3aa54 100644 --- a/thin-str/src/thin_string.rs +++ b/thin-str/src/thin_string.rs @@ -195,20 +195,19 @@ extern crate std; #[cfg(feature = "std")] mod ext { - + use std::borrow::Cow; use std::string::String; - use super::*; - impl From> for std::borrow::Cow<'static, str> { + impl From> for Cow<'static, str> { fn from(thin_string: ThinString) -> Self { if thin_string.tagged_ptr.tag() == OWNED { - let string = std::string::String::from(thin_string.as_ref()); - std::borrow::Cow::Owned(string) + let string = String::from(thin_string.as_ref()); + Cow::Owned(string) } else { // SAFETY: if the string is borrowed, it lives in static memory. let str = unsafe { mem::transmute::<&str, &str>(thin_string.as_ref()) }; - std::borrow::Cow::Borrowed(str) + Cow::Borrowed(str) } } } @@ -218,6 +217,17 @@ mod ext { ThinString::from_str_in(string.as_str(), Global) } } + + #[cfg(test)] + mod tests { + use super::*; + #[test] + fn test_from_string() { + let string = String::from("hello world"); + let thin_string = ThinString::from(string); + assert_eq!(thin_string.deref(), "hello world"); + } + } } #[cfg(test)] @@ -277,12 +287,4 @@ This is a tribute. let thin_string = ThinString::from(str); assert_eq!(thin_string.deref(), "hello world"); } - - #[cfg(feature = "std")] - #[test] - fn test_from_string() { - let string = std::string::String::from("hello world"); - let thin_string = ThinString::from(string); - assert_eq!(thin_string.deref(), "hello world"); - } }