Skip to content

Commit d1102c7

Browse files
committed
style: re-organize, refine docs
1 parent 085d216 commit d1102c7

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

thin-str/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@ pub use crate::thin_string::*;
1717
/// mutable references.
1818
#[repr(C)]
1919
pub struct Storage {
20+
/// The header stores the number of bytes used in the string.
2021
header: ThinHeader,
21-
/// The bytes of the strings are stored here. They need to be a valid str.
22+
/// The bytes of the strings are stored here.
2223
data: str,
2324
}
2425

26+
/// Represents a [Storage] with a known-at-compile-time len for the str.
27+
#[repr(C)]
28+
#[derive(Clone, Copy)]
29+
pub struct ConstStorage<const N: usize> {
30+
/// The header stores the number of bytes used in the string.
31+
header: ThinHeader,
32+
/// The bytes of the string are stored here, it must be a valid str.
33+
data: [u8; N],
34+
}
35+
2536
/// The alignment is so that tagged pointers can use the least significant bit
2637
/// for storing other things if they wish. However, it's intentionally minimal
2738
/// so that strings can be packed tightly into arenas. Wasting a single
@@ -35,14 +46,6 @@ pub struct ThinHeader {
3546
size: [u8; mem::size_of::<usize>()],
3647
}
3748

38-
/// Represents a [Storage] with a known-at-compile-time len for the str.
39-
#[repr(C)]
40-
#[derive(Clone, Copy)]
41-
pub struct ConstStorage<const N: usize> {
42-
header: ThinHeader,
43-
data: [u8; N],
44-
}
45-
4649
pub static EMPTY: ConstStorage<0> = ConstStorage::from_str("");
4750

4851
impl Storage {

thin-str/src/thin_string.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,19 @@ extern crate std;
195195

196196
#[cfg(feature = "std")]
197197
mod ext {
198-
198+
use std::borrow::Cow;
199199
use std::string::String;
200-
201200
use super::*;
202201

203-
impl<A: Allocator + 'static> From<ThinString<A>> for std::borrow::Cow<'static, str> {
202+
impl<A: Allocator + 'static> From<ThinString<A>> for Cow<'static, str> {
204203
fn from(thin_string: ThinString<A>) -> Self {
205204
if thin_string.tagged_ptr.tag() == OWNED {
206-
let string = std::string::String::from(thin_string.as_ref());
207-
std::borrow::Cow::Owned(string)
205+
let string = String::from(thin_string.as_ref());
206+
Cow::Owned(string)
208207
} else {
209208
// SAFETY: if the string is borrowed, it lives in static memory.
210209
let str = unsafe { mem::transmute::<&str, &str>(thin_string.as_ref()) };
211-
std::borrow::Cow::Borrowed(str)
210+
Cow::Borrowed(str)
212211
}
213212
}
214213
}
@@ -218,6 +217,17 @@ mod ext {
218217
ThinString::from_str_in(string.as_str(), Global)
219218
}
220219
}
220+
221+
#[cfg(test)]
222+
mod tests {
223+
use super::*;
224+
#[test]
225+
fn test_from_string() {
226+
let string = String::from("hello world");
227+
let thin_string = ThinString::from(string);
228+
assert_eq!(thin_string.deref(), "hello world");
229+
}
230+
}
221231
}
222232

223233
#[cfg(test)]
@@ -277,12 +287,4 @@ This is a tribute.
277287
let thin_string = ThinString::from(str);
278288
assert_eq!(thin_string.deref(), "hello world");
279289
}
280-
281-
#[cfg(feature = "std")]
282-
#[test]
283-
fn test_from_string() {
284-
let string = std::string::String::from("hello world");
285-
let thin_string = ThinString::from(string);
286-
assert_eq!(thin_string.deref(), "hello world");
287-
}
288290
}

0 commit comments

Comments
 (0)