Skip to content

Commit 193d13e

Browse files
authored
docs: Update some naming and README (#404)
* start * Update the name of LastByteUtf8 to LastByte since it represents more than just the possible values of the last byte of a UTF-8 char * Update the README to include more similar crates * cargo fmt
1 parent bf0113a commit 193d13e

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

compact_str/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,17 @@ That being said, uses of unsafe code in this library are constrained to only whe
154154

155155
### Similar Crates
156156
Storing strings on the stack is not a new idea, in fact there are a few other crates in the Rust ecosystem that do similar things, an incomplete list:
157-
1. [`smol_str`](https://crates.io/crates/smol_str) - Can inline 22 bytes, `Clone` is `O(1)`, doesn't adjust for 32-bit archs
158-
2. [`smartstring`](https://crates.io/crates/smartstring) - Can inline 23 bytes, `Clone` is `O(n)`, is mutable
159-
3. [`kstring`](https://crates.io/crates/kstring) - Can inline 15 or 22 bytes dependent on crate features, `Clone` is `O(1)`, can also store `&'static str`s
160-
4. [`flexstr`](https://crates.io/crates/flexstr) - Can inline 22 bytes, `Clone` is `O(1)`, can also store `&'static str`s
157+
158+
* [`arcstr`](https://crates.io/crates/arcstr)
159+
* [`byteyarn`](https://crates.io/crates/byteyarn)
160+
* [`ecow`](https://crates.io/crates/ecow)
161+
* [`flexstr`](https://crates.io/crates/flexstr)
162+
* [`hipstr`](https://crates.io/crates/hipstr)
163+
* [`imstr`](https://crates.io/crates/imstr)
164+
* [`kstring`](https://crates.io/crates/kstring)
165+
* [`smartstring`](https://crates.io/crates/smartstring)
166+
167+
For a comparison of all these crates (and possibly more!) please see the [Rust String Benchmarks](https://github.com/rosetta-rs/string-rosetta-rs).
161168

162169
<br />
163170
Thanks for readingme!

compact_str/src/repr/capacity.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::fmt;
22

3-
use crate::repr::LastUtf8Char;
3+
use crate::repr::LastByte;
44

55
// how many bytes a `usize` occupies
66
const USIZE_SIZE: usize = core::mem::size_of::<usize>();
@@ -15,13 +15,13 @@ const VALID_MASK: usize = {
1515
/// Mask of bits that are set in [`Capacity`] if the string data is stored on the heap.
1616
const HEAP_MARKER: usize = {
1717
let mut bytes = [0; USIZE_SIZE];
18-
bytes[USIZE_SIZE - 1] = LastUtf8Char::Heap as u8;
18+
bytes[USIZE_SIZE - 1] = LastByte::Heap as u8;
1919
usize::from_ne_bytes(bytes)
2020
};
2121

2222
/// State that describes the capacity as being stored on the heap.
2323
///
24-
/// All bytes `255`, with the last being [`LastUtf8Char::Heap`], using the same amount of bytes
24+
/// All bytes `255`, with the last being [`LastByte::Heap`], using the same amount of bytes
2525
/// as `usize`. Example (64-bit): `[255, 255, 255, 255, 255, 255, 255, 216]`
2626
const CAPACITY_IS_ON_THE_HEAP: Capacity = Capacity(VALID_MASK | HEAP_MARKER);
2727

@@ -78,7 +78,7 @@ impl Capacity {
7878
// the heap. return an Error so `BoxString` can do the right thing
7979
CAPACITY_IS_ON_THE_HEAP
8080
} else {
81-
// otherwise, we can store this capacity inline! Set the last byte to be our `LastUtf8Char::Heap as u8`
81+
// otherwise, we can store this capacity inline! Set the last byte to be our `LastByte::Heap as u8`
8282
// for our discriminant, using the leading bytes to store the actual value
8383
Capacity(capacity.to_le() | HEAP_MARKER)
8484
}

compact_str/src/repr/last_utf8_char.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use alloc::string::String;
22

3-
/// [`LastUtf8Char`] is an unsigned 8-bit integer data type that has a valid range of `[0, 216]`.
4-
/// Excluding `[217, 255]` allows the Rust compiler to use these values as niches.
3+
/// [`LastByte`] is an unsigned 8-bit integer data type that has a valid range of `[0, 217]`.
4+
/// Excluding `[218, 255]` allows the Rust compiler to use these values as niches.
55
///
66
/// Specifically the compiler can use a value in this range to encode the `None` variant of
7-
/// `Option<NonMaxU8>` allowing
8-
/// `std::mem::size_of::<NonMaxU8> == std::mem::size_of::<Option<NonMaxU8>>()`
7+
/// `Option<LastByte>` allowing:
8+
/// `std::mem::size_of::<LastByte> == std::mem::size_of::<Option<LastByte>>()`
99
#[allow(dead_code)]
1010
#[derive(Copy, Clone, Debug)]
1111
#[repr(u8)]
12-
pub(crate) enum LastUtf8Char {
12+
pub(crate) enum LastByte {
1313
// single character, ASCII:
1414
V0 = 0,
1515
V1 = 1,
@@ -241,5 +241,5 @@ pub(crate) enum LastUtf8Char {
241241
Static = 217,
242242
}
243243

244-
static_assertions::assert_eq_size!(LastUtf8Char, Option<LastUtf8Char>, u8);
244+
static_assertions::assert_eq_size!(LastByte, Option<LastByte>, u8);
245245
static_assertions::const_assert!(core::mem::size_of::<String>() <= 24);

compact_str/src/repr/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use alloc::string::String;
2525
use capacity::Capacity;
2626
use heap::HeapBuffer;
2727
use inline::InlineBuffer;
28-
use last_utf8_char::LastUtf8Char;
28+
use last_utf8_char::LastByte;
2929
use static_str::StaticStr;
3030
pub(crate) use traits::IntoRepr;
3131

@@ -37,9 +37,9 @@ use crate::{
3737
/// The max size of a string we can fit inline
3838
pub(crate) const MAX_SIZE: usize = core::mem::size_of::<String>();
3939
/// Used as a discriminant to identify different variants
40-
pub(crate) const HEAP_MASK: u8 = LastUtf8Char::Heap as u8;
40+
pub(crate) const HEAP_MASK: u8 = LastByte::Heap as u8;
4141
/// Used for `StaticStr` variant
42-
pub(crate) const STATIC_STR_MASK: u8 = LastUtf8Char::Static as u8;
42+
pub(crate) const STATIC_STR_MASK: u8 = LastByte::Static as u8;
4343
/// When our string is stored inline, we represent the length of the string in the last byte, offset
4444
/// by `LENGTH_MASK`
4545
pub(crate) const LENGTH_MASK: u8 = 0b11000000;
@@ -48,16 +48,18 @@ const EMPTY: Repr = Repr::const_new("");
4848

4949
#[repr(C)]
5050
pub(crate) struct Repr(
51-
// We have a pointer in the representation to properly carry provenance
51+
/// We have a pointer in the representation to properly carry provenance.
5252
*const (),
53-
// Then we need two `usize`s (aka WORDs) of data, for the first we just define a `usize`...
53+
/// Then we need two `usize`s (aka WORDs) of data, for the first we just define a `usize`...
5454
usize,
55-
// ...but the second we breakup into multiple pieces...
56-
#[cfg(target_pointer_width = "64")] u32,
55+
/// ...but the second we breakup into multiple pieces...
56+
#[cfg(target_pointer_width = "64")]
57+
u32,
5758
u16,
5859
u8,
59-
// ...so that the last byte can be a NonMax, which allows the compiler to see a niche value
60-
LastUtf8Char,
60+
/// ...so that the last byte can be a [`LastByte`], which allows the compiler to see a niche
61+
/// value.
62+
LastByte,
6163
);
6264
static_assertions::assert_eq_size!([u8; MAX_SIZE], Repr);
6365

@@ -420,8 +422,8 @@ impl Repr {
420422
pub(crate) fn is_empty(&self) -> bool {
421423
let len_heap = ensure_read(self.1);
422424
let last_byte = self.last_byte() as usize;
423-
let mut len = last_byte.wrapping_sub(LastUtf8Char::L0 as u8 as usize);
424-
if last_byte >= LastUtf8Char::Heap as u8 as usize {
425+
let mut len = last_byte.wrapping_sub(LastByte::L0 as u8 as usize);
426+
if last_byte >= LastByte::Heap as u8 as usize {
425427
len = len_heap;
426428
}
427429
len == 0

0 commit comments

Comments
 (0)