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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use core::{borrow::Borrow, ops::RangeBounds, str};
use super::{
BytesMut,
error::*,
utf8_buf::{Utf8Buf, Utf8BufMut},
};
use bytes::BufMut;
mod cmp;
mod fmt;
mod from;
mod ops;
#[cfg(feature = "arbitrary")]
mod arbitrary;
#[cfg(feature = "borsh")]
mod borsh;
#[cfg(feature = "quickcheck")]
mod quickcheck;
#[cfg(feature = "serde")]
mod serde;
#[cfg(feature = "pyo3")]
mod python;
#[cfg(feature = "wasm")]
mod wasm;
#[doc = "Growable mutable UTF-8 string with inline/heap storage."]
#[doc = ""]
#[doc = "Guarantees valid UTF-8. Split operations check char boundaries."]
#[cfg_attr(not(feature = "wasm"), doc = "")]
#[cfg_attr(not(feature = "wasm"), doc = "# Examples")]
#[cfg_attr(not(feature = "wasm"), doc = "")]
#[cfg_attr(not(feature = "wasm"), doc = "```")]
#[cfg_attr(not(feature = "wasm"), doc = "use smol_bytes::Utf8BytesMut;")]
#[cfg_attr(not(feature = "wasm"), doc = "")]
#[cfg_attr(not(feature = "wasm"), doc = "let mut buf = Utf8BytesMut::new();")]
#[cfg_attr(not(feature = "wasm"), doc = "buf.push_str(\"Hello\");")]
#[cfg_attr(not(feature = "wasm"), doc = "buf.push(' ');")]
#[cfg_attr(not(feature = "wasm"), doc = "buf.push_str(\"world!\");")]
#[cfg_attr(
not(feature = "wasm"),
doc = "assert_eq!(buf.as_str(), \"Hello world!\");"
)]
#[cfg_attr(not(feature = "wasm"), doc = "```")]
#[cfg_attr(feature = "wasm", doc = "")]
#[cfg_attr(feature = "wasm", doc = "@example")]
#[cfg_attr(feature = "wasm", doc = "```typescript")]
#[cfg_attr(feature = "wasm", doc = "import { Utf8BytesMut } from 'smol-bytes';")]
#[cfg_attr(feature = "wasm", doc = "const buf = new Utf8BytesMut();")]
#[cfg_attr(feature = "wasm", doc = "buf.pushStr('hello world');")]
#[cfg_attr(
feature = "wasm",
doc = "console.log(buf.toString()); // 'hello world'"
)]
#[cfg_attr(feature = "wasm", doc = "```")]
#[derive(Clone)]
#[cfg_attr(feature = "pyo3", ::pyo3::prelude::pyclass(from_py_object))]
#[cfg_attr(feature = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub struct Utf8BytesMut {
pub(crate) inner: BytesMut,
}
impl Default for Utf8BytesMut {
fn default() -> Self {
Self::new()
}
}
impl core::fmt::Write for Utf8BytesMut {
#[inline(always)]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.push_str(s);
Ok(())
}
}
impl Utf8BytesMut {
/// Creates a new, empty `Utf8BytesMut`.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// const EMPTY: Utf8BytesMut = Utf8BytesMut::new();
/// assert!(EMPTY.is_empty());
/// ```
#[inline(always)]
pub const fn new() -> Self {
Self {
inner: BytesMut::new(),
}
}
/// Creates a new `Utf8BytesMut` with the specified capacity.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let buf = Utf8BytesMut::with_capacity(100);
/// assert!(buf.capacity() >= 100);
/// ```
pub fn with_capacity(capacity: usize) -> Self {
Self {
inner: BytesMut::with_capacity(capacity),
}
}
/// Returns a reference to the inner `BytesMut`.
///
/// Note: named `as_inner` to avoid confusion with `str::as_bytes`/
/// `String::as_bytes_mut`, which users might expect to return `&[u8]`.
#[inline(always)]
pub const fn as_inner(&self) -> &BytesMut {
&self.inner
}
/// Consumes `self` and returns the inner `BytesMut`.
#[inline(always)]
pub fn into_inner(self) -> BytesMut {
self.inner
}
/// Returns the contents as a string slice.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let buf = Utf8BytesMut::from("hello");
/// assert_eq!(buf.as_str(), "hello");
/// ```
#[inline(always)]
pub fn as_str(&self) -> &str {
// SAFETY: Utf8BytesMut guarantees valid UTF-8
unsafe { str::from_utf8_unchecked(self.inner.as_ref()) }
}
/// Returns the length in bytes.
#[inline(always)]
pub fn len(&self) -> usize {
self.inner.len()
}
/// Returns `true` if the buffer has length 0.
#[cfg_attr(not(coverage), inline(always))]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Returns the number of bytes the buffer can hold without reallocating.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let buf = Utf8BytesMut::with_capacity(128);
/// assert!(buf.capacity() >= 128);
/// ```
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
/// Returns `true` if the underlying bytes are stored inline (≤ 62 bytes).
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let small = Utf8BytesMut::from("hi");
/// assert!(small.is_inline());
/// ```
#[inline]
pub fn is_inline(&self) -> bool {
self.inner.is_inline()
}
/// Returns `true` if the underlying bytes are heap-allocated.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let big = Utf8BytesMut::with_capacity(128);
/// assert!(big.is_heap());
/// ```
#[inline]
pub fn is_heap(&self) -> bool {
self.inner.is_heap()
}
/// Appends a character to the buffer.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::new();
/// buf.push('a');
/// buf.push('b');
/// assert_eq!(buf.as_str(), "ab");
/// ```
pub fn push(&mut self, ch: char) {
<Self as Utf8BufMut>::push(self, ch)
}
/// Appends a string slice to the buffer.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::new();
/// buf.push_str("hello");
/// assert_eq!(buf.as_str(), "hello");
/// ```
pub fn push_str(&mut self, s: &str) {
<Self as Utf8BufMut>::push_str(self, s)
}
/// Clears the buffer, removing all contents.
pub fn clear(&mut self) {
<Self as Utf8BufMut>::clear(self)
}
/// Truncates the buffer to the specified length.
///
/// # Panics
///
/// Panics if `new_len` does not lie on a UTF-8 character boundary.
pub fn truncate(&mut self, new_len: usize) {
self
.try_truncate(new_len)
.expect("new_len must lie on a UTF-8 character boundary");
}
/// Truncates the buffer at a UTF-8 character boundary.
pub fn try_truncate(&mut self, new_len: usize) -> Result<(), Utf8Error> {
<Self as Utf8BufMut>::try_truncate(self, new_len)
}
/// Freezes this mutable value using the shared immutable strategy.
pub fn freeze_shared(self) -> crate::shared::Utf8Bytes {
crate::utf8_bytes::Utf8Bytes {
inner: self.inner.freeze_shared(),
}
}
/// Freezes this mutable value using the compact immutable strategy.
pub fn freeze_compact(self) -> crate::compact::Utf8Bytes {
crate::utf8_bytes::Utf8Bytes {
inner: self.inner.freeze_compact(),
}
}
/// Reserves capacity for at least `additional` more bytes.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::new();
/// buf.reserve(100);
/// assert!(buf.capacity() >= 100);
/// ```
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional);
}
/// Splits the buffer at the given index.
///
/// # Panics
///
/// Panics if `at` is not on a character boundary or is out of bounds.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::from("hello world");
/// let head = buf.split_to(5);
///
/// assert_eq!(head.as_str(), "hello");
/// assert_eq!(buf.as_str(), " world");
/// ```
pub fn split_to(&mut self, at: usize) -> Self {
self.try_split_to(at).expect("split_to failed")
}
/// Tries to split the buffer at the given index.
///
/// # Errors
///
/// Returns an error if `at` is out of bounds or not on a character boundary.
pub fn try_split_to(&mut self, at: usize) -> Result<Self, Utf8Error> {
<Self as Utf8Buf>::try_split_to(self, at)
}
/// Splits the buffer at the given index, returning the tail.
///
/// # Panics
///
/// Panics if `at` is not on a character boundary or is out of bounds.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::from("hello world");
/// let tail = buf.split_off(6);
///
/// assert_eq!(buf.as_str(), "hello ");
/// assert_eq!(tail.as_str(), "world");
/// ```
pub fn split_off(&mut self, at: usize) -> Self {
self.try_split_off(at).expect("split_off failed")
}
/// Tries to split the buffer at the given index, returning the tail.
///
/// # Errors
///
/// Returns an error if `at` is out of bounds or not on a character boundary.
pub fn try_split_off(&mut self, at: usize) -> Result<Self, Utf8Error> {
<Self as Utf8Buf>::try_split_off(self, at)
}
/// Removes all bytes from the buffer and returns them in a new buffer.
///
/// Afterwards, `self` will be empty, retaining any additional capacity
/// it had before the split (see [`bytes::BytesMut::split`]).
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::with_capacity(128);
/// buf.push_str("hello");
/// let data = buf.split();
///
/// assert_eq!(data.as_str(), "hello");
/// assert_eq!(buf.len(), 0);
/// ```
pub fn split(&mut self) -> Self {
let inner = self
.inner
.try_split()
.expect("split always succeeds")
.unwrap_or_else(BytesMut::from);
Self { inner }
}
/// Attempts to merge another buffer back into this one.
///
/// Returns `None` on success. Returns `Some(other)` when the buffers
/// cannot be efficiently merged — in particular, **both buffers must be
/// heap-allocated** (see [`BytesMut::unsplit`]). If either side is
/// inline, `unsplit` leaves `self` unchanged and hands `other` back.
///
/// # Examples
///
/// Heap buffers merge successfully:
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::with_capacity(128);
/// buf.push_str("hello world");
/// let tail = buf.split_off(5);
/// assert!(buf.unsplit(tail).is_none());
/// assert_eq!(buf.as_str(), "hello world");
/// ```
///
/// Inline buffers fail to merge:
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let mut buf = Utf8BytesMut::from("hi"); // inline
/// let tail = Utf8BytesMut::from("!");
/// assert!(buf.unsplit(tail).is_some()); // returned unchanged
/// ```
pub fn unsplit(&mut self, other: Self) -> Option<Self> {
self.inner.unsplit(other.inner).map(|inner| Self { inner })
}
/// Returns a slice of this buffer.
///
/// # Panics
///
/// Panics if the range is not on character boundaries or is out of bounds.
///
/// # Examples
///
/// ```
/// use smol_bytes::Utf8BytesMut;
///
/// let buf = Utf8BytesMut::from("hello world");
/// let slice = buf.slice(0..5);
///
/// assert_eq!(slice.as_str(), "hello");
/// ```
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
self.try_slice(range).expect("slice failed")
}
/// Tries to return a slice of this buffer.
///
/// # Errors
///
/// Returns an error if the range is out of bounds or not on character boundaries.
pub fn try_slice(&self, range: impl RangeBounds<usize>) -> Result<Self, Utf8Error> {
<Self as Utf8Buf>::try_slice(self, range)
}
}
impl AsRef<str> for Utf8BytesMut {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<[u8]> for Utf8BytesMut {
fn as_ref(&self) -> &[u8] {
self.as_str().as_bytes()
}
}
impl Borrow<str> for Utf8BytesMut {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl core::ops::Deref for Utf8BytesMut {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
// The trait impls hold the real bodies. Inherent methods (above) forward
// here via explicit `<Self as Utf8Buf>::method` / `<Self as Utf8BufMut>::method`
// UFCS so there is no ambiguity about which impl is called and no fragile
// reliance on the inherent-over-trait method resolution rule.
impl Utf8Buf for Utf8BytesMut {
fn try_split_to(&mut self, at: usize) -> Result<Self, Utf8Error> {
self.validate_char_boundary(at)?;
let inner = self
.inner
.try_split_to(at)
.expect("already checked bounds")
.unwrap_or_else(BytesMut::from);
Ok(Self { inner })
}
fn try_split_off(&mut self, at: usize) -> Result<Self, Utf8Error> {
self.validate_char_boundary(at)?;
let inner = self
.inner
.try_split_off(at)
.expect("already checked bounds")
.unwrap_or_else(BytesMut::from);
Ok(Self { inner })
}
fn try_slice(&self, range: impl RangeBounds<usize>) -> Result<Self, Utf8Error> {
let (start, end) = self.range_bounds_to_start_end(range)?;
Ok(Self {
inner: BytesMut::from(&self.inner.as_slice()[start..end]),
})
}
}
impl Utf8BufMut for Utf8BytesMut {
fn try_truncate(&mut self, new_len: usize) -> Result<(), Utf8Error> {
if new_len >= self.len() {
return Ok(());
}
if !self.as_str().is_char_boundary(new_len) {
return Err(Utf8Error::InvalidCharBoundary { at: new_len });
}
self.inner.truncate(new_len);
Ok(())
}
fn push(&mut self, ch: char) {
let mut buf = [0u8; 4];
let s = ch.encode_utf8(&mut buf);
self.inner.put_slice(s.as_bytes());
}
fn push_str(&mut self, s: &str) {
self.inner.put_slice(s.as_bytes());
}
fn clear(&mut self) {
self.inner.clear();
}
}