wiwi/
string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::prelude_std::*;

use crate::num::*;

const STRING_SIZE_BYTES: usize = size_of::<usize>() * 3;
const MAX_INLINE_LEN: usize = STRING_SIZE_BYTES - 1;
const CAP_MARKER_BE: usize = (!(usize::MAX >> 1)).to_be();
const CAP_MARKER_U8: u8 = !(u8::MAX >> 1);

const _: () = assert!(size_of::<StringInlineable>() == STRING_SIZE_BYTES);
const _: () = assert!(size_of::<StringInline>() == STRING_SIZE_BYTES);
const _: () = assert!(size_of::<StringHeap>() == STRING_SIZE_BYTES);
const _: () = assert!(MAX_INLINE_LEN > 0);

pub union StringInlineable {
	inline: ManuallyDrop<StringInline>,
	heap: ManuallyDrop<StringHeap>
}

impl StringInlineable {
	#[inline]
	pub const fn new() -> Self {
		let inline = StringInline::new();
		Self { inline: ManuallyDrop::new(inline) }
	}
}

impl StringInlineable {
	#[inline]
	pub fn len(&self) -> usize {
		self.do_thing(|s| s.len(), |s| s.len())
	}

	#[inline]
	pub fn capacity(&self) -> usize {
		self.do_thing(|s| s.capacity(), |s| s.capacity())
	}

	#[inline]
	pub fn is_empty(&self) -> bool {
		self.len() == 0
	}

	#[inline]
	pub fn as_str(&self) -> &str {
		self.do_thing(|s| s.as_str(), |s| s.as_str())
	}

	#[inline]
	pub fn as_str_mut(&mut self) -> &mut str {
		self.do_thing_mut(|s| s.as_str_mut(), |s| s.as_str_mut())
	}
}

impl StringInlineable {
	#[inline]
	fn is_inline(&self) -> bool {
		// SAFETY: all memory-valid instancees of `StringHeap` satisfy memory
		// invariants of `StringInline`, so union field access `self.inline` is fine
		let len = unsafe { self.inline.len };

		len & CAP_MARKER_U8 == 0
	}

	#[inline]
	fn do_thing<'h, T, FInline, FHeap>(&'h self, f_inline: FInline, f_heap: FHeap) -> T
	where
		FInline: FnOnce(&'h StringInline) -> T,
		FHeap: FnOnce(&'h StringHeap) -> T
	{
		match self.is_inline() {
			// SAFETY: we just checked `self.is_inline()`
			true => unsafe { f_inline(&self.inline) }
			// SAFETY: we just checked `self.is_inline()`
			false => unsafe { f_heap(&self.heap) }
		}
	}

	#[inline]
	fn do_thing_mut<'h, T, FInline, FHeap>(&'h mut self, f_inline: FInline, f_heap: FHeap) -> T
	where
		FInline: FnOnce(&'h mut StringInline) -> T,
		FHeap: FnOnce(&'h mut StringHeap) -> T
	{
		match self.is_inline() {
			// SAFETY: we just checked `self.is_inline()`
			true => unsafe { f_inline(&mut self.inline) }
			// SAFETY: we just checked `self.is_inline()`
			false => unsafe { f_heap(&mut self.heap) }
		}
	}
}

impl Default for StringInlineable {
	#[inline]
	fn default() -> Self {
		Self::new()
	}
}

impl Deref for StringInlineable {
	type Target = str;

	#[inline]
	fn deref(&self) -> &str {
		self.as_str()
	}
}

impl From<&str> for StringInlineable {
	#[inline]
	fn from(s: &str) -> Self {
		match s.len() <= MAX_INLINE_LEN {
			true => {
				// SAFETY: just checked `s.len() <= MAX_INLINE_LEN`
				let inline = unsafe { StringInline::from_str_unchecked(s) };
				Self { inline: ManuallyDrop::new(inline) }
			}
			false => {
				// SAFETY: just checked `s.len() > MAX_INLINE_LEN`
				// (which is also not zero)
				let heap = unsafe { StringHeap::from_str_unchecked(s) };
				Self { heap: ManuallyDrop::new(heap) }
			}
		}
	}
}

#[repr(C)]
struct StringInline {
	/// regular u8, represented as is
	len: u8,
	rest: MaybeUninit<[u8; MAX_INLINE_LEN]>
}

impl StringInline {
	#[inline]
	const fn new() -> Self {
		Self { len: 0, rest: MaybeUninit::uninit() }
	}

	/// # Safety
	///
	/// The passed in `str` must have length less than or equal to [`MAX_INLINE_LEN`].
	#[inline]
	unsafe fn from_str_unchecked(s: &str) -> Self {
		debug_assert!(s.len() <= MAX_INLINE_LEN);

		let mut inline = Self {
			len: 0,
			rest: MaybeUninit::uninit()
		};

		// SAFETY:
		// - ptr obtained from `value` is valid, and for `s.len()` reads
		// - ptr obtained from `inline.rest` is valid
		// - caller promises `s.len()` is lte `MAX_INLINE_LEN`
		// - ptrs obtained from aligned sources
		// - reference to memory outside local stack memory in `value`
		//   cannot overlap with local stack memory in `inline`
		unsafe {
			ptr::copy_nonoverlapping(
				s.as_ptr(),
				inline.rest.as_mut_ptr().cast::<u8>(),
				s.len()
			)
		}

		// we just initialised `s.len()` amount of
		// memory with that `copy` call above
		inline.len = s.len().into_u8_lossy();

		inline
	}
}

impl StringInline {
	#[inline]
	fn len(&self) -> usize {
		usize::from_u8(self.len)
	}

	#[inline]
	fn capacity(&self) -> usize {
		MAX_INLINE_LEN
	}

	#[inline]
	fn as_str(&self) -> &str {
		let ptr = self.rest.as_ptr().cast::<u8>();
		let len = self.len.into_usize();

		// SAFETY: relying on invariant that `self.rest` must have
		// at least `self.len` elements initialised
		let slice = unsafe { slice::from_raw_parts(ptr, len) };
		// SAFETY: relying on invariant that `self` contains valid utf-8
		unsafe { str::from_utf8_unchecked(slice) }
	}

	#[inline]
	fn as_str_mut(&mut self) -> &mut str {
		let ptr = self.rest.as_ptr().cast::<u8>();
		let len = self.len.into_usize();

		// SAFETY: relying on invariant that `self.rest` must have
		// at least `self.len` elements initialised
		let slice = unsafe { slice::from_raw_parts_mut(ptr.cast_mut(), len) };
		// SAFETY: relying on invariant that `self` contains valid utf-8
		unsafe { str::from_utf8_unchecked_mut(slice) }
	}
}

#[repr(C)]
struct StringHeap {
	/// This value needs processing in order to be a valid capacity
	///
	/// This stores the capacity, in big endian, with the highest bit set. Just
	/// use [`capacity`](Self::capacity) function to get the capacity.
	cap_be_and_marker: usize,
	len: usize,
	ptr: *const u8
}

impl StringHeap {
	/// # Safety
	///
	/// The passed in `str` must have length greater than zero. (The passed in
	/// `str` _should_ have greater than `MAX_INLINE_LEN` len, which is larger
	/// than zero, and `StringInlineable` already ensures this)
	unsafe fn from_str_unchecked(s: &str) -> Self {
		let layout = alloc_mod::Layout::for_value(s);
		// SAFETY: layout is nonzero (caller promises `s` is not zero length)
		let ptr = unsafe { alloc(layout) };

		let mut heap = Self {
			cap_be_and_marker: 0,
			len: 0,
			ptr
		};
		// SAFETY:
		// - we just allocated the ptr inside with this layout
		// - existing `&str` cannot have memory larger than `isize::MAX`
		unsafe { heap.set_capacity(layout.size()) }

		// SAFETY:
		// - ptr obtained from `value` is valid, and for `s.len()` reads
		// - we just allocated ptr in `heap.ptr` for `s.len()` bytes
		// - ptrs obtained from aligned sources
		// - reference to existing memory in `value` cannot overlap with
		//   memory we just allocated
		unsafe {
			ptr::copy_nonoverlapping(
				s.as_ptr(),
				heap.ptr.cast_mut(),
				s.len()
			)
		}

		// we just initialised `s.len()` amount of
		// memory with that `copy` call above
		heap.len = s.len();

		heap
	}
}

impl StringHeap {
	#[inline]
	fn len(&self) -> usize {
		self.len
	}

	#[inline]
	fn capacity(&self) -> usize {
		usize::from_be(self.cap_be_and_marker ^ CAP_MARKER_BE)
	}

	/// Helper for setting capacity (since it's stored in a... nonstandard way)
	///
	/// # Safety
	///
	/// - Capacity in `self` must actually be `capacity`
	/// - `capacity` must be less than or equal to `isize::MAX`. This is
	///   required by rust's allocation APIs, as well as needed for the heap marker
	///   to be set properlu
	#[inline]
	unsafe fn set_capacity(&mut self, capacity: usize) {
		self.cap_be_and_marker = capacity.to_be() ^ CAP_MARKER_BE
	}

	#[inline]
	fn as_str(&self) -> &str {
		// SAFETY: relying on invariant that `self.rest` must have
		// at least `self.len` elements initialised
		let slice = unsafe { slice::from_raw_parts(self.ptr, self.len) };
		// SAFETY: relying on invariant that `self` contains valid utf-8
		unsafe { str::from_utf8_unchecked(slice) }
	}

	#[inline]
	fn as_str_mut(&mut self) -> &mut str {
		// SAFETY: relying on invariant that `self.rest` must have
		// at least `self.len` elements initialised
		let slice = unsafe { slice::from_raw_parts_mut(self.ptr.cast_mut(), self.len) };
		// SAFETY: relying on invariant that `self` contains valid utf-8
		unsafe { str::from_utf8_unchecked_mut(slice) }
	}
}