small-collections 0.6.0

A collection of data structures optimized for small-buffer scenarios that reside on the stack and seamlessly spill to the heap.
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
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
//! UTF-8 string that lives on the stack and spills to the heap.
//!
//! Provides [`SmallString`] — backed by `heapless::String<N>` (stack) and
//! `std::string::String` (heap).  Spill uses `from_utf8_unchecked` to skip a redundant
//! UTF-8 scan since both sources already guarantee valid UTF-8, giving a memcpy-only
//! migration cost.
//!
//! Implements `Deref<Target = str>` so all `&str` methods are available directly.
//! [`AnyString`] provides an object-safe trait over both backends.

use core::mem::ManuallyDrop;
use std::borrow::{Borrow, BorrowMut};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};

/// A trait for abstraction over different string types (Stack, Heap, Small).
pub trait AnyString {
    /// Returns a string slice representing the contents.
    fn as_str(&self) -> &str;
    /// Returns the number of elements.
    fn len(&self) -> usize;
    /// Returns `true` if the collection is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Pushes an item into the collection.
    fn push_str(&mut self, s: &str);
    /// Pushes an item into the collection.
    fn push(&mut self, ch: char);
    /// Clears all elements from the collection.
    fn clear(&mut self);
    /// Removes and returns an item from the collection.
    fn pop(&mut self) -> Option<char>;
    /// Truncates the collection to a specific length.
    fn truncate(&mut self, new_len: usize);
}

impl AnyString for String {
    fn as_str(&self) -> &str {
        self.as_str()
    }
    fn len(&self) -> usize {
        self.len()
    }
    fn push_str(&mut self, s: &str) {
        self.push_str(s);
    }
    fn push(&mut self, ch: char) {
        self.push(ch);
    }
    fn clear(&mut self) {
        self.clear();
    }
    fn pop(&mut self) -> Option<char> {
        self.pop()
    }
    fn truncate(&mut self, new_len: usize) {
        self.truncate(new_len);
    }
}

/// A string that lives on the stack for `N` bytes, then spills to the heap.
///
/// # Overview
/// This collection uses a `heapless::String` for stack storage and a
/// `std::string::String` for heap storage.
///
/// # Safety
/// * `on_stack` tag determines which side of the `StringData` union is active.
/// * `SmallString` ensures all data remains valid UTF-8 by leveraging the invariants
///   of the underlying collections and performing manual UTF-8 checks only when necessary.
pub struct SmallString<const N: usize> {
    on_stack: bool,
    data: StringData<N>,
}

impl<const N: usize> AnyString for SmallString<N> {
    fn as_str(&self) -> &str {
        self.as_str()
    }
    fn len(&self) -> usize {
        self.len()
    }
    fn push_str(&mut self, s: &str) {
        self.push_str(s);
    }
    fn push(&mut self, ch: char) {
        self.push(ch);
    }
    fn clear(&mut self) {
        self.clear();
    }
    fn pop(&mut self) -> Option<char> {
        self.pop()
    }
    fn truncate(&mut self, new_len: usize) {
        self.truncate(new_len);
    }
}

/// The internal storage for `SmallString`.
///
/// We use `ManuallyDrop` because the compiler cannot know which field is active
/// and therefore cannot automatically drop the correct one.
union StringData<const N: usize> {
    stack: ManuallyDrop<heapless::String<N>>,
    heap: ManuallyDrop<std::string::String>,
}

impl<const N: usize> SmallString<N> {
    /// The maximum allowed stack size in bytes (16 KB).
    pub const MAX_STACK_SIZE: usize = 16 * 1024;

    /// Creates a new empty SmallString.
    ///
    /// # Compile-Time Safety
    /// **Size Limit:** Enforces a limit of 16 KB. Exceeding this fails the build.
    ///
    /// ## Test: Valid (Compiles)
    /// ```rust
    /// use small_collections::SmallString;
    /// let s: SmallString<64> = SmallString::new();
    /// ```
    ///
    /// ## Test: Invalid Size (Fails Compilation)
    /// ```rust,compile_fail
    /// use small_collections::SmallString;
    /// // 32 KB string -> Too big for stack guard
    /// let s: SmallString<32768> = SmallString::new();
    /// ```
    ///
    pub fn new() -> Self {
        // COMPILER GUARD
        const {
            assert!(
                std::mem::size_of::<Self>() <= Self::MAX_STACK_SIZE,
                "SmallString is too large! The struct size exceeds the 16KB limit. Reduce N."
            );
        }

        Self {
            on_stack: true,
            data: StringData {
                stack: ManuallyDrop::new(heapless::String::new()),
            },
        }
    }

    /// Creates a SmallString from a literal or slice
    pub fn from_str(s: &str) -> Self {
        let mut str = Self::new();
        str.push_str(s);
        str
    }

    /// Returns `true` if the string is currently storing data on the stack.
    #[inline(always)]
    pub fn is_on_stack(&self) -> bool {
        self.on_stack
    }

    // --- Core Operations ---

    /// Appends the given `char` to the end of this `SmallString`.
    ///
    /// If the stack capacity `N` is exceeded, this triggers a transparent spill to the heap.
    #[inline(always)]
    pub fn push(&mut self, ch: char) {
        unsafe {
            if self.on_stack {
                // 1. Get reference to the inner stack string
                let stack_str = &mut *self.data.stack;

                // 2. Pre-emptive Size Check
                // We check if adding the char's byte length exceeds capacity N.
                if stack_str.len() + ch.len_utf8() > N {
                    self.spill_to_heap_and_push_char(ch);
                } else {
                    // 3. Guaranteed Success
                    // Since we checked capacity, this push will never fail.
                    // We can safely ignore the Result.
                    match stack_str.push(ch) {
                        Ok(()) => return, // Success: exit early
                        Err(_) => unreachable!("Stack capacity check failed in push"),
                    }
                }
            } else {
                (*self.data.heap).push(ch);
            }
        }
    }

    /// Appends a given string slice onto the end of this `SmallString`.
    ///
    /// If the stack capacity `N` is exceeded, this triggers a transparent spill to the heap.
    #[inline(always)]
    pub fn push_str(&mut self, s: &str) {
        unsafe {
            if self.on_stack {
                let stack_str = &mut *self.data.stack;

                // 2. Pre-emptive Size Check
                // s.len() returns the byte length, which matches N's definition.
                if stack_str.len() + s.len() > N {
                    self.spill_to_heap_and_push_str(s);
                } else {
                    // 3. Guaranteed Success
                    match stack_str.push_str(s) {
                        Ok(()) => return, // Success: exit early
                        Err(_) => unreachable!("Stack capacity check failed in push str"),
                    }
                }
            } else {
                (*self.data.heap).push_str(s);
            }
        }
    }

    /// Returns the length of this `SmallString`, in bytes, not [`char`]s or graphemes.
    #[inline(always)]
    pub fn len(&self) -> usize {
        // We can use the Deref trait here for simplicity,
        // but explicit access is slightly faster in debug builds.
        unsafe {
            if self.on_stack {
                self.data.stack.len()
            } else {
                self.data.heap.len()
            }
        }
    }

    /// Returns `true` if this `SmallString` has a length of zero, and `false` otherwise.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Truncates this `SmallString`, removing all contents.
    ///
    /// While this means the `SmallString` will have a length of zero, it does not
    /// touch its capacity.
    #[inline(always)]
    pub fn clear(&mut self) {
        unsafe {
            if self.on_stack {
                (*self.data.stack).clear();
            } else {
                (*self.data.heap).clear();
            }
        }
    }

    // --- Spill Logic ---

    #[inline(never)]
    unsafe fn spill_to_heap_and_push_str(&mut self, pending_str: &str) {
        unsafe {
            // 1. Get raw bytes from stack (Instant access)
            let stack_bytes = self.data.stack.as_bytes();
            let pending_bytes = pending_str.as_bytes();

            // 2. Calculate exact capacity needed
            // We use N*2 strategy to prevent frequent re-allocations after spill
            let total_len = stack_bytes.len() + pending_bytes.len();
            let cap = std::cmp::max(total_len, N * 2);

            // 3. Allocate RAW MEMORY (Vec<u8>)
            // We do NOT allocate a String yet. This avoids String metadata overhead.
            let mut heap_vec = Vec::with_capacity(cap);

            // 4. Bitwise Copy (Memcpy)
            // extend_from_slice compiles down to a highly optimized 'rep movsb' or SIMD copy.
            heap_vec.extend_from_slice(stack_bytes);
            heap_vec.extend_from_slice(pending_bytes);

            // 5. ZERO-COST TRANSFORMATION
            // We skip the O(N) UTF-8 validation scan.
            // Safety: We know 'stack_bytes' came from a valid str, and 'pending_str' is a valid str.
            let new_heap = String::from_utf8_unchecked(heap_vec);

            // 6. State Switch
            ManuallyDrop::drop(&mut self.data.stack);
            self.data.heap = ManuallyDrop::new(new_heap);
            self.on_stack = false;
        }
    }

    #[inline(never)]
    unsafe fn spill_to_heap_and_push_char(&mut self, pending_char: char) {
        unsafe {
            let stack_bytes = self.data.stack.as_bytes();

            // char can be up to 4 bytes
            let cap = std::cmp::max(stack_bytes.len() + 4, N * 2);
            let mut heap_vec = Vec::with_capacity(cap);

            heap_vec.extend_from_slice(stack_bytes);

            // Encode char directly into the buffer without intermediate String allocation
            // 'encode_utf8' writes bytes directly to the end of the Vec
            let char_len = pending_char.len_utf8();
            let old_len = heap_vec.len();

            // Reserve space and write directly (unsafe speedup)
            heap_vec.set_len(old_len + char_len);
            pending_char.encode_utf8(&mut heap_vec[old_len..]);

            let new_heap = String::from_utf8_unchecked(heap_vec);

            ManuallyDrop::drop(&mut self.data.stack);
            self.data.heap = ManuallyDrop::new(new_heap);
            self.on_stack = false;
        }
    }
}

// --- Critical Trait: Deref ---
// This allows SmallString to be used exactly like &str
// You can call .trim(), .split(), .contains() on it automatically.

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

    fn deref(&self) -> &Self::Target {
        unsafe {
            if self.on_stack {
                self.data.stack.as_str()
            } else {
                self.data.heap.as_str()
            }
        }
    }
}

impl<const N: usize> DerefMut for SmallString<N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe {
            if self.on_stack {
                (*self.data.stack).as_mut_str()
            } else {
                (*self.data.heap).as_mut_str()
            }
        }
    }
}

impl<const N: usize> SmallString<N> {
    /// Extracts a string slice containing the entire `SmallString`.
    ///
    /// Equivalent to `&s[..]`.
    #[inline(always)]
    pub fn as_str(&self) -> &str {
        // We can leverage the Deref trait we already wrote
        &**self
    }

    /// Extracts a mutable string slice containing the entire `SmallString`.
    ///
    /// Equivalent to `&mut s[..]`.
    #[inline(always)]
    pub fn as_mut_str(&mut self) -> &mut str {
        // Leverage DerefMut
        &mut **self
    }

    /// Converts a `SmallString` into a byte slice.
    #[inline(always)]
    pub fn as_bytes(&self) -> &[u8] {
        self.as_str().as_bytes()
    }

    /// Converts a mutable `SmallString` into a mutable byte slice.
    ///
    /// # Safety
    /// The caller must ensure that the content of the slice remains valid UTF-8.
    /// If this invariant is violated, it is Undefined Behavior.
    #[inline(always)]
    pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
        unsafe { self.as_mut_str().as_bytes_mut() }
    }
}

// --- Safety & Standard Traits ---

// Manual implementation of Clone.
// We must check `on_stack` to know which field to clone.
impl<const N: usize> Clone for SmallString<N> {
    fn clone(&self) -> Self {
        unsafe {
            if self.on_stack {
                // Clone the Stack string
                let stack_clone = (*self.data.stack).clone();
                SmallString {
                    on_stack: true,
                    data: StringData {
                        stack: ManuallyDrop::new(stack_clone),
                    },
                }
            } else {
                // Clone the Heap string
                let heap_clone = (*self.data.heap).clone();
                SmallString {
                    on_stack: false,
                    data: StringData {
                        heap: ManuallyDrop::new(heap_clone),
                    },
                }
            }
        }
    }
}

impl<const N: usize> Drop for SmallString<N> {
    fn drop(&mut self) {
        unsafe {
            if self.on_stack {
                ManuallyDrop::drop(&mut self.data.stack);
            } else {
                ManuallyDrop::drop(&mut self.data.heap);
            }
        }
    }
}

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

impl<const N: usize> fmt::Display for SmallString<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_str(), f) // Delegate to str implementation
    }
}

impl<const N: usize> fmt::Debug for SmallString<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_str(), f)
    }
}

// Allow: let s: SmallString<32> = "Hello".into();
impl<const N: usize> From<&str> for SmallString<N> {
    fn from(s: &str) -> Self {
        Self::from_str(s)
    }
}

// Allow writing to it like a buffer: write!(s, "Val: {}", 42)
impl<const N: usize> fmt::Write for SmallString<N> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.push_str(s);
        Ok(())
    }
}

// --- Equality (==) ---

// Added `const M: usize` to allow comparing SmallStrings of different capacities.
// e.g., SmallString<16> == SmallString<2>
impl<const N: usize, const M: usize> PartialEq<SmallString<M>> for SmallString<N> {
    fn eq(&self, other: &SmallString<M>) -> bool {
        self.as_str() == other.as_str()
    }
}

// Eq implies PartialEq<Self>, which is satisfied by the implementation above where M = N.
impl<const N: usize> Eq for SmallString<N> {}

// --- Equality with other types (unchanged) ---

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

// Allow: small_string == "hello" (where "hello" is &str)
impl<'a, const N: usize> PartialEq<&'a str> for SmallString<N> {
    fn eq(&self, other: &&'a str) -> bool {
        self.as_str() == *other
    }
}

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

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

// --- Ordering (<, >) ---

// FIXED: Added `const M: usize` here as well.
impl<const N: usize, const M: usize> PartialOrd<SmallString<M>> for SmallString<N> {
    fn partial_cmp(&self, other: &SmallString<M>) -> Option<Ordering> {
        Some(self.as_str().cmp(other.as_str()))
    }
}

impl<const N: usize> Ord for SmallString<N> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

// --- Hashing ---

impl<const N: usize> Hash for SmallString<N> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // IMPORTANT: Hash the string slice, not the struct fields.
        // This ensures hash("abc") == hash(SmallString::from("abc"))
        self.as_str().hash(state);
    }
}

// --- Borrowing --

impl<const N: usize> Borrow<str> for SmallString<N> {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<const N: usize> BorrowMut<str> for SmallString<N> {
    fn borrow_mut(&mut self) -> &mut str {
        self.as_mut_str()
    }
}

impl<const N: usize> AsRef<str> for SmallString<N> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

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

// --- Extend ---

// Allow: let s: SmallString<16> = chars.collect();
impl<const N: usize> FromIterator<char> for SmallString<N> {
    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
        let mut s = Self::new();
        for c in iter {
            s.push(c); // push handles spilling automatically
        }
        s
    }
}

// Allow: let s: SmallString<16> = strings.collect();
impl<'a, const N: usize> FromIterator<&'a str> for SmallString<N> {
    fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
        let mut s = Self::new();
        for str_slice in iter {
            s.push_str(str_slice);
        }
        s
    }
}

// Allow: s.extend(chars)
impl<const N: usize> Extend<char> for SmallString<N> {
    fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
        for c in iter {
            self.push(c);
        }
    }
}

// Allow: s.extend(strings)
impl<'a, const N: usize> Extend<&'a str> for SmallString<N> {
    fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
        for str_slice in iter {
            self.push_str(str_slice);
        }
    }
}

// --- Pop, Truncate ---

impl<const N: usize> SmallString<N> {
    /// Removes the last character from the string buffer and returns it.
    /// Returns None if the string is empty.
    #[inline(always)]
    pub fn pop(&mut self) -> Option<char> {
        unsafe {
            if self.on_stack {
                (*self.data.stack).pop()
            } else {
                (*self.data.heap).pop()
            }
        }
    }

    /// Shortens this String to the specified length.
    /// If new_len >= current length, this does nothing.
    /// Panics if new_len does not lie on a char boundary.
    #[inline(always)]
    pub fn truncate(&mut self, new_len: usize) {
        unsafe {
            if self.on_stack {
                (*self.data.stack).truncate(new_len);
            } else {
                (*self.data.heap).truncate(new_len);
            }
        }
    }

    /// Returns the total capacity (in bytes) of the string.
    #[inline(always)]
    pub fn capacity(&self) -> usize {
        unsafe {
            if self.on_stack {
                N // Stack capacity is fixed at N
            } else {
                (*self.data.heap).capacity()
            }
        }
    }
}

// --- Reserve ---

impl<const N: usize> SmallString<N> {
    /// Ensures that this string has at least the specified capacity.
    /// If the request exceeds N, this forces a spill to the heap immediately.
    #[inline(always)]
    pub fn reserve(&mut self, additional: usize) {
        unsafe {
            if self.on_stack {
                let len = self.len();
                if len + additional > N {
                    // Force spill logic
                    // We cheat slightly by calling spill with an empty string,
                    // but we modify the logic to respect `capacity`.

                    // 1. Copy Stack
                    let stack_bytes = self.data.stack.as_bytes();

                    // 2. Alloc Heap with requested size
                    let cap = std::cmp::max(len + additional, N * 2);
                    let mut heap_vec = Vec::with_capacity(cap);
                    heap_vec.extend_from_slice(stack_bytes);

                    let new_heap = String::from_utf8_unchecked(heap_vec);

                    // 3. Switch
                    ManuallyDrop::drop(&mut self.data.stack);
                    self.data.heap = ManuallyDrop::new(new_heap);
                    self.on_stack = false;
                }
                // Else: it fits on stack, do nothing.
            } else {
                (*self.data.heap).reserve(additional);
            }
        }
    }
}

// -- u8 --

impl<const N: usize> SmallString<N> {
    /// Converts a vector of bytes to a SmallString.
    ///
    /// If the bytes are not valid UTF-8, this returns an error.
    /// If valid, it attempts to store them on the Stack if they fit.
    pub fn from_utf8(vec: Vec<u8>) -> Result<Self, std::string::FromUtf8Error> {
        // 1. Check UTF-8 validity using std helper
        let s_std = String::from_utf8(vec)?;

        // 2. Try to fit on Stack
        if s_std.len() <= N {
            let mut small = Self::new();
            small.push_str(&s_std);
            Ok(small)
        } else {
            // 3. Keep on Heap
            Ok(Self {
                on_stack: false,
                data: StringData {
                    heap: ManuallyDrop::new(s_std),
                },
            })
        }
    }

    /// Converts a byte slice to a SmallString.
    pub fn from_utf8_lossy(bytes: &[u8]) -> Self {
        let cow = String::from_utf8_lossy(bytes);
        match cow {
            // It was already valid utf8 and borrowed
            std::borrow::Cow::Borrowed(s) => Self::from(s),
            // It had invalid chars and was replaced/allocated
            std::borrow::Cow::Owned(s) => {
                if s.len() <= N {
                    Self::from(s.as_str())
                } else {
                    Self {
                        on_stack: false,
                        data: StringData {
                            heap: ManuallyDrop::new(s),
                        },
                    }
                }
            }
        }
    }
}

// -- interoperability --

impl<const N: usize> SmallString<N> {
    /// Consumes the SmallString and returns a heap-allocated `std::string::String`.
    ///
    /// If already on the heap, this is free (zero allocation).
    /// If on the stack, this allocates a new String.
    pub fn into_string(self) -> String {
        // Prevent Drop from running, we are taking ownership
        let mut this = ManuallyDrop::new(self);

        unsafe {
            if this.on_stack {
                let stack_str = &*this.data.stack;
                stack_str.as_str().to_string()
            } else {
                // Take ownership of the heap string
                ManuallyDrop::take(&mut this.data.heap)
            }
        }
    }

    /// Consumes the SmallString and returns a `Vec<u8>`.
    pub fn into_bytes(self) -> Vec<u8> {
        self.into_string().into_bytes()
    }
}

// -- retian --

impl<const N: usize> SmallString<N> {
    /// Retains only the characters specified by the predicate.
    /// In other words, remove all characters `c` such that `f(c)` returns `false`.
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(char) -> bool,
    {
        unsafe {
            if self.on_stack {
                // heapless::String doesn't implement retain directly.
                // We simulate it by rebuilding the string in-place (simplest safe approach)
                // or doing a "scan and shift".
                // Since N is small, rebuilding is acceptable overhead.

                let stack_str = &mut *self.data.stack;
                // Note: This is an O(N) approach using a temporary buffer strategy
                // implicitly via string manipulation, or we simply rely on the fact
                // that we can't easily mutate the private buffer of heapless.

                // Safe Workaround: Copy chars that pass to a temp buffer, then clear and push back.
                // Since N is small (stack), this copy is cheap.
                let mut temp: heapless::String<N> = heapless::String::new();
                for c in stack_str.chars() {
                    if f(c) {
                        match temp.push(c) {
                            Ok(()) => continue,
                            Err(_) => unreachable!("temp string capacity check failed in push"),
                        }
                    }
                }
                *stack_str = temp;
            } else {
                (*self.data.heap).retain(f);
            }
        }
    }
}

// -- shrink --

impl<const N: usize> SmallString<N> {
    /// Demands that the underlying buffer releases any extra capacity.
    /// Only affects the Heap state. Stack state size is fixed at `N`.
    pub fn shrink_to_fit(&mut self) {
        unsafe {
            if !self.on_stack {
                (*self.data.heap).shrink_to_fit();
            }
        }
    }
}

#[cfg(test)]
mod string_basic_tests {

    use super::*;
    use std::borrow::Borrow;
    use std::collections::HashSet;
    use std::collections::hash_map::DefaultHasher;
    use std::fmt::Write; // Required for write! macro tests
    use std::hash::{Hash, Hasher};

    #[test]
    fn test_string_traits_borrow() {
        use std::borrow::{Borrow, BorrowMut};
        let mut s: SmallString<16> = SmallString::from("abc");

        // Test Borrow<str>
        let b: &str = s.borrow();
        assert_eq!(b, "abc");

        // Test BorrowMut<str>
        let b_mut: &mut str = s.borrow_mut();
        b_mut.make_ascii_uppercase();
        assert_eq!(s.as_str(), "ABC");
    }

    #[test]
    fn test_string_stack_ops_basic() {
        let mut s: SmallString<16> = SmallString::new();

        assert!(s.is_on_stack());
        assert!(s.is_empty());

        s.push_str("Hello");
        assert_eq!(s.len(), 5);
        assert_eq!(s.as_str(), "Hello");
        assert!(s.is_on_stack());

        s.push(' ');
        s.push_str("World");
        assert_eq!(s.len(), 11);
        assert_eq!(&*s, "Hello World"); // Test Deref
        assert!(s.is_on_stack());
    }

    // --- 2. Exact Boundary Tests ---
    #[test]
    fn test_string_spill_trigger_on_exact_capacity() {
        let mut s: SmallString<5> = SmallString::new();

        // Fill exactly to capacity
        s.push_str("12345");

        assert_eq!(s.len(), 5);
        assert!(s.is_on_stack(), "Should remain on stack at exact capacity");

        // Push one more char -> Spill
        s.push('6');
        assert!(!s.is_on_stack(), "Should spill after N+1");
        assert_eq!(s.as_str(), "123456");
    }

    // --- 3. Spill via push_str (Multi-char) ---
    #[test]
    fn test_string_spill_trigger_on_push_str() {
        let mut s: SmallString<4> = SmallString::new();
        s.push_str("Hi"); // Len 2 (Stack)

        // Push string that exceeds remaining space AND total capacity
        s.push_str(" there"); // "Hi there" = 8 chars

        assert!(!s.is_on_stack());
        assert_eq!(s.as_str(), "Hi there");
        assert_eq!(s.len(), 8);
    }

    // --- 4. Spill via push (Single char) ---
    #[test]
    fn test_string_spill_trigger_on_push_char() {
        let mut s: SmallString<3> = SmallString::new();
        s.push('A');
        s.push('B');
        s.push('C'); // Full (3/3)

        s.push('D'); // Spill

        assert!(!s.is_on_stack());
        assert_eq!(s.as_str(), "ABCD");
    }

    // --- 5. UTF-8 & Emojis (CRITICAL for unsafe blocks) ---
    #[test]
    fn test_string_spill_trigger_on_multibyte_char() {
        // Emojis are 4 bytes each.
        // Capacity 6: Can hold 1 emoji (4 bytes) + 2 bytes.
        // Cannot hold 2 emojis (8 bytes).
        let mut s: SmallString<6> = SmallString::new();

        s.push('🦀'); // 4 bytes
        assert!(s.is_on_stack());
        assert_eq!(s.len(), 4);

        // Push another emoji. 4 + 4 = 8 bytes. Spill required.
        // This tests the `push(char)` spill path with multibyte chars.
        s.push('🚀');

        assert!(!s.is_on_stack());
        assert_eq!(s.as_str(), "🦀🚀");
        assert_eq!(s.len(), 8);
    }

    #[test]
    fn test_string_spill_trigger_on_multibyte_str() {
        // Test `push_str` splitting logic (though we copy fully)
        let mut s: SmallString<5> = SmallString::new();
        s.push_str("hi");

        // Push mixed ascii and unicode that causes spill
        // "hi" (2) + "_👋" (1 + 4) = 7 bytes total
        s.push_str("_👋");

        assert!(!s.is_on_stack());
        assert_eq!(s.as_str(), "hi_👋");
    }

    // --- 6. Formatting Macro (fmt::Write) ---
    #[test]
    fn test_string_traits_fmt_write() {
        let mut s: SmallString<16> = SmallString::new();

        // Should fit on stack
        write!(s, "Value: {}", 100).unwrap();
        assert_eq!(s.as_str(), "Value: 100");
        assert!(s.is_on_stack());

        // Should cause spill
        write!(s, " and a very long suffix to force spill").unwrap();
        assert!(!s.is_on_stack());
        assert!(s.contains("Value: 100"));
        assert!(s.contains("suffix"));
    }

    // --- 7. Large Allocation Strategy ---
    #[test]
    fn test_string_spill_trigger_on_large_growth() {
        let mut s: SmallString<4> = SmallString::new();
        s.push_str("12");

        // Push a huge string immediately
        // The spill logic `max(needed, N*2)` should handle this without crashing
        let huge_chunk = "a".repeat(100);
        s.push_str(&huge_chunk);

        assert!(!s.is_on_stack());
        assert_eq!(s.len(), 102);
        assert!(s.ends_with("aaaa"));
    }

    // --- 8. Zero Capacity Edge Case ---
    #[test]
    fn test_string_any_storage_zero_capacity() {
        // A SmallString that holds nothing on the stack
        let mut s: SmallString<0> = SmallString::new();

        assert!(s.is_on_stack()); // Technically starts on "stack" (empty)

        s.push('a');
        assert!(!s.is_on_stack()); // Immediate spill
        assert_eq!(s.as_str(), "a");
    }

    // --- 9. Clear & Reuse ---
    #[test]
    fn test_string_any_storage_clear_reuse() {
        let mut s: SmallString<4> = SmallString::new();

        // Stack -> Clear -> Stack
        s.push_str("abc");
        s.clear();
        assert!(s.is_empty());
        assert!(s.is_on_stack());
        s.push_str("xyz");
        assert_eq!(s.as_str(), "xyz");

        // Stack -> Spill -> Clear -> Heap (Remains on heap usually)
        s.push_str("12345"); // Spill
        assert!(!s.is_on_stack());

        s.clear();
        assert!(s.is_empty());
        assert!(!s.is_on_stack()); // Implementation detail: keeps heap allocation

        // Reuse heap allocation
        s.push_str("HeapReuse");
        assert_eq!(s.as_str(), "HeapReuse");
    }

    // --- 10. Deref Methods ---
    #[test]
    fn test_string_traits_deref_methods() {
        let s: SmallString<10> = SmallString::from("hello");

        // These methods come from standard `str`, working via Deref
        assert!(s.starts_with("he"));
        assert!(s.contains("ll"));
        assert_eq!(s.to_uppercase(), "HELLO"); // Returns a new Std String
        assert_eq!(s.find('o'), Some(4));
    }

    #[test]
    fn test_string_traits_clone_on_stack() {
        // N=16. "Hello" (5 bytes) fits on stack.
        let mut original: SmallString<16> = SmallString::new();
        original.push_str("Hello");

        assert!(original.is_on_stack());

        // Clone it
        let mut copy = original.clone();

        // Verify state
        assert!(copy.is_on_stack());
        assert_eq!(original.as_str(), copy.as_str());

        // Modify Copy -> Ensure Original is untouched
        copy.push_str(" World");

        assert_eq!(original.as_str(), "Hello");
        assert_eq!(copy.as_str(), "Hello World");
    }

    #[test]
    fn test_string_traits_clone_on_heap() {
        // N=4. "Hello" (5 bytes) forces a spill to Heap.
        let mut original: SmallString<4> = SmallString::new();
        original.push_str("Hello");

        assert!(!original.is_on_stack());

        // Clone it
        let mut copy = original.clone();

        // Verify state
        assert!(!copy.is_on_stack());
        assert_eq!(original.as_str(), copy.as_str());

        // Modify Copy -> Ensure Original is untouched
        copy.push_str(" World");

        assert_eq!(original.as_str(), "Hello");
        assert_eq!(copy.as_str(), "Hello World");
    }

    // --- Helper to verify Hashing consistency ---
    fn calculate_hash<T: Hash>(t: &T) -> u64 {
        let mut s = DefaultHasher::new();
        t.hash(&mut s);
        s.finish()
    }

    // --- 1. Equality & Ordering Tests ---
    #[test]
    fn test_string_traits_equality() {
        // Stack vs Stack
        let s1: SmallString<16> = SmallString::from("hello");
        let s2: SmallString<16> = SmallString::from("hello");
        let s3: SmallString<16> = SmallString::from("world");
        assert_eq!(s1, s2);
        assert_ne!(s1, s3);

        // Heap vs Heap (N=2 forces spill)
        let h1: SmallString<2> = SmallString::from("hello");
        let h2: SmallString<2> = SmallString::from("hello");
        assert_eq!(h1, h2);
        assert!(!h1.is_on_stack());

        // Stack vs Heap
        // s1 is on stack, h1 is on heap. Content is same.
        assert_eq!(s1, h1);

        // Comparison with &str and String
        assert_eq!(s1, "hello");
        assert_eq!("hello", s1);
        assert_eq!(s1, String::from("hello"));
    }

    #[test]
    fn test_string_traits_ordering() {
        let apple: SmallString<16> = SmallString::from("Apple");
        let banana: SmallString<16> = SmallString::from("Banana");

        // "Apple" < "Banana"
        assert!(apple < banana);

        // Sort a vector of SmallStrings
        let mut list = vec![banana.clone(), apple.clone()];
        list.sort();

        assert_eq!(list[0], "Apple");
        assert_eq!(list[1], "Banana");
    }

    // --- 2. Hashing Tests ---
    #[test]
    fn test_string_traits_hashing() {
        let s_stack: SmallString<16> = SmallString::from("testing");
        let s_heap: SmallString<2> = SmallString::from("testing"); // Spills
        let s_std: String = String::from("testing");
        let s_str: &str = "testing";

        // IMPORTANT: The hash must be identical to standard Rust strings
        // for HashMap interoperability.
        let h1 = calculate_hash(&s_stack);
        let h2 = calculate_hash(&s_heap);
        let h3 = calculate_hash(&s_std);
        let h4 = calculate_hash(&s_str);

        assert_eq!(h1, h2, "Stack and Heap hashing differ!");
        assert_eq!(h1, h3, "SmallString hash differs from String hash!");
        assert_eq!(h1, h4, "SmallString hash differs from &str hash!");

        // Verify use in HashSet
        let mut set = HashSet::new();
        set.insert(s_stack.clone());

        assert!(set.contains("testing")); // Look up using &str
        assert!(set.contains(s_heap.as_str())); // Look up using Heap SmallString
    }

    // --- 3. Borrow & AsRef Tests ---
    #[test]
    fn test_string_traits_borrow_as_ref() {
        let s: SmallString<16> = SmallString::from("hello");

        // Function expecting &str
        fn takes_str(_: &str) {}
        takes_str(s.as_ref());
        takes_str(s.borrow());

        // Function expecting AsRef<str>
        fn takes_as_ref<T: AsRef<str>>(_: T) {}
        takes_as_ref(s); // Pass by value
    }

    // --- 4. Iterator Tests (FromIterator / Extend) ---
    #[test]
    fn test_string_traits_from_iterator() {
        // Collect chars -> Stack
        let chars = vec!['a', 'b', 'c'];
        let s_stack: SmallString<16> = chars.into_iter().collect();
        assert_eq!(s_stack, "abc");
        assert!(s_stack.is_on_stack());

        // Collect chars -> Heap
        let many_chars = vec!['a'; 100];
        let s_heap: SmallString<16> = many_chars.into_iter().collect();
        assert_eq!(s_heap.len(), 100);
        assert!(!s_heap.is_on_stack());

        // Collect strings
        let strings = vec!["Hello", " ", "World"];
        let s_str: SmallString<32> = strings.into_iter().collect();
        assert_eq!(s_str, "Hello World");
    }

    #[test]
    fn test_string_traits_extend() {
        let mut s: SmallString<4> = SmallString::new();

        // Extend a little (Stack)
        s.extend(vec!['H', 'i']);
        assert_eq!(s, "Hi");
        assert!(s.is_on_stack());

        // Extend a lot (Spill)
        s.extend(vec!['!'; 20]);
        assert!(!s.is_on_stack());
        assert_eq!(s.len(), 22);
    }

    // --- 5. Manipulation Tests (Pop / Truncate) ---
    #[test]
    fn test_string_any_storage_pop() {
        // Stack Pop
        let mut s: SmallString<16> = SmallString::from("abc");
        assert_eq!(s.pop(), Some('c'));
        assert_eq!(s.pop(), Some('b'));
        assert_eq!(s, "a");

        // Heap Pop
        let mut h: SmallString<2> = SmallString::from("abc"); // Spills
        assert!(!h.is_on_stack());
        assert_eq!(h.pop(), Some('c'));
        assert_eq!(h, "ab");

        // Empty Pop
        let mut empty: SmallString<4> = SmallString::new();
        assert_eq!(empty.pop(), None);
    }

    #[test]
    fn test_string_any_storage_truncate_v2() {
        // Stack Truncate
        let mut s: SmallString<16> = SmallString::from("Hello World");
        s.truncate(5);
        assert_eq!(s, "Hello");
        assert_eq!(s.len(), 5);

        // Truncate to larger size (Should do nothing)
        s.truncate(100);
        assert_eq!(s, "Hello");

        // Heap Truncate
        let mut h: SmallString<2> = SmallString::from("Hello World"); // Spills
        assert!(!h.is_on_stack());
        h.truncate(2);
        assert_eq!(h, "He");
    }

    #[test]
    #[should_panic]
    fn test_string_any_storage_truncate_panic() {
        let mut s: SmallString<16> = SmallString::from("🦀"); // 4 bytes
        // Truncating inside a multibyte char panics in std, should panic here too
        s.truncate(2);
    }

    // --- 6. Reserve (Spill Control) ---
    #[test]
    fn test_string_any_storage_reserve() {
        // Case 1: Reserve fits in Stack
        let mut s: SmallString<16> = SmallString::new();
        s.push_str("Hi");
        s.reserve(4); // 2 + 4 = 6 <= 16.
        assert!(s.is_on_stack());

        // Case 2: Reserve forces Spill
        // We are at len 2. We request 20 more. Total 22 > 16.
        // This should force a move to heap.
        s.reserve(20);

        assert!(!s.is_on_stack());
        assert_eq!(s.as_str(), "Hi");
        assert!(s.capacity() >= 22); // Correct: 2 + 20

        // Case 3: Reserve on existing Heap
        // We request 100 MORE bytes than current length (2).
        // Total required = 102.
        s.reserve(100);

        // FIX: Check against 102, not 122.
        assert!(s.capacity() >= 102);
    }

    #[test]
    fn test_string_any_storage_into_string() {
        // Stack -> String
        let s_stack: SmallString<16> = SmallString::from("stack");
        let std_str = s_stack.into_string();
        assert_eq!(std_str, "stack");
        // Verify type is actually String
        let _: String = std_str;

        // Heap -> String (Zero cost move)
        let s_heap: SmallString<2> = SmallString::from("heap");
        let std_str2 = s_heap.into_string();
        assert_eq!(std_str2, "heap");
    }

    #[test]
    fn test_string_any_storage_from_utf8() {
        let bytes = vec![104, 101, 108, 108, 111]; // "hello"

        // Fits on Stack
        let s: SmallString<16> = SmallString::from_utf8(bytes.clone()).unwrap();
        assert!(s.is_on_stack());
        assert_eq!(s, "hello");

        // Force Heap
        let s2: SmallString<2> = SmallString::from_utf8(bytes).unwrap();
        assert!(!s2.is_on_stack());
        assert_eq!(s2, "hello");

        // Invalid UTF-8
        let invalid = vec![0, 159, 146, 150];
        assert!(SmallString::<16>::from_utf8(invalid).is_err());
    }

    #[test]
    fn test_string_any_storage_retain() {
        // Stack Retain
        let mut s: SmallString<16> = SmallString::from("AbCdEf");
        s.retain(|c| c.is_lowercase());
        assert_eq!(s, "bdf");
        assert!(s.is_on_stack());

        // Heap Retain
        let mut h: SmallString<2> = SmallString::from("AbCdEf"); // Spills
        h.retain(|c| c.is_uppercase());
        assert_eq!(h, "ACE");
        assert!(!h.is_on_stack());
    }

    #[test]
    fn test_string_any_storage_into_bytes() {
        let s: SmallString<16> = SmallString::from("ABC");
        let bytes = s.into_bytes();
        assert_eq!(bytes, vec![65, 66, 67]);
    }

    #[test]
    fn test_string_any_storage_from_utf8_lossy() {
        let bytes = b"hello \xF0\x90\x80world"; // Invalid UTF-8
        let s: SmallString<16> = SmallString::from_utf8_lossy(bytes);
        assert!(s.contains("hello "));
        assert!(s.contains("world"));
        assert!(s.is_on_stack());

        let huge_invalid = b"a".repeat(100);
        let s2: SmallString<16> = SmallString::from_utf8_lossy(&huge_invalid);
        assert!(!s2.is_on_stack());
    }

    #[test]
    fn test_string_any_storage_as_bytes_mut() {
        let mut s: SmallString<16> = SmallString::from("abc");
        unsafe {
            let bytes = s.as_bytes_mut();
            bytes[0] = b'z';
        }
        assert_eq!(s, "zbc");
    }

    #[test]
    fn test_string_traits_debug_display() {
        let h: SmallString<2> = SmallString::from("a");
        let debug = format!("{:?}", h);
        assert_eq!(debug, "\"a\"");
        let display = format!("{}", h);
        assert_eq!(display, "a");
    }

    #[test]
    fn test_string_any_storage_truncate() {
        // truncate stack
        let mut s: SmallString<16> = SmallString::from("hello");
        s.truncate(2);
        assert_eq!(s, "he");

        // truncate heap
        let mut h: SmallString<2> = SmallString::from("abc");
        h.truncate(1);
        assert_eq!(h, "a");
    }

    #[test]
    fn test_string_any_storage_deref_mut() {
        let mut s_mut = SmallString::<16>::from("abc");
        s_mut.as_mut_str().make_ascii_uppercase();
        assert_eq!(s_mut, "ABC");

        let mut h: SmallString<1> = SmallString::from_str("abc");
        h.as_mut_str().make_ascii_uppercase();
        assert_eq!(h, "ABC");
    }

    #[test]
    fn test_string_any_storage_partial_eq_variants() {
        let h: SmallString<1> = SmallString::from_str("abc");
        assert!(h == "abc");
        let s_ref = "abc";
        assert!(h == s_ref);
    }

    #[test]
    fn test_string_any_storage_extend_char() {
        let mut h: SmallString<1> = SmallString::from_str("abc");
        h.extend(['!', '?'].iter().cloned());
        assert_eq!(h, "abc!?");
    }

    #[test]
    fn test_string_any_storage_from_utf8_long() {
        let long_bytes = b"a".repeat(100);
        let s_long = SmallString::<16>::from_utf8(long_bytes).unwrap();
        assert!(!s_long.is_on_stack());
    }
}

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

    #[test]
    fn test_any_string_trait_std_string_implementation() {
        let mut s = String::from("abc");
        let any: &mut dyn AnyString = &mut s;
        assert_eq!(any.as_str(), "abc");
        assert_eq!(any.len(), 3);
        assert!(!any.is_empty());
        any.push('d');
        any.push_str("ef");
        assert_eq!(any.as_str(), "abcdef");
        assert_eq!(any.pop(), Some('f'));
        any.truncate(3);
        assert_eq!(any.as_str(), "abc");
        any.clear();
        assert!(any.is_empty());
    }

    #[test]
    fn test_any_string_trait_small_string_implementation() {
        let mut s: SmallString<4> = SmallString::new();
        let any: &mut dyn AnyString = &mut s;
        any.push_str("abc");
        assert_eq!(any.len(), 3);
        any.push('d');
        assert_eq!(any.as_str(), "abcd");
        any.pop();
        any.truncate(1);
        any.clear();
        assert!(any.is_empty());
    }

    #[test]
    fn test_small_string_heap_storage_utf8_validity_and_retain() {
        let mut s: SmallString<2> = SmallString::new();
        s.push_str("abc"); // heap
        assert!(!s.is_on_stack());
        assert!(s.capacity() >= 2);

        // from_utf8 heap
        let long_vec = vec![b'a'; 10];
        let s2 = SmallString::<2>::from_utf8(long_vec).unwrap();
        assert!(!s2.is_on_stack());

        // from_utf8_lossy heap
        let s3 = SmallString::<2>::from_utf8_lossy(&[b'a'; 10]);
        assert!(!s3.is_on_stack());

        // retain heap
        let mut s4: SmallString<2> = SmallString::from("abcde");
        s4.retain(|c| c != 'b');
        assert_eq!(s4.as_str(), "acde");

        // shrink_to_fit heap
        s4.shrink_to_fit();
    }

    #[test]
    fn test_small_string_formatting_comparison_and_extension() {
        let mut s: SmallString<4> = SmallString::from("abc");

        // Display / Debug
        assert_eq!(format!("{}", s), "abc");
        assert_eq!(format!("{:?}", s), "\"abc\"");

        // PartialEq / PartialOrd
        let s2 = SmallString::<8>::from("abc");
        assert_eq!(s, s2);
        assert_eq!(s, "abc");
        assert_eq!("abc", s);
        assert_eq!(s, String::from("abc"));

        let s3 = SmallString::<4>::from("abd");
        assert!(s < s3);
        assert_eq!(s.cmp(&s3), std::cmp::Ordering::Less);

        // Hash
        let mut h = std::collections::hash_map::DefaultHasher::new();
        s.hash(&mut h);

        // Borrow / AsRef
        let b: &str = s.borrow();
        assert_eq!(b, "abc");
        let b_mut: &mut str = s.borrow_mut();
        b_mut.make_ascii_uppercase();
        assert_eq!(s.as_str(), "ABC");

        let r: &str = s.as_ref();
        assert_eq!(r, "ABC");
        let r_bytes: &[u8] = s.as_ref();
        assert_eq!(r_bytes, b"ABC");

        // Extend / FromIterator
        let mut s4: SmallString<4> = SmallString::new();
        s4.extend(['a', 'b']);
        s4.extend(["cd", "ef"]); // heap
        assert_eq!(s4.as_str(), "abcdef");

        let s5 = SmallString::<4>::from_iter(['x', 'y']);
        assert_eq!(s5.as_str(), "xy");
        let s6 = SmallString::<4>::from_iter(["hi", "ho"]);
        assert_eq!(s6.as_str(), "hiho");
    }

    #[test]
    fn test_small_string_fmt_write_trait() {
        use std::fmt::Write;
        let mut s: SmallString<4> = SmallString::new();
        write!(s, "{}", 12345).unwrap();
        assert_eq!(s.as_str(), "12345");
    }
}