Skip to content

Commit

Permalink
style: re-organize, refine docs
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed Dec 16, 2024
1 parent 085d216 commit d1102c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
21 changes: 12 additions & 9 deletions thin-str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize> {
/// 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
Expand All @@ -35,14 +46,6 @@ pub struct ThinHeader {
size: [u8; mem::size_of::<usize>()],
}

/// Represents a [Storage] with a known-at-compile-time len for the str.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct ConstStorage<const N: usize> {
header: ThinHeader,
data: [u8; N],
}

pub static EMPTY: ConstStorage<0> = ConstStorage::from_str("");

impl Storage {
Expand Down
30 changes: 16 additions & 14 deletions thin-str/src/thin_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,19 @@ extern crate std;

#[cfg(feature = "std")]
mod ext {

use std::borrow::Cow;
use std::string::String;

use super::*;

impl<A: Allocator + 'static> From<ThinString<A>> for std::borrow::Cow<'static, str> {
impl<A: Allocator + 'static> From<ThinString<A>> for Cow<'static, str> {
fn from(thin_string: ThinString<A>) -> 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)
}
}
}
Expand All @@ -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)]
Expand Down Expand Up @@ -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");
}
}

0 comments on commit d1102c7

Please sign in to comment.