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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#![warn(missing_docs)]
#![no_std]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]
use core::{mem, slice, ptr, cmp, ops, hash, borrow};
#[cfg(feature = "serde")]
mod serde;
pub struct StrBuf<T: Sized> {
inner: mem::MaybeUninit<T>,
cursor: u8,
}
impl<S: Sized> StrBuf<S> {
#[inline]
pub const fn new() -> Self {
Self {
inner: mem::MaybeUninit::uninit(),
cursor: 0,
}
}
#[inline]
pub const unsafe fn from_storage(storage: S, cursor: u8) -> Self {
Self {
inner: mem::MaybeUninit::new(storage),
cursor,
}
}
#[inline]
pub fn from_str(text: &str) -> Self {
debug_assert!(text.len() <= Self::capacity());
let mut result = Self::new();
result.push_str(text);
result
}
#[inline]
pub const fn as_ptr(&self) -> *const u8 {
&self.inner as *const _ as *const u8
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut u8 {
&mut self.inner as *mut _ as *mut u8
}
#[inline]
pub const fn remaining(&self) -> usize {
Self::capacity() - self.cursor as usize
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.as_ptr(), self.cursor as usize)
}
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self.as_mut_ptr(), self.cursor as usize)
}
}
#[inline(always)]
pub fn clear(&mut self) {
unsafe {
self.truncate(0);
}
}
#[inline]
pub unsafe fn truncate(&mut self, cursor: u8) {
if cursor < self.cursor {
self.set_len(cursor);
}
}
#[inline]
pub const fn capacity() -> usize {
mem::size_of::<S>()
}
#[inline]
pub const fn len(&self) -> usize {
self.cursor as usize
}
#[inline(always)]
pub unsafe fn set_len(&mut self, len: u8) {
self.cursor = len
}
#[inline]
pub unsafe fn push_str_unchecked(&mut self, text: &str) {
ptr::copy_nonoverlapping(text.as_ptr(), self.as_mut_ptr().offset(self.cursor as isize), text.len());
self.set_len(self.cursor.saturating_add(text.len() as u8));
}
#[inline]
pub fn push_str(&mut self, text: &str) -> usize {
let size = cmp::min(text.len(), self.remaining());
unsafe {
self.push_str_unchecked(&text[..size]);
}
size
}
#[inline(always)]
pub fn as_str(&self) -> &str {
unsafe {
let slice = core::slice::from_raw_parts(self.as_ptr(), self.len());
core::str::from_utf8_unchecked(slice)
}
}
}
impl<S: Sized> AsRef<str> for StrBuf<S> {
#[inline(always)]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: Sized> core::fmt::Write for StrBuf<S> {
#[inline(always)]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
if self.push_str(s) == s.len() {
Ok(())
} else {
Err(core::fmt::Error)
}
}
}
impl<S: Sized> core::fmt::Display for StrBuf<S> {
#[inline(always)]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
impl<S: Sized> core::fmt::Debug for StrBuf<S> {
#[inline(always)]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
impl<S: Sized> Clone for StrBuf<S> {
#[inline]
fn clone(&self) -> Self {
let mut result = Self::new();
unsafe {
result.push_str_unchecked(self.as_str())
}
result
}
#[inline]
fn clone_from(&mut self, source: &Self) {
self.clear();
unsafe {
self.push_str_unchecked(source.as_str());
}
}
}
impl<S: Sized> AsRef<[u8]> for StrBuf<S> {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl<S: Sized> AsMut<[u8]> for StrBuf<S> {
#[inline(always)]
fn as_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}
impl<S: Sized> borrow::Borrow<str> for StrBuf<S> {
#[inline(always)]
fn borrow(&self) -> &str {
self.as_str()
}
}
impl<S: Sized> ops::Deref for StrBuf<S> {
type Target = str;
#[inline(always)]
fn deref(&self) -> &str {
self.as_str()
}
}
impl<S: Sized> Eq for StrBuf<S> {}
impl<S: Sized> PartialEq<StrBuf<S>> for StrBuf<S> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl<S: Sized> PartialEq<StrBuf<S>> for &str {
#[inline(always)]
fn eq(&self, other: &StrBuf<S>) -> bool {
*self == other.as_str()
}
}
impl<S: Sized> PartialEq<StrBuf<S>> for str {
#[inline(always)]
fn eq(&self, other: &StrBuf<S>) -> bool {
self == other.as_str()
}
}
impl<S: Sized> PartialEq<str> for StrBuf<S> {
#[inline(always)]
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl<S: Sized> PartialEq<&str> for StrBuf<S> {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl<S: Sized> cmp::Ord for StrBuf<S> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
impl<S: Sized> PartialOrd for StrBuf<S> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<S: Sized> hash::Hash for StrBuf<S> {
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
self.as_str().hash(hasher)
}
}
impl<S: Sized> From<&str> for StrBuf<S> {
#[inline(always)]
fn from(text: &str) -> Self {
Self::from_str(text)
}
}