tiny-str 0.10.1

Small string optimization
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
/*  Copyright (C) 2025 Saúl Valdelvira
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>. */

//! Tiny string
//!
//! A string that can store a small amount of bytes on the stack.
//!
//! This struct provides a string-like API, but performs SSO (Small String Optimization)
//! This means that a `TinyString<N>` stores up to N bytes on the stack.
//! If the string grows bigger than that, it moves the contents to the heap.
//!
#![cfg_attr(not(feature = "alloc"), doc = "
# WARNING
The `alloc` feature is disabled. This means that a `TinyString` won't be able to
grow over it's stack capacity.

The following functions from [TinyString] can cause the program to panic if the string
exceeds its capacity.
- [with_capacity]
- [repeat](TinyString::repeat)
- [push]
- [push_str]
- [reserve]
- [reserve_exact]
- [extend_from_within](TinyString::extend_from_within)
- [insert](TinyString::insert)
- [insert_str](TinyString::insert_str)

## Alternatives
| May Panic | No Panic |
| --------- | -------- |
|  [push]   | [push_within_capacity](TinyString::push_within_capacity) |
|  [push_str]   | [push_within_capacity](TinyString::push_str_within_capacity) |
|  [reserve]   | [try_reserve](TinyString::try_reserve) |
| [with_capacity] | [try_with_capacity](TinyString::try_with_capacity) |
| [reserve] | [try_reserve](TinyString::try_reserve) |
| [reserve_exact] | [try_reserve_exact](TinyString::try_reserve_exact) |

[push]: TinyString::push
[push_str]: TinyString::push_str
[reserve]: TinyString::reserve
[reserve_exact]: TinyString::reserve_exact
[with_capacity]: TinyString::with_capacity
")]
//!
//! # Example
//! ```
//! use tiny_str::TinyString;
//!
//! let mut s = TinyString::<10>::new();
//!
//! for (i, c) in (b'0'..=b'9').enumerate() {
//!     s.push(c as char);
//!     assert_eq!(s.len(), i + 1);
//! }
//!
//! // Up to this point, no heap allocations are needed.
//! // The string is stored on the stack.
//!
//! s.push_str("abc"); // This moves the string to the heap
//!
//! assert_eq!(&s[..], "0123456789abc")
//! ```
//!
//! # Memory layout
//! [TinyString] is based on [TinyVec], just like [String] is based on [Vec].
//!
//! You can read the [tiny_vec] crate documentation to learn about the internal
//! representation of the data.
//!
#![cfg_attr(not(feature = "alloc"), doc = "
[String]: <https://doc.rust-lang.org/alloc/string/struct.String.html>
[Vec]: <https://doc.rust-lang.org/alloc/vec/struct.Vec.html>")]

#![no_std]

#![cfg_attr(feature = "use-nightly-features", feature(extend_one))]

use core::fmt::{self, Display};
use core::hash::Hash;
use core::ops::{AddAssign, Bound, Deref, DerefMut, Range, RangeBounds};
use core::str::{self, FromStr, Utf8Error};

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::{
    vec::Vec,
    boxed::Box,
    string::String,
};

use tiny_vec::TinyVec;
pub use tiny_vec::ResizeError;
pub mod iter;

pub mod drain;

mod cow;
pub use cow::Cow;

const MAX_N_STACK_ELEMENTS: usize = tiny_vec::n_elements_for_stack::<u8>();

/// A string that can store a small amount of bytes on the stack.
pub struct TinyString<const N: usize = MAX_N_STACK_ELEMENTS> {
    buf: TinyVec<u8, N>,
}

impl<const N: usize> TinyString<N> {
    fn slice_range<R>(&self, range: R, len: usize) -> Range<usize>
    where
        R: RangeBounds<usize>
    {
        let start = match range.start_bound() {
            Bound::Included(n) => *n,
            Bound::Excluded(n) => *n + 1,
            Bound::Unbounded => 0,
        };

        let end = match range.end_bound() {
            Bound::Included(n) => *n + 1,
            Bound::Excluded(n) => *n,
            Bound::Unbounded => len,
        };

        assert!(start <= end);
        assert!(end <= len);
        assert!(self.is_char_boundary(start));
        assert!(self.is_char_boundary(end));

        Range { start, end }
    }
}

impl<const N: usize> TinyString<N> {

    /// Creates a new [TinyString]
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self { buf: TinyVec::new() }
    }

    /// Creates a new [TinyString] with the given capacity
    #[must_use]
    pub fn with_capacity(cap: usize) -> Self {
        Self { buf: TinyVec::with_capacity(cap) }
    }

    /// Like [with_capacity](Self::with_capacity), but it returns a [Result].
    ///
    /// If an allocation error hapens when reserving the memory, returns
    /// a [ResizeError] unlike [with_capacity](Self::with_capacity), which
    /// panics in such case.
    pub fn try_with_capacity(cap: usize) -> Result<Self,ResizeError> {
        Ok(Self { buf: TinyVec::try_with_capacity(cap)? })
    }

    /// Creates a new [TinyString] from the given utf8 buffer.
    ///
    /// # Errors
    /// If the byte buffer contains invalid uft8
    pub fn from_utf8(utf8: TinyVec<u8, N>) -> Result<Self,Utf8Error> {
        str::from_utf8(utf8.as_slice())?;
        Ok(Self { buf: utf8 })
    }

    /// Creates a new [TinyString] from the given utf8 buffer.
    ///
    /// # Safety
    /// The caller must ensure that the given contains valid utf8
    #[inline(always)]
    #[must_use]
    pub const unsafe fn from_utf8_unchecked(utf8: TinyVec<u8, N>) -> Self {
        Self { buf: utf8 }
    }

    /// Creates a new `TinyString` by repeating the given `slice` `n` times.
    ///
    /// # Panics
    /// If the capacity requires overflows `isize::MAX`
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    /// let s = TinyString::<10>::repeat("abc", 5);
    /// assert_eq!(s.as_str(), "abcabcabcabcabc");
    /// ```
    #[must_use]
    pub fn repeat(slice: &str, n: usize) -> Self {
        Self {
            buf: TinyVec::repeat(slice.as_bytes(), n)
        }
    }

    /// Returns the number of elements inside this string
    #[inline]
    pub const fn len(&self) -> usize { self.buf.len() }

    /// Returns true if the string is empty
    #[inline]
    pub const fn is_empty(&self) -> bool { self.buf.is_empty() }

    /// Returns the allocated capacity for this string
    #[inline]
    pub const fn capacity(&self) -> usize { self.buf.capacity() }

    /// Returns true if the string is currently using stack memory.
    ///
    /// This means that [Self::len] <= `N`
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut vec = TinyString::<5>::new();
    ///
    /// for n in 0..5 {
    ///     vec.push('a')
    /// }
    ///
    /// assert!(vec.lives_on_stack());
    /// vec.push('a');
    /// assert!(!vec.lives_on_stack());
    /// ```
    #[inline]
    pub const fn lives_on_stack(&self) -> bool { self.buf.lives_on_stack() }

    /// Returns a str slice
    #[inline]
    pub const fn as_str(&self) -> &str {
        unsafe { str::from_utf8_unchecked(self.buf.as_slice()) }
    }

    /// Returns a mutable str slice
    #[inline]
    pub const fn as_mut_str(&mut self) -> &mut str {
        unsafe { str::from_utf8_unchecked_mut(self.buf.as_mut_slice()) }
    }

    /// Returns a const pointer to the buffer
    ///
    /// This method shadows [str::as_ptr], to avoid a deref
    #[inline]
    pub const fn as_ptr(&self) -> *const u8 {
        self.buf.as_ptr()
    }

    /// Returns a mutable pointer to the buffer
    ///
    /// This method shadows [str::as_mut_ptr], to avoid a deref
    #[inline]
    pub const fn as_mut_ptr(&mut self) -> *mut u8 {
        self.buf.as_mut_ptr()
    }

    /// Returns the string as a byte slice
    #[inline]
    pub const fn as_bytes(&self) -> &[u8] {
        self.buf.as_slice()
    }

    /// Returns the string as a byte slice
    ///
    /// Returns the string as a mutable bytes slice
    ///
    /// # Safety
    /// Modifying this byte slice is dangerous, because it can leave the
    /// buffer on an inconsistent state.
    /// Strings must be valid UTF8. So manually changing the byte contents
    /// of the string could lead to bugs.
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<10>::from("hello");
    /// unsafe {
    ///     let slice = s.as_mut_bytes();
    ///     assert_eq!(&[104, 101, 108, 108, 111][..], &slice[..]);
    ///     slice.reverse();
    /// }
    /// assert_eq!(s, "olleh");
    /// ```
    #[inline]
    pub const unsafe fn as_mut_bytes(&mut self) -> &mut [u8] {
        self.buf.as_mut_slice()
    }

    /// Returns a mutable reference to the contents of this `TinyString`
    ///
    /// # Safety
    /// Modifying this [TinyVec] is dangerous, because it can leave the
    /// buffer on an inconsistent state.
    /// Strings must be valid UTF8. So mutating the vector without respecting
    /// that could lead to bugs.
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<10>::from("hello");
    /// unsafe {
    ///     let vec = s.as_mut_vec();
    ///     assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
    ///     vec.drain(1..3);
    /// }
    /// assert_eq!(s, "hlo");
    /// ```
    #[inline]
    pub const unsafe fn as_mut_vec(&mut self) -> &mut TinyVec<u8, N> {
        &mut self.buf
    }

    /// Pushes a character into the string
    pub fn push(&mut self, c: char) {
        let len = c.len_utf8();
        if len == 1 {
            self.buf.push(c as u8);
        } else {
            let mut buf = [0_u8; 4];
            c.encode_utf8(&mut buf);
            self.buf.copy_from_slice(&buf[..len]);
        }
    }

    /// Tries to push a character. If the string doesn't have enough capacity to store
    /// the new char, returns an [Err] variant.
    ///
    /// # Errors
    /// If pushing the character would've caused the buffer to grow.
    pub fn push_within_capacity(&mut self, c: char) -> Result<(), char> {
        let len = c.len_utf8();
        if self.buf.len() + len > self.buf.capacity() {
            return Err(c)
        }
        if len == 1 {
            unsafe { self.buf.push_unchecked(c as u8) };
        } else {
            let mut buf = [0_u8; 4];
            c.encode_utf8(&mut buf);
            self.buf.copy_from_slice(&buf[..len]);
        }
        Ok(())
    }


    /// Returns the last char of this string, if present
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<10>::new();
    ///
    /// s.push_str("abcd");
    ///
    /// assert_eq!(s.pop(), Some('d'));
    /// assert_eq!(s, "abc");
    /// ```
    pub fn pop(&mut self) -> Option<char> {
        let c = self.chars().next_back()?;
        let new_len = self.len() - c.len_utf8();
        unsafe {
            self.buf.set_len(new_len);
        }
        Some(c)
    }

    /// Pushes a str slice into this string
    #[inline]
    pub fn push_str(&mut self, s: &str) {
        self.buf.copy_from_slice(s.as_bytes());
    }

    /// Tries to push a str slice. If this `TinyString` doesn't have enough
    /// capacity to store the new slice, returns an [Err] variant.
    ///
    /// # Errors
    /// If pushing the string would've caused the buffer to grow.
    pub fn push_str_within_capacity<'a>(&mut self, s: &'a str) -> Result<(), &'a str> {
        if self.buf.len() + s.len() > self.buf.capacity() {
            Err(s)
        } else {
            self.buf.copy_from_slice(s.as_bytes());
            Ok(())
        }
    }
    /// Shrinks the capacity of this `String` with a lower bound.
    ///
    /// The capacity will remain at least as large as both the length
    /// and the supplied value.
    ///
    /// If the current capacity is less than the lower limit, this is a no-op.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<5>::from("foobar");
    ///
    /// s.reserve(100);
    /// assert!(s.capacity() >= 100);
    ///
    /// s.shrink_to(10);
    /// assert!(s.capacity() >= 10, "{}", s.capacity());
    /// s.shrink_to(0);
    /// assert!(s.capacity() >= 6);
    /// ```
    #[inline]
    #[cfg(feature = "alloc")]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.buf.shrink_to(min_capacity)
    }

    /// Shrinks the capacity of `self` to match its length.
    ///
    /// # Examples
    /// ```
    /// use tiny_str::TinyString;
    /// let mut s = TinyString::<5>::from("foobar");
    ///
    /// s.reserve(100);
    /// assert!(s.capacity() >= 100);
    ///
    /// s.shrink_to_fit();
    /// assert_eq!(6, s.capacity());
    /// ```
    #[inline]
    #[cfg(feature = "alloc")]
    pub fn shrink_to_fit(&mut self) {
        self.buf.shrink_to_fit();
    }

    /// Clears the string
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s: TinyString<5> = TinyString::from("Hello");
    /// s.clear();
    ///
    /// assert!(s.is_empty());
    /// assert_eq!(s.as_str(), "");
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        self.buf.clear();
    }

    /// Reserves space for, at least, n bytes
    #[inline]
    pub fn reserve(&mut self, n: usize) {
        self.buf.reserve(n);
    }

    /// Like [reserve](Self::reserve), but on failure returns an [Err] variant
    /// with a [ResizeError], instead of panicking.
    #[inline]
    pub fn try_reserve(&mut self, n: usize) -> Result<(), ResizeError> {
        self.buf.try_reserve(n)
    }

    /// Reserves space for exactly n more bytes
    #[inline]
    pub fn reserve_exact(&mut self, n: usize) {
        self.buf.reserve_exact(n);
    }

    /// Like [reserve_exact](Self::reserve_exact), but on failure returns an [Err] variant
    /// with a [ResizeError], instead of panicking.
    #[inline]
    pub fn try_reserve_exact(&mut self, n: usize) -> Result<(), ResizeError> {
        self.buf.try_reserve_exact(n)
    }

    /// Converts this TinyString into a boxed str
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<10>::new();
    /// s.push_str("abc");
    ///
    /// let b = s.into_boxed_str();
    /// assert_eq!(&*b, "abc");
    /// ```
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn into_boxed_str(self) -> Box<str> {
        let b = self.buf.into_boxed_slice();
        unsafe { alloc::str::from_boxed_utf8_unchecked(b) }
    }

    /// Copies the slice from the given range to the back
    /// of this string.
    ///
    /// # Panics
    /// - If the range is invalid for [0, self.len)
    /// - If either the start or the end of the range fall
    ///   outside a char boundary
    ///
    /// # Example
    /// ```
    /// use tiny_str::TinyString;
    ///
    /// let mut s = TinyString::<10>::from("abcdefg");
    ///
    /// s.extend_from_within(3..=5);
    ///
    /// assert_eq!(s, "abcdefgdef");
    /// ```
    pub fn extend_from_within<R>(&mut self, range: R)
    where
        R: RangeBounds<usize>
    {
        let Range { start, end } = self.slice_range(range, self.len());
        self.buf.extend_from_within_copied(start..end);
    }

    /// Consumes and leaks the `TinyString`, returning a mutable reference to the contents,
    /// `&'a mut str`.
    ///
    /// This method shrinks the buffer, and moves it to the heap in case it lived
    /// on the stack.
    ///
    /// This function is mainly useful for data that lives for the remainder of
    /// the program's life. Dropping the returned reference will cause a memory
    /// leak.
    ///
    /// # Example
    /// ```
    /// let x = tiny_str::TinyString::<10>::from("ABCDEFG");
    ///
    /// let static_ref: &'static mut str = x.leak();
    /// static_ref.make_ascii_lowercase();
    ///
    /// assert_eq!(static_ref, "abcdefg");
    /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
    /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
    /// # drop(unsafe{Box::from_raw(static_ref)})
    /// ```
    #[cfg(feature = "alloc")]
    pub fn leak<'a>(mut self) -> &'a mut str {
        self.buf.move_to_heap_exact();
        self.buf.shrink_to_fit_heap_only();
        unsafe {
            let bytes = self.buf.leak();
            str::from_utf8_unchecked_mut(bytes)
        }
    }

    /// Splits the string into two at the given byte index.
    ///
    /// Returns a newly allocated `String`. `self` contains bytes `[0, at)`, and
    /// the returned `String` contains bytes `[at, len)`. `at` must be on the
    /// boundary of a UTF-8 code point.
    ///
    /// Note that the capacity of `self` does not change.
    ///
    /// # Panics
    ///
    /// Panics if `at` is not on a `UTF-8` code point boundary, or if it is beyond the last
    /// code point of the string.
    ///
    /// # Examples
    /// ```
    /// let mut hello = tiny_str::TinyString::<8>::from("Hello, World!");
    /// let world = hello.split_off(7);
    /// assert_eq!(hello, "Hello, ");
    /// assert_eq!(world, "World!");
    /// ```
    #[inline]
    #[must_use = "use `.truncate()` if you don't need the other half"]
    pub fn split_off(&mut self, at: usize) -> TinyString<N> {
        assert!(self.is_char_boundary(at));
        let other = self.buf.split_off(at);
        unsafe { TinyString::from_utf8_unchecked(other) }
    }

    /// Shortens this `TinyString` to the specified length.
    ///
    /// If `new_len` is greater than or equal to the string's current length, this has no
    /// effect.
    ///
    /// Note that this method has no effect on the allocated capacity
    /// of the string
    ///
    /// # Panics
    ///
    /// Panics if `new_len` does not lie on a [`char`] boundary.
    ///
    /// # Example
    /// ```
    /// let mut s = tiny_str::TinyString::<6>::from("hello");
    ///
    /// s.truncate(2);
    ///
    /// assert_eq!(s, "he");
    /// ```
    pub fn truncate(&mut self, new_len: usize) {
        assert!(self.is_char_boundary(new_len));
        self.buf.truncate(new_len);
    }

    /// Inserts a character into this `TinyString` at a byte position.
    ///
    /// This is an *O*(*n*) operation as it requires copying every element in the
    /// buffer.
    ///
    /// # Panics
    ///
    /// Panics if `index` is larger than the `TinyString`'s length, or if it does not
    /// lie on a [`char`] boundary.
    ///
    /// # Example
    /// ```
    /// let mut s = tiny_str::TinyString::<10>::from("Hello world :)");
    ///
    /// s.insert(5, '@');
    ///
    /// assert_eq!(s, "Hello@ world :)");
    /// ```
    pub fn insert(&mut self, index: usize, ch: char) {
        assert!(self.is_char_boundary(index));
        let mut buf = [0; 4];
        ch.encode_utf8(&mut buf);
        let len = ch.len_utf8();
        self.buf.insert_slice(index, &buf[..len]).unwrap_or_else(|_| {
            unreachable!("We've checked the index in the assertion above")
        })
    }

    /// Inserts a string slice into this `TinyString` at a byte position.
    ///
    /// This is an *O*(*n*) operation as it requires copying every element in the
    /// buffer.
    ///
    /// # Panics
    ///
    /// Panics if `index` is larger than the `TinyString`'s length, or if it does not
    /// lie on a [`char`] boundary.
    ///
    /// # Example
    /// ```
    /// let mut s = tiny_str::TinyString::<8>::from("Heworld");
    ///
    /// s.insert_str(2, "llo ");
    ///
    /// assert_eq!("Hello world", s);
    /// ```
    pub fn insert_str(&mut self, index: usize, s: &str) {
        assert!(self.is_char_boundary(index));
        self.buf.insert_slice(index, s.as_bytes()).unwrap_or_else(|_| {
            unreachable!("We've checked the index in the assertion above")
        })
    }
}

impl<const N: usize> Default for TinyString<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> Deref for TinyString<N> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl<const N: usize> DerefMut for TinyString<N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_str()
    }
}

impl<const N: usize> From<&str> for TinyString<N> {
    fn from(value: &str) -> Self {
        let mut s = Self::with_capacity(value.len());
        s.push_str(value);
        s
    }
}

impl<const N: usize> TryFrom<&[u8]> for TinyString<N> {
    type Error = Utf8Error;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        str::from_utf8(value)?;
        Ok(unsafe { Self::from_utf8_unchecked(TinyVec::from_slice_copied(value)) })
    }
}

impl<const N: usize> TryFrom<TinyVec<u8, N>> for TinyString<N> {
    type Error = Utf8Error;

    fn try_from(value: TinyVec<u8, N>) -> Result<Self, Self::Error> {
        Self::from_utf8(value)
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> TryFrom<Vec<u8>> for TinyString<N> {
    type Error = Utf8Error;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        str::from_utf8(value.as_slice())?;
        Ok(unsafe { Self::from_utf8_unchecked(TinyVec::from_vec(value)) })
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> From<String> for TinyString<N> {
    fn from(value: String) -> Self {
        let vec = Vec::from(value);
        let vec = TinyVec::<_, N>::from_vec(vec);
        unsafe { Self::from_utf8_unchecked(vec) }
    }
}

impl<const N: usize> From<TinyString<N>> for TinyVec<u8, N> {
    fn from(value: TinyString<N>) -> Self {
        value.buf
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> From<TinyString<N>> for Vec<u8> {
    fn from(value: TinyString<N>) -> Self {
        value.buf.into_vec()
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> From<TinyString<N>> for Box<str> {
    fn from(value: TinyString<N>) -> Self {
        value.into_boxed_str()
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> From<Box<str>> for TinyString<N> {
    fn from(value: Box<str>) -> Self {
        let vec = value.as_bytes();
        let s = TinyVec::from(vec);
        unsafe { Self::from_utf8_unchecked(s) }
    }
}

macro_rules! impl_from_iter {
    ($( $( { $($tok:tt)* } )? $t:ty),* $(,)?) => {
        $(
            impl< $($($tok)*, )? const N: usize> FromIterator<$t> for TinyString<N> {
                fn from_iter<T: IntoIterator<Item = $t>>(iter: T) -> Self {
                    let mut s = Self::new();
                    s.extend(iter);
                    s
                }
            }
        )*
    };
}

impl_from_iter!(
    char,
    {'a} &'a char,
    {'a} &'a str,
    {'a, const M: usize} Cow<'a, M>
);

#[cfg(feature = "alloc")]
impl_from_iter!(Box<str>);

impl<const N: usize> Extend<char> for TinyString<N> {
    fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) {
        let iter = iter.into_iter();
        let (lower, _) = iter.size_hint();
        self.reserve(lower);
        for c in iter {
            self.push(c);
        }
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_one(&mut self, item: char) {
        self.push(item);
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_reserve(&mut self, additional: usize) {
        self.reserve(additional);
    }
}

impl<'a, const N: usize> Extend<&'a char> for TinyString<N> {
    fn extend<T: IntoIterator<Item = &'a char>>(&mut self, iter: T) {
        iter.into_iter().for_each(|slice| self.push(*slice));
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_one(&mut self, item: &'a char) {
        self.push(*item);
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_reserve(&mut self, additional: usize) {
        self.reserve(additional);
    }
}

impl<'a, const N: usize> Extend<&'a str> for TinyString<N> {
    fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
        iter.into_iter().for_each(|slice| self.push_str(slice));
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_one(&mut self, item: &'a str) {
        self.push_str(item);
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> Extend<Box<str>> for TinyString<N> {
    fn extend<T: IntoIterator<Item = Box<str>>>(&mut self, iter: T) {
        iter.into_iter().for_each(|slice| self.push_str(&slice));
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_one(&mut self, item: Box<str>) {
        self.push_str(&item);
    }
}

impl<'a, const N: usize, const M: usize> Extend<Cow<'a, M>> for TinyString<N> {
    fn extend<T: IntoIterator<Item = Cow<'a, M>>>(&mut self, iter: T) {
        iter.into_iter().for_each(|slice| self.push_str(&slice));
    }

    #[cfg(feature = "use-nightly-features")]
    #[inline]
    fn extend_one(&mut self, item: Cow<'a, M>) {
        self.push_str(&item);
    }
}

impl<const N: usize, const M: usize> PartialEq<TinyString<M>> for TinyString<N> {
    fn eq(&self, other: &TinyString<M>) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

impl<const N: usize> Eq for TinyString<N> { }

impl<'a, const N: usize, const M: usize> PartialEq<Cow<'a, M>> for TinyString<N> {
    fn eq(&self, other: &Cow<'a, M>) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> PartialEq<String> for TinyString<N> {
    fn eq(&self, other: &String) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

impl<const N: usize, const M: usize> PartialEq<TinyVec<u8, M>> for TinyString<N> {
    fn eq(&self, other: &TinyVec<u8, M>) -> bool {
        self.as_bytes() == other.as_slice()
    }
}

impl<'a, const N: usize, const M: usize> PartialEq<tiny_vec::Cow<'a, u8, M>> for TinyString<N> {
    fn eq(&self, other: &tiny_vec::Cow<'a, u8, M>) -> bool {
        self.as_bytes() == other.as_slice()
    }
}

#[cfg(feature = "alloc")]
impl<const N: usize> PartialEq<Vec<u8>> for TinyString<N> {
    fn eq(&self, other: &Vec<u8>) -> bool {
        self.as_bytes() == other.as_slice()
    }
}

impl<const N: usize> PartialEq<str> for TinyString<N> {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl<'a, const N: usize> PartialEq<&'a str> for TinyString<N> {
    fn eq(&self, other: &&'a str) -> bool {
        self.as_str() == *other
    }
}

impl<const N: usize> PartialEq<[u8]> for TinyString<N> {
    fn eq(&self, other: &[u8]) -> bool {
        self.as_bytes() == other
    }
}

impl<const N: usize> PartialEq<TinyString<N>> for &str {
    fn eq(&self, other: &TinyString<N>) -> bool {
        self.as_bytes() == other.as_bytes()
    }
}

impl<const N: usize> PartialOrd<TinyString<N>> for TinyString<N> {
    #[inline]
    fn partial_cmp(&self, other: &TinyString<N>) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<const N: usize> Ord for TinyString<N> {
    #[inline]
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.buf.cmp(&other.buf)
    }
}

impl<const N: usize> Clone for TinyString<N> {
    #[inline]
    fn clone(&self) -> Self {
        Self { buf: self.buf.clone() }
    }

    #[inline]
    fn clone_from(&mut self, source: &Self) {
        self.buf.clone_from(&source.buf);
    }
}

impl<const N: usize> AsRef<[u8]> for TinyString<N> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<const N: usize> AsRef<str> for TinyString<N> {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<const N: usize> AsMut<str> for TinyString<N> {
    #[inline]
    fn as_mut(&mut self) -> &mut str {
        self.as_mut_str()
    }
}

impl<const N: usize> AsRef<TinyString<N>> for TinyString<N> {
    #[inline]
    fn as_ref(&self) -> &TinyString<N> {
        self
    }
}

impl<const N: usize> AsMut<TinyString<N>> for TinyString<N> {
    #[inline]
    fn as_mut(&mut self) -> &mut TinyString<N> {
        self
    }
}

impl<const N: usize> Hash for TinyString<N> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.buf.hash(state);
    }
}

impl<const N: usize> fmt::Debug for TinyString<N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:?}", self.bytes())
    }
}

impl<const N: usize> Display for TinyString<N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<const N: usize> FromStr for TinyString<N> {
    type Err = core::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(s))
    }
}

impl<const N: usize> core::fmt::Write for TinyString<N> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.push_str(s);
        Ok(())
    }

    fn write_char(&mut self, c: char) -> fmt::Result {
        self.push(c);
        Ok(())
    }
}

impl<'a, const N: usize> AddAssign<&'a str> for TinyString<N> {
    #[inline]
    fn add_assign(&mut self, rhs: &'a str) {
        self.push_str(rhs);
    }
}

#[cfg(feature = "serde")]
impl<const N: usize> serde::Serialize for TinyString<N> {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer
    {
        self.buf.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, const N: usize> serde::Deserialize<'de> for TinyString<N> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>
    {
        let buf = TinyVec::<u8, N>::deserialize(deserializer)?;
        Ok(Self { buf })
    }
}

#[cfg(test)]
mod test;