| 1 |
use core::fmt; |
| 2 |
|
| 3 |
use crate::repr::LastByte; |
| 4 |
|
| 5 |
// how many bytes a `usize` occupies |
| 6 |
const USIZE_SIZE: usize = core::mem::size_of::<usize>(); |
| 7 |
|
| 8 |
/// Mask of bits in [`Capacity`] that encode the value. |
| 9 |
const VALID_MASK: usize = { |
| 10 |
let mut bytes = [255; USIZE_SIZE]; |
| 11 |
bytes[USIZE_SIZE - 1] = 0; |
| 12 |
usize::from_ne_bytes(bytes) |
| 13 |
}; |
| 14 |
|
| 15 |
/// Mask of bits that are set in [`Capacity`] if the string data is stored on the heap. |
| 16 |
const HEAP_MARKER: usize = { |
| 17 |
let mut bytes = [0; USIZE_SIZE]; |
| 18 |
bytes[USIZE_SIZE - 1] = LastByte::Heap as u8; |
| 19 |
usize::from_ne_bytes(bytes) |
| 20 |
}; |
| 21 |
|
| 22 |
/// State that describes the capacity as being stored on the heap. |
| 23 |
/// |
| 24 |
/// All bytes `255`, with the last being [`LastByte::Heap`], using the same amount of bytes |
| 25 |
/// as `usize`. Example (64-bit): `[255, 255, 255, 255, 255, 255, 255, 216]` |
| 26 |
#[cfg(not(target_pointer_width = "64"))] |
| 27 |
const CAPACITY_IS_ON_THE_HEAP: Capacity = Capacity(VALID_MASK | HEAP_MARKER); |
| 28 |
|
| 29 |
/// The maximum value we're able to store, e.g. on 64-bit arch this is 2^56 - 2. |
| 30 |
pub(crate) const MAX_VALUE: usize = { |
| 31 |
let mut bytes = [255; USIZE_SIZE]; |
| 32 |
bytes[USIZE_SIZE - 1] = 0; |
| 33 |
usize::from_le_bytes(bytes) - 1 |
| 34 |
}; |
| 35 |
|
| 36 |
/// An integer type that uses `core::mem::size_of::<usize>() - 1` bytes to store the capacity of |
| 37 |
/// a heap buffer. |
| 38 |
/// |
| 39 |
/// Assuming a 64-bit arch, a [`super::BoxString`] uses 8 bytes for a pointer, 8 bytes for a |
| 40 |
/// length, and then needs 1 byte for a discriminant. We need to store the capacity somewhere, and |
| 41 |
/// we could store it on the heap, but we also have 7 unused bytes. [`Capacity`] handles storing a |
| 42 |
/// value in these 7 bytes, returning an error if it's not possible, at which point we'll store the |
| 43 |
/// capacity on the heap. |
| 44 |
/// |
| 45 |
/// # Max Values |
| 46 |
/// * __64-bit:__ `(2 ^ (7 * 8)) - 2 = 72_057_594_037_927_934 ~= 64 petabytes` |
| 47 |
/// * __32-bit:__ `(2 ^ (3 * 8)) - 2 = 16_777_214 ~= 16 megabytes` |
| 48 |
/// |
| 49 |
/// Practically speaking, on a 64-bit architecture we'll never need to store the capacity on the |
| 50 |
/// heap, because with it's impossible to create a string that is 64 petabytes or larger. But for |
| 51 |
/// 32-bit architectures we need to be able to store a capacity larger than 16 megabytes, since a |
| 52 |
/// string larger than 16 megabytes probably isn't that uncommon. |
| 53 |
#[derive(Copy, Clone, PartialEq, Eq)] |
| 54 |
#[repr(transparent)] |
| 55 |
pub(crate) struct Capacity(usize); |
| 56 |
|
| 57 |
static_assertions::assert_eq_size!(Capacity, usize); |
| 58 |
static_assertions::assert_eq_align!(Capacity, usize); |
| 59 |
|
| 60 |
impl fmt::Debug for Capacity { |
| 61 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 62 |
write!(f, "Capacity(0x{:x})", usize::from_le(self.0)) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl Capacity { |
| 67 |
#[inline] |
| 68 |
pub(crate) const fn new(capacity: usize) -> Self { |
| 69 |
cfg_if::cfg_if! { |
| 70 |
if #[cfg(target_pointer_width = "64")] { |
| 71 |
// on 64-bit arches we can always fit the capacity inline |
| 72 |
debug_assert!(capacity <= MAX_VALUE); |
| 73 |
// SAFETY: a 64-bit capacity is `< 2^56`, so its `to_le()` form leaves the |
| 74 |
// discriminant byte free for `HEAP_MARKER`, letting the compiler fold the dead |
| 75 |
// `Err` arm at callers like `From<String>`. (`to_le()`, not the raw value: |
| 76 |
// `VALID_MASK` is a byte-layout mask, so the raw check breaks on big-endian.) |
| 77 |
unsafe { super::assume(capacity.to_le() & !VALID_MASK == 0) }; |
| 78 |
|
| 79 |
Capacity(capacity.to_le() | HEAP_MARKER) |
| 80 |
} else if #[cfg(target_pointer_width = "32")] { |
| 81 |
// on 32-bit arches we might need to store the capacity on the heap |
| 82 |
if capacity > MAX_VALUE { |
| 83 |
// if we need the last byte to encode this capacity then we need to put the capacity on |
| 84 |
// the heap. return an Error so `BoxString` can do the right thing |
| 85 |
CAPACITY_IS_ON_THE_HEAP |
| 86 |
} else { |
| 87 |
// otherwise, we can store this capacity inline! Set the last byte to be our `LastByte::Heap as u8` |
| 88 |
// for our discriminant, using the leading bytes to store the actual value |
| 89 |
Capacity(capacity.to_le() | HEAP_MARKER) |
| 90 |
} |
| 91 |
} else { |
| 92 |
compile_error!("Unsupported target_pointer_width"); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// Re-interprets a [`Capacity`] as a `usize` |
| 98 |
/// |
| 99 |
/// # SAFETY: |
| 100 |
/// * `self` must be less than or equal to [`MAX_VALUE`] |
| 101 |
#[inline(always)] |
| 102 |
pub(crate) unsafe fn as_usize(self) -> usize { |
| 103 |
usize::from_le(self.0 & VALID_MASK) |
| 104 |
} |
| 105 |
|
| 106 |
/// Returns whether or not this [`Capacity`] has a value that indicates the capacity is being |
| 107 |
/// stored on the heap |
| 108 |
#[inline(always)] |
| 109 |
pub(crate) fn is_heap(self) -> bool { |
| 110 |
cfg_if::cfg_if! { |
| 111 |
if #[cfg(target_pointer_width = "64")] { |
| 112 |
false |
| 113 |
} else { |
| 114 |
self == CAPACITY_IS_ON_THE_HEAP |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
mod tests { |
| 122 |
use super::Capacity; |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_zero_roundtrips() { |
| 126 |
let og = 0; |
| 127 |
let cap = Capacity::new(og); |
| 128 |
let after = unsafe { cap.as_usize() }; |
| 129 |
|
| 130 |
assert_eq!(og, after); |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn test_max_value() { |
| 135 |
let available_bytes = (core::mem::size_of::<usize>() - 1) as u32; |
| 136 |
let max_value = 2usize.pow(available_bytes * 8) - 2; |
| 137 |
|
| 138 |
#[cfg(target_pointer_width = "64")] |
| 139 |
assert_eq!(max_value, 72057594037927934); |
| 140 |
#[cfg(target_pointer_width = "32")] |
| 141 |
assert_eq!(max_value, 16777214); |
| 142 |
|
| 143 |
let cap = Capacity::new(max_value); |
| 144 |
let after = unsafe { cap.as_usize() }; |
| 145 |
|
| 146 |
assert_eq!(max_value, after); |
| 147 |
} |
| 148 |
|
| 149 |
#[cfg(target_pointer_width = "32")] |
| 150 |
#[test] |
| 151 |
fn test_invalid_value() { |
| 152 |
let invalid_val = usize::MAX; |
| 153 |
let cap = Capacity::new(invalid_val); |
| 154 |
let after = unsafe { cap.as_usize() }; |
| 155 |
|
| 156 |
// anything greater than or equal to 16777215, should "resolve" to 16777215 |
| 157 |
assert_eq!(16777215, after); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
#[cfg_attr(miri, ignore)] |
| 162 |
fn test_all_valid_32bit_values() { |
| 163 |
#[cfg(target_pointer_width = "32")] |
| 164 |
assert_eq!(16_777_214, super::MAX_VALUE); |
| 165 |
|
| 166 |
for i in 0..=16_777_214 { |
| 167 |
let cap = Capacity::new(i); |
| 168 |
let val = unsafe { cap.as_usize() }; |
| 169 |
|
| 170 |
assert_eq!(val, i, "value roundtriped to wrong value?"); |
| 171 |
} |
| 172 |
} |
| 173 |
} |