Skip to content

Commit bfa73c8

Browse files
committed
simplify endianness management
1 parent a91389c commit bfa73c8

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ColdString is an 8 byte array (4 bytes on 32-bit machines):
3434
```rust,ignore
3535
pub struct ColdString([u8; 8]);
3636
```
37-
The array acts as either a pointer to heap data for strings longer than 7 bytes or is the inlined data itself. Below assumes 64-bit, little endian machine.
37+
The array acts as either a pointer to heap data for strings longer than 7 bytes or is the inlined data itself.
3838
## Inline Mode
3939
`self.0[1]` to `self.0[7]` store the bytes of string. In the least significant byte, `self.0[0]`, the least significant bit signifies the inline/heap flag, and is set to "1" for inline mode. The next bits encode the length (always between 0 and 7).
4040
```ignore

src/lib.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ const WIDTH: usize = mem::size_of::<usize>();
2727
pub struct ColdString([u8; WIDTH]);
2828

2929
impl ColdString {
30-
const LSB_INDEX: usize = if cfg!(target_endian = "little") {
31-
0
32-
} else {
33-
WIDTH - 1
34-
};
35-
const DATA_START: usize = if cfg!(target_endian = "little") { 1 } else { 0 };
36-
3730
pub fn new(s: &str) -> Self {
3831
if s.len() < WIDTH {
3932
Self::new_inline(s)
@@ -44,18 +37,18 @@ impl ColdString {
4437

4538
#[inline]
4639
fn is_inline(&self) -> bool {
47-
self.0[Self::LSB_INDEX] & 1 == 1
40+
self.0[0] & 1 == 1
4841
}
4942

5043
#[inline]
5144
fn new_inline(s: &str) -> Self {
5245
debug_assert!(s.len() < WIDTH);
5346
let mut buf = [0u8; WIDTH];
5447
unsafe {
55-
let dest_ptr = buf.as_mut_ptr().add(Self::DATA_START);
48+
let dest_ptr = buf.as_mut_ptr().add(1);
5649
ptr::copy_nonoverlapping(s.as_ptr(), dest_ptr, s.len());
5750
}
58-
buf[Self::LSB_INDEX] = ((s.len() as u8) << 1) | 1;
51+
buf[0] = ((s.len() as u8) << 1) | 1;
5952
Self(buf)
6053
}
6154

@@ -79,21 +72,21 @@ impl ColdString {
7972

8073
let addr = ptr.expose_provenance();
8174
debug_assert!(addr % 2 == 0);
82-
Self(addr.to_ne_bytes())
75+
Self(addr.to_le_bytes())
8376
}
8477
}
8578

8679
#[inline]
8780
fn heap_ptr(&self) -> *mut u8 {
8881
debug_assert!(!self.is_inline());
89-
let addr = usize::from_ne_bytes(self.0);
82+
let addr = usize::from_le_bytes(self.0);
9083
debug_assert!(addr % 2 == 0);
9184
with_exposed_provenance_mut::<u8>(addr)
9285
}
9386

9487
#[inline]
9588
fn inline_len(&self) -> usize {
96-
self.0[Self::LSB_INDEX] as usize >> 1
89+
self.0[0] as usize >> 1
9790
}
9891

9992
#[inline]
@@ -113,7 +106,7 @@ impl ColdString {
113106
#[inline]
114107
unsafe fn decode_inline(&self) -> &[u8] {
115108
let len = self.inline_len();
116-
let ptr = self.0.as_ptr().add(Self::DATA_START);
109+
let ptr = self.0.as_ptr().add(1);
117110
slice::from_raw_parts(ptr, len)
118111
}
119112

0 commit comments

Comments
 (0)