boundstr 0.1.0

A string type for Rust whose length is guaranteed to be within bounds.
Documentation
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::TryReserveError;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::{Deref, RangeBounds};
use std::str::FromStr;

use crate::bstr::BStr;
use crate::encoding::{Encoding, Utf8};
use crate::error::{FromUtf8Error, FromUtf16Error, LengthExceeded};

/// A bounded [`String`] whose length is at most `MAX` in encoding `E`.
pub struct BString<const MAX: usize, E = Utf8> {
    s: String,
    /// The remaining budget for the string length, in encoding `E`.
    budget: usize,
    phantom: PhantomData<E>,
}

impl<const MAX: usize, E: Encoding> BString<MAX, E> {
    /// The maximum length, in encoding `E`.
    pub const MAX: usize = MAX;

    /// Creates an empty [`BString`].
    pub const fn new() -> Self {
        Self {
            s: String::new(),
            budget: MAX,
            phantom: PhantomData,
        }
    }

    /// Creates a new empty [`BString`] with at least the specified capacity, in
    /// bytes.
    ///
    /// See [`String::with_capacity`] for details.
    ///
    /// # Panics
    ///
    /// Panics if the capacity exceeds `isize::MAX` _bytes_.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            s: String::with_capacity(capacity),
            budget: MAX,
            phantom: PhantomData,
        }
    }

    /// Converts a vector of bytes to a [`BString`].
    ///
    /// # Errors
    ///
    /// Returns [`FromUtf8Error`] if `vec` is not valid UTF-8 or the string
    /// exceeds `MAX` in encoding `E`.
    pub fn from_utf8(vec: Vec<u8>) -> Result<Self, FromUtf8Error<E>> {
        let s = String::from_utf8(vec)?;
        Ok(Self::from_string(s)?)
    }

    /// Decodes a UTF-16–encoded slice into a [`BString`].
    ///
    /// # Errors
    ///
    /// Returns [`FromUtf16Error`] if `v` is not valid UTF-16 or the string
    /// exceeds `MAX` in encoding `E`.
    pub fn from_utf16(v: &[u16]) -> Result<Self, FromUtf16Error<E>> {
        // TODO: Check the bounds before decoding the whole input.
        let s = String::from_utf16(v)?;
        Ok(Self::from_string(s)?)
    }

    /// Converts a [`String`] to a [`BString`].
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `s` exceeds `MAX` in encoding `E`.
    pub fn from_string(s: String) -> Result<Self, LengthExceeded<E>> {
        let len = E::len(&s);
        if len > MAX {
            return Err(LengthExceeded::new(len, MAX));
        }
        Ok(Self {
            s,
            budget: MAX - len,
            phantom: PhantomData,
        })
    }

    /// Converts a [`String`] to a [`BString`], truncating it to at most `MAX`
    /// in encoding `E`.
    ///
    /// This method ensures that the [`BString`] is still valid UTF-8 after
    /// truncation. If the given `s` cannot be truncated to exactly `MAX`, the
    /// string is truncated at the last valid character boundary in encoding `E`
    /// that does not exceed `MAX`. So, the resulting [`BString`] may be shorter
    /// than `MAX`.
    ///
    /// This method has no effect on the allocated capacity of the string.
    pub fn from_string_truncated(mut s: String) -> Self {
        s.truncate(E::floor_char_boundary(&s, MAX));
        let len = E::len(&s);
        Self {
            s,
            budget: MAX - len,
            phantom: PhantomData,
        }
    }

    /// Returns this [`BString`]'s capacity, in bytes.
    ///
    /// See [`String::capacity`] for details.
    pub const fn capacity(&self) -> usize {
        self.s.capacity()
    }

    /// Reserves capacity for at least `additional` bytes more than the current
    /// length.
    ///
    /// See [`String::reserve`] for details.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
    pub fn reserve(&mut self, additional: usize) {
        self.s.reserve(additional);
    }

    /// Reserves the minimum capacity for at least `additional` bytes more than
    /// the current length.
    ///
    /// See [`String::reserve_exact`] for details.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
    pub fn reserve_exact(&mut self, additional: usize) {
        self.s.reserve_exact(additional);
    }

    /// Tries to reserve capacity for at least `additional` bytes more than the
    /// current length.
    ///
    /// See [`String::try_reserve`] for details.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an
    /// error is returned.
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.s.try_reserve(additional)
    }

    /// Tries to reserve the minimum capacity for at least `additional` bytes
    /// more than the current length.
    ///
    /// See [`String::try_reserve_exact`] for details.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an
    /// error is returned.
    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.s.try_reserve_exact(additional)
    }

    /// Shrinks the capacity of this [`BString`] to match its length.
    ///
    /// See [`String::shrink_to_fit`] for details.
    pub fn shrink_to_fit(&mut self) {
        self.s.shrink_to_fit();
    }

    /// Shrinks the capacity of this [`BString`] with a lower bound, in bytes.
    ///
    /// See [`String::shrink_to`] for details.
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.s.shrink_to(min_capacity);
    }

    /// Appends `ch` to the end of this [`BString`].
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `ch` would make the string exceed `MAX` in
    /// encoding `E`.
    pub fn try_push(&mut self, ch: char) -> Result<(), LengthExceeded<E>> {
        self.try_insert(self.s.len(), ch)
    }

    /// Appends `s` to the end of this [`BString`].
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `s` would make the string exceed `MAX` in
    /// encoding `E`.
    pub fn try_push_str(&mut self, s: &str) -> Result<(), LengthExceeded<E>> {
        self.try_insert_str(self.s.len(), s)
    }

    /// Appends `s` to the end of this [`BString`], truncating `s` so that the
    /// string is at most `MAX` in encoding `E`.
    ///
    /// This method ensures that the [`BString`] is still valid UTF-8 after
    /// truncation. If the given `s` cannot be truncated to reach exactly `MAX`,
    /// `s` is truncated at the last valid character boundary in encoding `E`
    /// that does not make the string exceed `MAX`. So, the resulting
    /// [`BString`] may be shorter than `MAX`.
    ///
    /// The returned slice is empty if all of `s` fit.
    pub fn push_str_truncated<'a>(&mut self, s: &'a str) -> &'a str {
        self.insert_str_truncated(self.s.len(), s)
    }

    /// Inserts `ch` at byte position `idx`.
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `ch` would make the string exceed `MAX` in
    /// encoding `E`.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the string's length in bytes, or if it
    /// does not lie on a char boundary.
    pub fn try_insert(&mut self, idx: usize, ch: char) -> Result<(), LengthExceeded<E>> {
        self.try_insert_str(idx, ch.encode_utf8(&mut [0; 4]))
    }

    /// Inserts `s` at byte position `idx`.
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `s` would make the string exceed `MAX` in
    /// encoding `E`.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the string's length in bytes, or if it
    /// does not lie on a char boundary.
    pub fn try_insert_str(&mut self, idx: usize, s: &str) -> Result<(), LengthExceeded<E>> {
        assert!(
            self.s.is_char_boundary(idx),
            "byte index {idx} is out of bounds or not a char boundary"
        );
        let added = E::len(s);
        if added > self.budget {
            return Err(LengthExceeded::new(MAX - self.budget + added, MAX));
        }
        self.s.insert_str(idx, s);
        self.budget -= added;
        Ok(())
    }

    /// Inserts `s` at byte position `idx`, truncating `s` so that the string is
    /// at most `MAX` in encoding `E`.
    ///
    /// This method ensures that the [`BString`] is still valid UTF-8 after
    /// truncation. If the given `s` cannot be truncated to reach exactly `MAX`,
    /// `s` is truncated at the last valid character boundary in encoding `E`
    /// that does not make the string exceed `MAX`. So, the resulting
    /// [`BString`] may be shorter than `MAX`.
    ///
    /// The returned slice is empty if all of `s` fit.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the string's length in bytes, or if it
    /// does not lie on a char boundary.
    pub fn insert_str_truncated<'a>(&mut self, idx: usize, s: &'a str) -> &'a str {
        let (head, rest) = s.split_at(E::floor_char_boundary(s, self.budget));
        self.s.insert_str(idx, head);
        self.budget -= E::len(head);
        rest
    }

    /// Replaces the given byte range with `with`.
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if `with` would make the string exceed `MAX`
    /// in encoding `E`.
    ///
    /// # Panics
    ///
    /// Panics if the start or end of the range is out of bounds or does not
    /// lie on a char boundary.
    pub fn try_replace_range<R: RangeBounds<usize>>(
        &mut self,
        range: R,
        with: &str,
    ) -> Result<(), LengthExceeded<E>> {
        let range = (range.start_bound().cloned(), range.end_bound().cloned());
        let removed = E::len(&self.s[range]);
        let added = E::len(with);
        let room = self.budget + removed;
        if added > room {
            return Err(LengthExceeded::new(MAX - room + added, MAX));
        }
        self.s.replace_range(range, with);
        self.budget = room - added;
        Ok(())
    }

    /// Replaces the given byte range with `with`, truncating `with` so that the
    /// string is at most `MAX` in encoding `E`.
    ///
    /// The `range` is always removed in full. So, the room for `with` is what
    /// the range frees plus what was already remaining.
    ///
    /// This method ensures that the [`BString`] is still valid UTF-8 after
    /// truncation. If the given `with` cannot be truncated to reach exactly
    /// `MAX`, `with` is truncated at the last valid character boundary in
    /// encoding `E` that does not make the string exceed `MAX`. So, the
    /// resulting [`BString`] may be shorter than `MAX`.
    ///
    /// The returned slice is empty if all of `with` fit.
    ///
    /// # Panics
    ///
    /// Panics if the start or end of the range is out of bounds or does not
    /// lie on a char boundary.
    pub fn replace_range_truncated<'a, R>(&mut self, range: R, with: &'a str) -> &'a str
    where
        R: RangeBounds<usize>,
    {
        let range = (range.start_bound().cloned(), range.end_bound().cloned());
        let removed = E::len(&self.s[range]);
        let room = self.budget + removed;
        let (head, rest) = with.split_at(E::floor_char_boundary(with, room));
        self.s.replace_range(range, head);
        self.budget = room - E::len(head);
        rest
    }

    /// Truncates this [`BString`], removing all contents.
    ///
    /// This method has no effect on the allocated capacity of the string.
    pub fn clear(&mut self) {
        self.s.clear();
        self.budget = MAX;
    }

    /// Removes the last character from this [`BString`] and returns it.
    ///
    /// Returns [`None`] if this [`BString`] is empty.
    pub fn pop(&mut self) -> Option<char> {
        let ch = self.s.pop()?;
        self.budget += E::len(ch.encode_utf8(&mut [0; 4]));
        Some(ch)
    }

    /// Removes the character at byte position `idx` from this [`BString`] and
    /// returns it.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than or equal to the string's length in bytes,
    /// or if it does not lie on a char boundary.
    pub fn remove(&mut self, idx: usize) -> char {
        let ch = self.s.remove(idx);
        self.budget += E::len(ch.encode_utf8(&mut [0; 4]));
        ch
    }

    /// Shortens this [`BString`] to `new_len` bytes.
    ///
    /// If `new_len` is greater than or equal to the string's current length in
    /// bytes, this has no effect.
    ///
    /// This method has no effect on the allocated capacity of the string.
    ///
    /// # Panics
    ///
    /// Panics if `new_len` does not lie on a char boundary.
    pub fn truncate(&mut self, new_len: usize) {
        if new_len < self.s.len() {
            let removed = E::len(&self.s[new_len..]);
            self.s.truncate(new_len);
            self.budget += removed;
        }
    }

    /// Splits this [`BString`] into two at byte position `at`.
    ///
    /// Returns a newly allocated [`BString`]. `self` contains bytes
    /// `[0, at)`, and the returned [`BString`] contains bytes `[at, len)`.
    ///
    /// This method has no effect on the allocated capacity of `self`.
    ///
    /// # Panics
    ///
    /// Panics if `at` is larger than the string's length in bytes, or if it
    /// does not lie on a char boundary.
    #[must_use = "use `.truncate()` if you don't need the other half"]
    pub fn split_off(&mut self, at: usize) -> Self {
        let tail = self.s.split_off(at);
        let moved = E::len(&tail);
        self.budget += moved;
        Self {
            s: tail,
            budget: MAX - moved,
            phantom: PhantomData,
        }
    }

    /// Retains only the characters for which `f` returns `true`.
    ///
    /// This method operates in place, visiting each character exactly once in
    /// the original order, and preserves the order of the retained characters.
    pub fn retain<F>(&mut self, f: F)
    where
        F: FnMut(char) -> bool,
    {
        self.s.retain(f);
        self.budget = MAX - E::len(&self.s);
    }

    /// Returns a [`BStr`] slice of the entire string.
    pub const fn as_bstr(&self) -> &BStr<MAX, E> {
        BStr::from_str_unchecked(self.s.as_str())
    }

    /// Returns a [`str`] slice of the entire string.
    pub const fn as_str(&self) -> &str {
        self.s.as_str()
    }

    /// Converts this [`BString`] into a [`String`].
    pub fn into_string(self) -> String {
        self.s
    }

    /// Converts this [`BString`] into a [`BString`] with a different maximum
    /// length `N`, a different encoding `F`, or both.
    ///
    /// The string is reused without reallocating.
    ///
    /// # Errors
    ///
    /// Returns [`LengthExceeded`] if the string exceeds `N` in encoding `F`.
    pub fn rebound<const N: usize, F: Encoding>(self) -> Result<BString<N, F>, LengthExceeded<F>> {
        BString::from_string(self.s)
    }
}

impl<const MAX: usize, E> fmt::Debug for BString<MAX, E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.s, f)
    }
}

impl<const MAX: usize, E> fmt::Display for BString<MAX, E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.s, f)
    }
}

impl<const MAX: usize, E: Encoding> Default for BString<MAX, E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const MAX: usize, E> Clone for BString<MAX, E> {
    fn clone(&self) -> Self {
        Self {
            s: self.s.clone(),
            budget: self.budget,
            phantom: PhantomData,
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.s.clone_from(&source.s);
        self.budget = source.budget;
    }
}

impl<const MAX: usize, E> PartialEq for BString<MAX, E> {
    fn eq(&self, other: &Self) -> bool {
        self.s == other.s
    }
}

impl<const MAX: usize, E> Eq for BString<MAX, E> {}

impl<const MAX: usize, E> PartialOrd for BString<MAX, E> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<const MAX: usize, E> Ord for BString<MAX, E> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.s.cmp(&other.s)
    }
}

impl<const MAX: usize, E> Hash for BString<MAX, E> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.s.hash(state);
    }
}

impl<const MAX: usize, E: Encoding> Deref for BString<MAX, E> {
    type Target = BStr<MAX, E>;

    fn deref(&self) -> &BStr<MAX, E> {
        self.as_bstr()
    }
}

impl<const MAX: usize, E: Encoding> AsRef<BStr<MAX, E>> for BString<MAX, E> {
    fn as_ref(&self) -> &BStr<MAX, E> {
        self.as_bstr()
    }
}

impl<const MAX: usize, E> AsRef<str> for BString<MAX, E> {
    fn as_ref(&self) -> &str {
        &self.s
    }
}

impl<const MAX: usize, E: Encoding> Borrow<BStr<MAX, E>> for BString<MAX, E> {
    fn borrow(&self) -> &BStr<MAX, E> {
        self.as_bstr()
    }
}

impl<const MAX: usize, E> Borrow<str> for BString<MAX, E> {
    fn borrow(&self) -> &str {
        &self.s
    }
}

impl<const MAX: usize, E: Encoding> ToOwned for BStr<MAX, E> {
    type Owned = BString<MAX, E>;

    fn to_owned(&self) -> BString<MAX, E> {
        BString {
            s: String::from(&**self),
            budget: MAX - E::len(self),
            phantom: PhantomData,
        }
    }
}

impl<const MAX: usize, E: Encoding> From<&BStr<MAX, E>> for BString<MAX, E> {
    fn from(s: &BStr<MAX, E>) -> Self {
        s.to_owned()
    }
}

impl<const MAX: usize, E> From<BString<MAX, E>> for String {
    fn from(s: BString<MAX, E>) -> Self {
        s.s
    }
}

impl<const MAX: usize, E: Encoding> TryFrom<String> for BString<MAX, E> {
    type Error = LengthExceeded<E>;

    fn try_from(s: String) -> Result<Self, LengthExceeded<E>> {
        Self::from_string(s)
    }
}

impl<const MAX: usize, E: Encoding> TryFrom<&str> for BString<MAX, E> {
    type Error = LengthExceeded<E>;

    fn try_from(s: &str) -> Result<Self, LengthExceeded<E>> {
        let len = E::len(s);
        if len > MAX {
            return Err(LengthExceeded::new(len, MAX));
        }
        Ok(Self {
            s: s.to_owned(),
            budget: MAX - len,
            phantom: PhantomData,
        })
    }
}

impl<const MAX: usize, E: Encoding> FromStr for BString<MAX, E> {
    type Err = LengthExceeded<E>;

    fn from_str(s: &str) -> Result<Self, LengthExceeded<E>> {
        Self::try_from(s)
    }
}

#[cfg(feature = "serde")]
mod serde_impl {
    use std::fmt;
    use std::marker::PhantomData;

    use serde_core::de::{self, Deserialize, Deserializer, Unexpected, Visitor};
    use serde_core::ser::{Serialize, Serializer};

    use crate::encoding::Encoding;

    use super::BString;

    impl<const MAX: usize, E> Serialize for BString<MAX, E> {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(&self.s)
        }
    }

    struct BStringVisitor<const MAX: usize, E>(PhantomData<E>);

    impl<const MAX: usize, E: Encoding> Visitor<'_> for BStringVisitor<MAX, E> {
        type Value = BString<MAX, E>;

        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "a string of at most {MAX} in {}", E::default())
        }

        fn visit_str<Err: de::Error>(self, v: &str) -> Result<Self::Value, Err> {
            BString::try_from(v).map_err(Err::custom)
        }

        fn visit_string<Err: de::Error>(self, v: String) -> Result<Self::Value, Err> {
            BString::from_string(v).map_err(Err::custom)
        }

        fn visit_bytes<Err: de::Error>(self, v: &[u8]) -> Result<Self::Value, Err> {
            match str::from_utf8(v) {
                Ok(s) => self.visit_str(s),
                Err(_) => Err(Err::invalid_value(Unexpected::Bytes(v), &self)),
            }
        }

        fn visit_byte_buf<Err: de::Error>(self, v: Vec<u8>) -> Result<Self::Value, Err> {
            match String::from_utf8(v) {
                Ok(s) => self.visit_string(s),
                Err(e) => Err(Err::invalid_value(Unexpected::Bytes(e.as_bytes()), &self)),
            }
        }
    }

    impl<'de, const MAX: usize, E: Encoding> Deserialize<'de> for BString<MAX, E> {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            deserializer.deserialize_string(BStringVisitor::<MAX, E>(PhantomData))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::encoding::Utf16;

    fn bstring<const MAX: usize, E: Encoding>(s: &str) -> BString<MAX, E> {
        BString::from_string(s.to_owned()).unwrap()
    }

    /// The content and the remaining budget of `s`.
    fn state<const MAX: usize, E: Encoding>(s: &BString<MAX, E>) -> (&str, usize) {
        (s.as_str(), s.budget)
    }

    #[test]
    fn from_string_truncated() {
        fn truncated<const MAX: usize, E: Encoding>(s: &str) -> BString<MAX, E> {
            BString::from_string_truncated(s.to_owned())
        }

        assert_eq!(state(&truncated::<10, Utf8>("hello")), ("hello", 5));
        assert_eq!(state(&truncated::<3, Utf8>("hello")), ("hel", 0));
        assert_eq!(state(&truncated::<0, Utf8>("hello")), ("", 0));
        // 'é' is 2 bytes in UTF-8 but 1 code unit in UTF-16.
        assert_eq!(state(&truncated::<2, Utf8>("éé")), ("é", 0));
        assert_eq!(state(&truncated::<1, Utf16>("éé")), ("é", 0));
        assert_eq!(state(&truncated::<3, Utf16>("a😀b")), ("a😀", 0));
        // `MAX` falls inside the 2-byte 'é'.
        assert_eq!(state(&truncated::<2, Utf8>("aé")), ("a", 1));
        // `MAX` falls between the two surrogates of '😀'.
        assert_eq!(state(&truncated::<2, Utf16>("a😀b")), ("a", 1));
    }

    #[test]
    #[should_panic(expected = "not a char boundary")]
    fn try_insert_str_panics_on_invalid_idx_even_if_too_long() {
        // Index 2 is inside the 2-byte 'é', and "xyz" does not fit.
        let _ = bstring::<3, Utf8>("aé").try_insert_str(2, "xyz");
    }

    #[test]
    fn insert_str_truncated() {
        // All of `s` fits.
        let mut s = bstring::<5, Utf8>("ad");
        assert_eq!(s.insert_str_truncated(1, "bc"), "");
        assert_eq!(state(&s), ("abcd", 1));

        // Only a prefix of `s` fits.
        let mut s = bstring::<5, Utf8>("ad");
        assert_eq!(s.insert_str_truncated(1, "bcxyz"), "yz");
        assert_eq!(state(&s), ("abcxd", 0));

        // Nothing fits.
        let mut s = bstring::<2, Utf8>("ad");
        assert_eq!(s.insert_str_truncated(1, "x"), "x");
        assert_eq!(state(&s), ("ad", 0));

        // The budget runs out inside the 2-byte 'é'.
        let mut s = bstring::<4, Utf8>("ab");
        assert_eq!(s.insert_str_truncated(1, "xé"), "é");
        assert_eq!(state(&s), ("axb", 1));

        // '😀' is 2 code units in UTF-16.
        let mut s = bstring::<4, Utf16>("ab");
        assert_eq!(s.insert_str_truncated(1, "😀c"), "c");
        assert_eq!(state(&s), ("a😀b", 0));

        // The budget runs out between the two surrogates of '😀'.
        let mut s = bstring::<3, Utf16>("ab");
        assert_eq!(s.insert_str_truncated(2, "😀"), "😀");
        assert_eq!(state(&s), ("ab", 1));
    }

    #[test]
    fn replace_range_truncated() {
        // All of `with` fits.
        let mut s = bstring::<5, Utf8>("hello");
        assert_eq!(s.replace_range_truncated(1..4, "EY"), "");
        assert_eq!(state(&s), ("hEYo", 1));

        // Only a prefix of `with` fits in what the range frees.
        let mut s = bstring::<5, Utf8>("hello");
        assert_eq!(s.replace_range_truncated(1..3, "WXYZ"), "YZ");
        assert_eq!(state(&s), ("hWXlo", 0));

        // An unbounded range replaces the whole string.
        let mut s = bstring::<3, Utf8>("abc");
        assert_eq!(s.replace_range_truncated(.., "wxyz"), "z");
        assert_eq!(state(&s), ("wxy", 0));

        // The range is removed in full even if nothing of `with` fits: the
        // room runs out inside the 2-byte 'é'.
        let mut s = bstring::<3, Utf8>("abc");
        assert_eq!(s.replace_range_truncated(1..2, "é"), "é");
        assert_eq!(state(&s), ("ac", 1));

        // '😀' is 4 bytes in UTF-8 but 2 code units in UTF-16.
        let mut s = bstring::<3, Utf16>("a😀");
        assert_eq!(s.replace_range_truncated(1..5, "éé!"), "!");
        assert_eq!(state(&s), ("aéé", 0));

        // The room runs out between the two surrogates of '😀'.
        let mut s = bstring::<3, Utf16>("abc");
        assert_eq!(s.replace_range_truncated(1..2, "😀"), "😀");
        assert_eq!(state(&s), ("ac", 1));
    }

    #[test]
    fn rebound() {
        // A larger bound always fits.
        let s = bstring::<3, Utf8>("aé").rebound::<5, Utf8>().unwrap();
        assert_eq!(state(&s), ("aé", 2));

        // 'é' is 2 bytes in UTF-8 but 1 code unit in UTF-16.
        let s = bstring::<3, Utf8>("aé").rebound::<2, Utf16>().unwrap();
        assert_eq!(state(&s), ("aé", 0));

        let err = bstring::<3, Utf8>("aé").rebound::<2, Utf8>().unwrap_err();
        assert_eq!((err.len, err.max), (3, 2));

        // '😀' is 4 bytes in UTF-8 but 2 code units in UTF-16.
        let err = bstring::<4, Utf8>("😀").rebound::<1, Utf16>().unwrap_err();
        assert_eq!((err.len, err.max), (2, 1));
    }

    #[cfg(feature = "serde")]
    #[test]
    fn deserialize() {
        use serde_core::de::Deserialize;
        use serde_core::de::value::{Error, StrDeserializer, StringDeserializer};

        fn copied<const MAX: usize>(s: &str) -> Result<BString<MAX, Utf16>, Error> {
            Deserialize::deserialize(StrDeserializer::new(s))
        }

        fn owned<const MAX: usize>(s: &str) -> Result<BString<MAX, Utf16>, Error> {
            Deserialize::deserialize(StringDeserializer::new(s.to_owned()))
        }

        assert_eq!(state(&copied::<4>("a😀").unwrap()), ("a😀", 1));
        assert!(copied::<2>("a😀").is_err());

        assert_eq!(state(&owned::<4>("a😀").unwrap()), ("a😀", 1));
        assert!(owned::<2>("a😀").is_err());
    }
}