arcstr 1.2.0

A better reference-counted string type, with zero-cost (allocation-free) support for string literals, and reference counted substrings.
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
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
#![allow(
// We follow libstd's lead and prefer to define both.
    clippy::partialeq_ne_impl,
// This is a really annoying clippy lint, since it's required for so many cases...
    clippy::cast_ptr_alignment,
// For macros
    clippy::redundant_slicing,
)]
use core::alloc::Layout;
use core::mem::{align_of, size_of, MaybeUninit};
use core::ptr::NonNull;
#[cfg(not(all(loom, test)))]
pub(crate) use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(all(loom, test))]
pub(crate) use loom::sync::atomic::{AtomicUsize, Ordering};

#[cfg(feature = "substr")]
use crate::Substr;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;

/// A better atomically-reference counted string type.
///
/// ## Benefits of `ArcStr` over `Arc<str>`
///
/// - It's possible to create a const `ArcStr` from a literal via the
///   [`arcstr::literal!`][crate::literal] macro. This is probably the killer
///   feature, to be honest.
///
///   These "static" `ArcStr`s are zero cost, take no heap allocation, and don't
///   even need to perform atomic reads/writes when being cloned or dropped (nor
///   at any other time).
///
///   They even get stored in the read-only memory of your executable, which can
///   be beneficial for performance and memory usage. (In theory your linker may
///   even dedupe these for you, but usually not)
///
/// - `ArcStr`s from `arcstr::literal!` can be turned into `&'static str` safely
///   at any time using [`ArcStr::as_static`]. (This returns an Option, which is
///   `None` if the `ArcStr` was not static)
///
/// - This should be unsurprising given the literal functionality, but
///   [`ArcStr::new`] is able to be a `const` function.
///
/// - `ArcStr` is thin, e.g. only a single pointer. Great for cases where you
///   want to keep the data structure lightweight or need to do some FFI stuff
///   with it.
///
/// - `ArcStr` is totally immutable. No need to lose sleep because you're afraid
///   of code which thinks it has a right to mutate your `Arc`s just because it
///   holds the only reference...
///
/// - Lower reference counting operations are lower overhead because we don't
///   support `Weak` references. This can be a drawback for some use cases, but
///   improves performance for the common case of no-weak-refs.
///
/// ## What does "zero-cost literals" mean?
///
/// In a few places I call the literal arcstrs "zero-cost". No overhead most
/// accesses accesses (aside from stuff like `as_static` which obviously
/// requires it). and it imposes a extra branch in both `clone` and `drop`.
///
/// This branch in `clone`/`drop` is not on the result of an atomic load, and is
/// just a normal memory read. This is actually what allows literal/static
/// `ArcStr`s to avoid needing to perform any atomic operations in those
/// functions, which seems likely more than cover the cost.
///
/// (Additionally, it's almost certain that in the future we'll be able to
/// reduce the synchronization required for atomic instructions. This is due to
/// our guarantee of immutability and lack of support for `Weak`.)
///
/// # Usage
///
/// ## As a `const`
///
/// The big unique feature of `ArcStr` is the ability to create static/const
/// `ArcStr`s. (See [the macro](crate::literal) docs or the [feature
/// overview][feats]
///
/// [feats]: index.html#feature-overview
///
/// ```
/// # use arcstr::ArcStr;
/// const WOW: ArcStr = arcstr::literal!("cool robot!");
/// assert_eq!(WOW, "cool robot!");
/// ```
///
/// ## As a `str`
///
/// (This is not unique to `ArcStr`, but is a frequent source of confusion I've
/// seen): `ArcStr` implements `Deref<Target = str>`, and so all functions and
/// methods from `str` work on it, even though we don't expose them on `ArcStr`
/// directly.
///
/// ```
/// # use arcstr::ArcStr;
/// let s = ArcStr::from("something");
/// // These go through `Deref`, so they work even though
/// // there is no `ArcStr::eq_ignore_ascii_case` function
/// assert!(s.eq_ignore_ascii_case("SOMETHING"));
/// ```
///
/// Additionally, `&ArcStr` can be passed to any function which accepts `&str`.
/// For example:
///
/// ```
/// # use arcstr::ArcStr;
/// fn accepts_str(s: &str) {
///    # let _ = s;
///     // s...
/// }
///
/// let test_str: ArcStr = "test".into();
/// // This works even though `&test_str` is normally an `&ArcStr`
/// accepts_str(&test_str);
///
/// // Of course, this works for functionality from the standard library as well.
/// let test_but_loud = ArcStr::from("TEST");
/// assert!(test_str.eq_ignore_ascii_case(&test_but_loud));
/// ```

#[repr(transparent)]
pub struct ArcStr(NonNull<ThinInner>);

unsafe impl Sync for ArcStr {}
unsafe impl Send for ArcStr {}

impl ArcStr {
    /// Construct a new empty string.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::new();
    /// assert_eq!(s, "");
    /// ```
    #[inline]
    pub const fn new() -> Self {
        EMPTY
    }

    /// Attempt to copy the provided string into a newly allocated `ArcStr`, but
    /// return `None` if we cannot allocate the required memory.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    ///
    /// # fn do_stuff_with(s: ArcStr) {}
    ///
    /// let some_big_str = "please pretend this is a very long string";
    /// if let Some(s) = ArcStr::try_alloc(some_big_str) {
    ///     do_stuff_with(s);
    /// } else {
    ///     // Complain about allocation failure, somehow.
    /// }
    /// ```
    #[inline]
    pub fn try_alloc(copy_from: &str) -> Option<Self> {
        if let Ok(inner) = ThinInner::try_allocate(copy_from, false) {
            Some(Self(inner))
        } else {
            None
        }
    }

    /// Attempt to allocate memory for an [`ArcStr`] of length `n`, and use the
    /// provided callback to fully initialize the provided buffer with valid
    /// UTF-8 text.
    ///
    /// This function returns `None` if memory allocation fails, see
    /// [`ArcStr::init_with_unchecked`] for a version which calls
    /// [`handle_alloc_error`](alloc::alloc::handle_alloc_error).
    ///
    /// # Safety
    /// The provided `initializer` callback must fully initialize the provided
    /// buffer with valid UTF-8 text.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// # use core::mem::MaybeUninit;
    /// let arcstr = unsafe {
    ///     ArcStr::try_init_with_unchecked(10, |s: &mut [MaybeUninit<u8>]| {
    ///         s.fill(MaybeUninit::new(b'a'));
    ///     }).unwrap()
    /// };
    /// assert_eq!(arcstr, "aaaaaaaaaa")
    /// ```
    #[inline]
    pub unsafe fn try_init_with_unchecked<F>(n: usize, initializer: F) -> Option<Self>
    where
        F: FnOnce(&mut [MaybeUninit<u8>]),
    {
        if let Ok(inner) = ThinInner::try_allocate_with(n, false, AllocInit::Uninit, initializer) {
            Some(Self(inner))
        } else {
            None
        }
    }

    /// Allocate memory for an [`ArcStr`] of length `n`, and use the provided
    /// callback to fully initialize the provided buffer with valid UTF-8 text.
    ///
    /// This function calls
    /// [`handle_alloc_error`](alloc::alloc::handle_alloc_error) if memory
    /// allocation fails, see [`ArcStr::try_init_with_unchecked`] for a version
    /// which returns `None`
    ///
    /// # Safety
    /// The provided `initializer` callback must fully initialize the provided
    /// buffer with valid UTF-8 text.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// # use core::mem::MaybeUninit;
    /// let arcstr = unsafe {
    ///     ArcStr::init_with_unchecked(10, |s: &mut [MaybeUninit<u8>]| {
    ///         s.fill(MaybeUninit::new(b'a'));
    ///     })
    /// };
    /// assert_eq!(arcstr, "aaaaaaaaaa")
    /// ```
    #[inline]
    pub unsafe fn init_with_unchecked<F>(n: usize, initializer: F) -> Self
    where
        F: FnOnce(&mut [MaybeUninit<u8>]),
    {
        match ThinInner::try_allocate_with(n, false, AllocInit::Uninit, initializer) {
            Ok(inner) => Self(inner),
            Err(None) => panic!("capacity overflow"),
            Err(Some(layout)) => alloc::alloc::handle_alloc_error(layout),
        }
    }

    /// Attempt to allocate memory for an [`ArcStr`] of length `n`, and use the
    /// provided callback to initialize the provided (initially-zeroed) buffer
    /// with valid UTF-8 text.
    ///
    /// Note: This function is provided with a zeroed buffer, and performs UTF-8
    /// validation after calling the initializer. While both of these are fast
    /// operations, some high-performance use cases will be better off using
    /// [`ArcStr::try_init_with_unchecked`] as the building block.
    ///
    /// # Errors
    /// The provided `initializer` callback must initialize the provided buffer
    /// with valid UTF-8 text, or a UTF-8 error will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    ///
    /// let s = ArcStr::init_with(5, |slice| {
    ///     slice
    ///         .iter_mut()
    ///         .zip(b'0'..b'5')
    ///         .for_each(|(db, sb)| *db = sb);
    /// }).unwrap();
    /// assert_eq!(s, "01234");
    /// ```
    #[inline]
    pub fn init_with<F>(n: usize, initializer: F) -> Result<Self, core::str::Utf8Error>
    where
        F: FnOnce(&mut [u8]),
    {
        let mut failed = None::<core::str::Utf8Error>;
        let wrapper = |zeroed_slice: &mut [MaybeUninit<u8>]| {
            debug_assert_eq!(n, zeroed_slice.len());
            // Safety: we pass `AllocInit::Zero`, so this is actually initialized
            let slice = unsafe {
                core::slice::from_raw_parts_mut(zeroed_slice.as_mut_ptr().cast::<u8>(), n)
            };
            initializer(slice);
            if let Err(e) = core::str::from_utf8(slice) {
                failed = Some(e);
            }
        };
        match unsafe { ThinInner::try_allocate_with(n, false, AllocInit::Zero, wrapper) } {
            Ok(inner) => {
                // Ensure we clean up the allocation even on error.
                let this = Self(inner);
                if let Some(e) = failed {
                    Err(e)
                } else {
                    Ok(this)
                }
            }
            Err(None) => panic!("capacity overflow"),
            Err(Some(layout)) => alloc::alloc::handle_alloc_error(layout),
        }
    }

    /// Extract a string slice containing our data.
    ///
    /// Note: This is an equivalent to our `Deref` implementation, but can be
    /// more readable than `&*s` in the cases where a manual invocation of
    /// `Deref` would be required.
    ///
    /// # Examples
    // TODO: find a better example where `&*` would have been required.
    /// ```
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::from("abc");
    /// assert_eq!(s.as_str(), "abc");
    /// ```
    #[inline]
    pub fn as_str(&self) -> &str {
        self
    }

    /// Returns the length of this `ArcStr` in bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let a = ArcStr::from("foo");
    /// assert_eq!(a.len(), 3);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.get_inner_len_flag().uint_part()
    }

    #[inline]
    fn get_inner_len_flag(&self) -> PackedFlagUint {
        unsafe { ThinInner::get_len_flag(self.0.as_ptr()) }
    }

    /// Returns true if this `ArcStr` is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// assert!(!ArcStr::from("foo").is_empty());
    /// assert!(ArcStr::new().is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Convert us to a `std::string::String`.
    ///
    /// This is provided as an inherent method to avoid needing to route through
    /// the `Display` machinery, but is equivalent to `ToString::to_string`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::from("abc");
    /// assert_eq!(s.to_string(), "abc");
    /// ```
    #[inline]
    #[allow(clippy::inherent_to_string_shadow_display)]
    pub fn to_string(&self) -> String {
        #[cfg(not(feature = "std"))]
        use alloc::borrow::ToOwned;
        self.as_str().to_owned()
    }

    /// Extract a byte slice containing the string's data.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let foobar = ArcStr::from("foobar");
    /// assert_eq!(foobar.as_bytes(), b"foobar");
    /// ```
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        let len = self.len();
        let p = self.0.as_ptr();
        unsafe {
            let data = p.cast::<u8>().add(OFFSET_DATA);
            debug_assert_eq!(core::ptr::addr_of!((*p).data).cast::<u8>(), data);
            core::slice::from_raw_parts(data, len)
        }
    }

    /// Return the raw pointer this `ArcStr` wraps, for advanced use cases.
    ///
    /// Note that in addition to the `NonNull` constraint expressed in the type
    /// signature, we also guarantee the pointer has an alignment of at least 8
    /// bytes, even on platforms where a lower alignment would be acceptable.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::from("abcd");
    /// let p = ArcStr::into_raw(s);
    /// // Some time later...
    /// let s = unsafe { ArcStr::from_raw(p) };
    /// assert_eq!(s, "abcd");
    /// ```
    #[inline]
    pub fn into_raw(this: Self) -> NonNull<()> {
        let p = this.0;
        core::mem::forget(this);
        p.cast()
    }

    /// The opposite version of [`Self::into_raw`]. Still intended only for
    /// advanced use cases.
    ///
    /// # Safety
    ///
    /// This function must be used on a valid pointer returned from
    /// [`ArcStr::into_raw`]. Additionally, you must ensure that a given `ArcStr`
    /// instance is only dropped once.
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::from("abcd");
    /// let p = ArcStr::into_raw(s);
    /// // Some time later...
    /// let s = unsafe { ArcStr::from_raw(p) };
    /// assert_eq!(s, "abcd");
    /// ```
    #[inline]
    pub unsafe fn from_raw(ptr: NonNull<()>) -> Self {
        Self(ptr.cast())
    }

    /// Returns true if the two `ArcStr`s point to the same allocation.
    ///
    /// Note that functions like `PartialEq` check this already, so there's
    /// no performance benefit to doing something like `ArcStr::ptr_eq(&a1, &a2) || (a1 == a2)`.
    ///
    /// Caveat: `const`s aren't guaranteed to only occur in an executable a
    /// single time, and so this may be non-deterministic for `ArcStr` defined
    /// in a `const` with [`arcstr::literal!`][crate::literal], unless one
    /// was created by a `clone()` on the other.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::ArcStr;
    ///
    /// let foobar = ArcStr::from("foobar");
    /// let same_foobar = foobar.clone();
    /// let other_foobar = ArcStr::from("foobar");
    /// assert!(ArcStr::ptr_eq(&foobar, &same_foobar));
    /// assert!(!ArcStr::ptr_eq(&foobar, &other_foobar));
    ///
    /// const YET_AGAIN_A_DIFFERENT_FOOBAR: ArcStr = arcstr::literal!("foobar");
    /// let strange_new_foobar = YET_AGAIN_A_DIFFERENT_FOOBAR.clone();
    /// let wild_blue_foobar = strange_new_foobar.clone();
    /// assert!(ArcStr::ptr_eq(&strange_new_foobar, &wild_blue_foobar));
    /// ```
    #[inline]
    pub fn ptr_eq(lhs: &Self, rhs: &Self) -> bool {
        core::ptr::eq(lhs.0.as_ptr(), rhs.0.as_ptr())
    }

    /// Returns the number of references that exist to this `ArcStr`. If this is
    /// a static `ArcStr` (For example, one from
    /// [`arcstr::literal!`][crate::literal]), returns `None`.
    ///
    /// Despite the difference in return type, this is named to match the method
    /// from the stdlib's Arc:
    /// [`Arc::strong_count`][alloc::sync::Arc::strong_count].
    ///
    /// If you aren't sure how to handle static `ArcStr` in the context of this
    /// return value, `ArcStr::strong_count(&s).unwrap_or(usize::MAX)` is
    /// frequently reasonable.
    ///
    /// # Safety
    ///
    /// This method by itself is safe, but using it correctly requires extra
    /// care. Another thread can change the strong count at any time, including
    /// potentially between calling this method and acting on the result.
    ///
    /// However, it may never change from `None` to `Some` or from `Some` to
    /// `None` for a given `ArcStr` — whether or not it is static is determined
    /// at construction, and never changes.
    ///
    /// # Examples
    ///
    /// ### Dynamic ArcStr
    /// ```
    /// # use arcstr::ArcStr;
    /// let foobar = ArcStr::from("foobar");
    /// assert_eq!(Some(1), ArcStr::strong_count(&foobar));
    /// let also_foobar = ArcStr::clone(&foobar);
    /// assert_eq!(Some(2), ArcStr::strong_count(&foobar));
    /// assert_eq!(Some(2), ArcStr::strong_count(&also_foobar));
    /// ```
    ///
    /// ### Static ArcStr
    /// ```
    /// # use arcstr::ArcStr;
    /// let baz = arcstr::literal!("baz");
    /// assert_eq!(None, ArcStr::strong_count(&baz));
    /// // Similarly:
    /// assert_eq!(None, ArcStr::strong_count(&ArcStr::default()));
    /// ```
    #[inline]
    pub fn strong_count(this: &Self) -> Option<usize> {
        let cf = Self::load_count_flag(this, Ordering::Acquire)?;
        if cf.flag_part() {
            None
        } else {
            Some(cf.uint_part())
        }
    }

    /// Safety: Unsafe to use `this` is stored in static memory (check
    /// `Self::has_static_lenflag`)
    #[inline]
    unsafe fn load_count_flag_raw(this: &Self, ord_if_needed: Ordering) -> PackedFlagUint {
        PackedFlagUint::from_encoded((*this.0.as_ptr()).count_flag.load(ord_if_needed))
    }

    #[inline]
    fn load_count_flag(this: &Self, ord_if_needed: Ordering) -> Option<PackedFlagUint> {
        if Self::has_static_lenflag(this) {
            None
        } else {
            let count_and_flag = PackedFlagUint::from_encoded(unsafe {
                (*this.0.as_ptr()).count_flag.load(ord_if_needed)
            });
            Some(count_and_flag)
        }
    }

    /// Convert the `ArcStr` into a "static" `ArcStr`, even if it was originally
    /// created from runtime values. The `&'static str` is returned.
    ///
    /// This is useful if you want to use [`ArcStr::as_static`] or
    /// [`ArcStr::is_static`] on a value only known at runtime.
    ///
    /// If the `ArcStr` is already static, then this is a noop.
    ///
    /// # Caveats
    /// Calling this function on an ArcStr will cause us to never free it, thus
    /// leaking it's memory. Doing this excessively can lead to problems.
    ///
    /// # Examples
    /// ```no_run
    /// # // This isn't run because it needs a leakcheck suppression,
    /// # // which I can't seem to make work in CI (no symbols for
    /// # // doctests?). Instead, we test this in tests/arc_str.rs
    /// # use arcstr::ArcStr;
    /// let s = ArcStr::from("foobar");
    /// assert!(!ArcStr::is_static(&s));
    /// assert!(ArcStr::as_static(&s).is_none());
    ///
    /// let leaked: &'static str = s.leak();
    /// assert_eq!(leaked, s);
    /// assert!(ArcStr::is_static(&s));
    /// assert_eq!(ArcStr::as_static(&s), Some("foobar"));
    /// ```
    #[inline]
    pub fn leak(&self) -> &'static str {
        if Self::has_static_lenflag(self) {
            return unsafe { Self::to_static_unchecked(self) };
        }
        let is_static_count = unsafe {
            // Not sure about ordering, maybe relaxed would be fine.
            Self::load_count_flag_raw(self, Ordering::Acquire)
        };
        if is_static_count.flag_part() {
            return unsafe { Self::to_static_unchecked(self) };
        }
        unsafe { Self::become_static(self, is_static_count.uint_part() == 1) };
        debug_assert!(Self::is_static(self));
        unsafe { Self::to_static_unchecked(self) }
    }

    unsafe fn become_static(this: &Self, is_unique: bool) {
        if is_unique {
            core::ptr::addr_of_mut!((*this.0.as_ptr()).count_flag).write(AtomicUsize::new(
                PackedFlagUint::new_raw(true, 1).encoded_value(),
            ));
            let lenp = core::ptr::addr_of_mut!((*this.0.as_ptr()).len_flag);
            debug_assert!(!lenp.read().flag_part());
            lenp.write(lenp.read().with_flag(true));
        } else {
            let flag_bit = PackedFlagUint::new_raw(true, 0).encoded_value();
            let atomic_count_flag = &*core::ptr::addr_of!((*this.0.as_ptr()).count_flag);
            atomic_count_flag.fetch_or(flag_bit, Ordering::Release);
        }
    }

    #[inline]
    unsafe fn to_static_unchecked(this: &Self) -> &'static str {
        &*Self::str_ptr(this)
    }

    #[inline]
    fn bytes_ptr(this: &Self) -> *const [u8] {
        let len = this.get_inner_len_flag().uint_part();
        unsafe {
            let p: *const ThinInner = this.0.as_ptr();
            let data = p.cast::<u8>().add(OFFSET_DATA);
            debug_assert_eq!(core::ptr::addr_of!((*p).data).cast::<u8>(), data,);
            core::ptr::slice_from_raw_parts(data, len)
        }
    }

    #[inline]
    fn str_ptr(this: &Self) -> *const str {
        Self::bytes_ptr(this) as *const str
    }

    /// Returns true if `this` is a "static" ArcStr. For example, if it was
    /// created from a call to [`arcstr::literal!`][crate::literal]),
    /// returned by `ArcStr::new`, etc.
    ///
    /// Static `ArcStr`s can be converted to `&'static str` for free using
    /// [`ArcStr::as_static`], without leaking memory — they're static constants
    /// in the program (somewhere).
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// const STATIC: ArcStr = arcstr::literal!("Electricity!");
    /// assert!(ArcStr::is_static(&STATIC));
    ///
    /// let still_static = arcstr::literal!("Shocking!");
    /// assert!(ArcStr::is_static(&still_static));
    /// assert!(
    ///     ArcStr::is_static(&still_static.clone()),
    ///     "Cloned statics are still static"
    /// );
    ///
    /// let nonstatic = ArcStr::from("Grounded...");
    /// assert!(!ArcStr::is_static(&nonstatic));
    /// ```
    #[inline]
    pub fn is_static(this: &Self) -> bool {
        // We align this to 16 bytes and keep the `is_static` flags in the same
        // place. In theory this means that if `cfg(target_feature = "avx")`
        // (where aligned 16byte loads are atomic), the compiler *could*
        // implement this function using the equivalent of:
        // ```
        // let vec = _mm_load_si128(self.0.as_ptr().cast());
        // let mask = _mm_movemask_pd(_mm_srli_epi64(vac, 63));
        // mask != 0
        // ```
        // and that's all; one load, no branching. (I don't think it *does*, but
        // I haven't checked so I'll be optimistic and keep the `#[repr(align)]`
        // -- hey, maybe the CPU can peephole-optimize it).
        //
        // That said, unless I did it in asm, *I* can't implement it that way,
        // since Rust's semantics don't allow me to make that change
        // optimization on my own (that load isn't considered atomic, for
        // example).
        this.get_inner_len_flag().flag_part()
            || unsafe { Self::load_count_flag_raw(this, Ordering::Relaxed).flag_part() }
    }

    /// This is true for any `ArcStr` that has been static from the time when it
    /// was created. It's cheaper than `has_static_rcflag`.
    #[inline]
    fn has_static_lenflag(this: &Self) -> bool {
        this.get_inner_len_flag().flag_part()
    }

    /// Returns true if `this` is a "static"/`"literal"` ArcStr. For example, if
    /// it was created from a call to [`literal!`][crate::literal]), returned by
    /// `ArcStr::new`, etc.
    ///
    /// Static `ArcStr`s can be converted to `&'static str` for free using
    /// [`ArcStr::as_static`], without leaking memory — they're static constants
    /// in the program (somewhere).
    ///
    /// # Examples
    ///
    /// ```
    /// # use arcstr::ArcStr;
    /// const STATIC: ArcStr = arcstr::literal!("Electricity!");
    /// assert_eq!(ArcStr::as_static(&STATIC), Some("Electricity!"));
    ///
    /// // Note that they don't have to be consts, just made using `literal!`:
    /// let still_static = arcstr::literal!("Shocking!");
    /// assert_eq!(ArcStr::as_static(&still_static), Some("Shocking!"));
    /// // Cloning a static still produces a static.
    /// assert_eq!(ArcStr::as_static(&still_static.clone()), Some("Shocking!"));
    ///
    /// // But it won't work for strings from other sources.
    /// let nonstatic = ArcStr::from("Grounded...");
    /// assert_eq!(ArcStr::as_static(&nonstatic), None);
    /// ```
    #[inline]
    pub fn as_static(this: &Self) -> Option<&'static str> {
        if Self::is_static(this) {
            // We know static strings live forever, so they can have a static lifetime.
            Some(unsafe { &*(this.as_str() as *const str) })
        } else {
            None
        }
    }

    // Not public API. Exists so the `arcstr::literal` macro can call it.
    #[inline]
    #[doc(hidden)]
    pub const unsafe fn _private_new_from_static_data<B>(
        ptr: &'static StaticArcStrInner<B>,
    ) -> Self {
        Self(NonNull::new_unchecked(ptr as *const _ as *mut ThinInner))
    }

    /// `feature = "substr"` Returns a substr of `self` over the given range.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::{ArcStr, Substr};
    ///
    /// let a = ArcStr::from("abcde");
    /// let b: Substr = a.substr(2..);
    ///
    /// assert_eq!(b, "cde");
    /// ```
    ///
    /// # Panics
    /// If any of the following are untrue, we panic
    /// - `range.start() <= range.end()`
    /// - `range.end() <= self.len()`
    /// - `self.is_char_boundary(start) && self.is_char_boundary(end)`
    /// - These can be conveniently verified in advance using
    ///   `self.get(start..end).is_some()` if needed.
    #[cfg(feature = "substr")]
    #[inline]
    pub fn substr(&self, range: impl core::ops::RangeBounds<usize>) -> Substr {
        Substr::from_parts(self, range)
    }

    /// `feature = "substr"` Returns a [`Substr`] of self over the given `&str`.
    ///
    /// It is not rare to end up with a `&str` which holds a view into a
    /// `ArcStr`'s backing data. A common case is when using functionality that
    /// takes and returns `&str` and are entirely unaware of `arcstr`, for
    /// example: `str::trim()`.
    ///
    /// This function allows you to reconstruct a [`Substr`] from a `&str` which
    /// is a view into this `ArcStr`'s backing string.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::{ArcStr, Substr};
    /// let text = ArcStr::from("   abc");
    /// let trimmed = text.trim();
    /// let substr: Substr = text.substr_from(trimmed);
    /// assert_eq!(substr, "abc");
    /// // for illustration
    /// assert!(ArcStr::ptr_eq(substr.parent(), &text));
    /// assert_eq!(substr.range(), 3..6);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if `substr` isn't a view into our memory.
    ///
    /// Also panics if `substr` is a view into our memory but is >= `u32::MAX`
    /// bytes away from our start, if we're a 64-bit machine and
    /// `substr-usize-indices` is not enabled.
    #[cfg(feature = "substr")]
    pub fn substr_from(&self, substr: &str) -> Substr {
        if substr.is_empty() {
            return Substr::new();
        }

        let self_start = self.as_ptr() as usize;
        let self_end = self_start + self.len();

        let substr_start = substr.as_ptr() as usize;
        let substr_end = substr_start + substr.len();
        if substr_start < self_start || substr_end > self_end {
            out_of_range(self, &substr);
        }

        let index = substr_start - self_start;
        let end = index + substr.len();
        self.substr(index..end)
    }

    /// `feature = "substr"` If possible, returns a [`Substr`] of self over the
    /// given `&str`.
    ///
    /// This is a fallible version of [`ArcStr::substr_from`].
    ///
    /// It is not rare to end up with a `&str` which holds a view into a
    /// `ArcStr`'s backing data. A common case is when using functionality that
    /// takes and returns `&str` and are entirely unaware of `arcstr`, for
    /// example: `str::trim()`.
    ///
    /// This function allows you to reconstruct a [`Substr`] from a `&str` which
    /// is a view into this `ArcStr`'s backing string.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::{ArcStr, Substr};
    /// let text = ArcStr::from("   abc");
    /// let trimmed = text.trim();
    /// let substr: Option<Substr> = text.try_substr_from(trimmed);
    /// assert_eq!(substr.unwrap(), "abc");
    /// // `&str`s not derived from `self` will return None.
    /// let not_substr = text.try_substr_from("abc");
    /// assert!(not_substr.is_none());
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if `substr` is a view into our memory but is >= `u32::MAX` bytes
    /// away from our start, if we're a 64-bit machine and
    /// `substr-usize-indices` is not enabled.
    #[cfg(feature = "substr")]
    pub fn try_substr_from(&self, substr: &str) -> Option<Substr> {
        if substr.is_empty() {
            return Some(Substr::new());
        }

        let self_start = self.as_ptr() as usize;
        let self_end = self_start + self.len();

        let substr_start = substr.as_ptr() as usize;
        let substr_end = substr_start + substr.len();
        if substr_start < self_start || substr_end > self_end {
            return None;
        }

        let index = substr_start - self_start;
        let end = index + substr.len();
        debug_assert!(self.get(index..end).is_some());
        Some(self.substr(index..end))
    }

    /// `feature = "substr"` Compute a derived `&str` a function of `&str` =>
    /// `&str`, and produce a Substr of the result if possible.
    ///
    /// The function may return either a derived string, or any empty string.
    ///
    /// This function is mainly a wrapper around [`ArcStr::try_substr_from`]. If
    /// you're coming to `arcstr` from the `shared_string` crate, this is the
    /// moral equivalent of the `slice_with` function.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::{ArcStr, Substr};
    /// let text = ArcStr::from("   abc");
    /// let trimmed: Option<Substr> = text.try_substr_using(str::trim);
    /// assert_eq!(trimmed.unwrap(), "abc");
    /// let other = text.try_substr_using(|_s| "different string!");
    /// assert_eq!(other, None);
    /// // As a special case, this is allowed.
    /// let empty = text.try_substr_using(|_s| "");
    /// assert_eq!(empty.unwrap(), "");
    /// ```
    #[cfg(feature = "substr")]
    pub fn try_substr_using(&self, f: impl FnOnce(&str) -> &str) -> Option<Substr> {
        self.try_substr_from(f(self.as_str()))
    }

    /// `feature = "substr"` Compute a derived `&str` a function of `&str` =>
    /// `&str`, and produce a Substr of the result.
    ///
    /// The function may return either a derived string, or any empty string.
    /// Returning anything else will result in a panic.
    ///
    /// This function is mainly a wrapper around [`ArcStr::try_substr_from`]. If
    /// you're coming to `arcstr` from the `shared_string` crate, this is the
    /// likely closest to the `slice_with_unchecked` function, but this panics
    /// instead of UB on dodginess.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::{ArcStr, Substr};
    /// let text = ArcStr::from("   abc");
    /// let trimmed: Substr = text.substr_using(str::trim);
    /// assert_eq!(trimmed, "abc");
    /// // As a special case, this is allowed.
    /// let empty = text.substr_using(|_s| "");
    /// assert_eq!(empty, "");
    /// ```
    #[cfg(feature = "substr")]
    pub fn substr_using(&self, f: impl FnOnce(&str) -> &str) -> Substr {
        self.substr_from(f(self.as_str()))
    }

    /// Creates an `ArcStr` by repeating the source string `n` times
    ///
    /// # Errors
    ///
    /// This function returns an error if the capacity overflows or allocation
    /// fails.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcstr::ArcStr;
    ///
    /// let source = "A";
    /// let repeated = ArcStr::try_repeat(source, 10);
    /// assert_eq!(repeated.unwrap(), "AAAAAAAAAA");
    /// ```
    pub fn try_repeat(source: &str, n: usize) -> Option<Self> {
        // If the source string is empty or the user asked for zero repetitions,
        // return an empty string
        if source.is_empty() || n == 0 {
            return Some(Self::new());
        }

        // Calculate the capacity for the allocated string
        let capacity = source.len().checked_mul(n)?;
        let inner =
            ThinInner::try_allocate_maybe_uninit(capacity, false, AllocInit::Uninit).ok()?;

        unsafe {
            let mut data_ptr = ThinInner::data_ptr(inner);
            let data_end = data_ptr.add(capacity);

            // Copy `source` into the allocated string `n` times
            while data_ptr < data_end {
                core::ptr::copy_nonoverlapping(source.as_ptr(), data_ptr, source.len());
                data_ptr = data_ptr.add(source.len());
            }
        }

        Some(Self(inner))
    }

    /// Creates an `ArcStr` by repeating the source string `n` times
    ///
    /// # Panics
    ///
    /// This function panics if the capacity overflows, see
    /// [`try_repeat`](ArcStr::try_repeat) if this is undesirable.
    ///
    /// # Examples
    ///
    /// Basic usage:
    /// ```
    /// use arcstr::ArcStr;
    ///
    /// let source = "A";
    /// let repeated = ArcStr::repeat(source, 10);
    /// assert_eq!(repeated, "AAAAAAAAAA");
    /// ```
    ///
    /// A panic upon overflow:
    /// ```should_panic
    /// # use arcstr::ArcStr;
    ///
    /// // this will panic at runtime
    /// let huge = ArcStr::repeat("A", usize::MAX);
    /// ```
    pub fn repeat(source: &str, n: usize) -> Self {
        Self::try_repeat(source, n).expect("capacity overflow")
    }
}

#[cold]
#[inline(never)]
#[cfg(feature = "substr")]
fn out_of_range(arc: &ArcStr, substr: &&str) -> ! {
    let arc_start = arc.as_ptr();
    let arc_end = arc_start.wrapping_add(arc.len());
    let substr_start = substr.as_ptr();
    let substr_end = substr_start.wrapping_add(substr.len());
    panic!(
        "ArcStr over ({:p}..{:p}) does not contain substr over ({:p}..{:p})",
        arc_start, arc_end, substr_start, substr_end,
    );
}

impl Clone for ArcStr {
    #[inline]
    fn clone(&self) -> Self {
        if !Self::is_static(self) {
            // From libstd's impl:
            //
            // > Using a relaxed ordering is alright here, as knowledge of the
            // > original reference prevents other threads from erroneously deleting
            // > the object.
            //
            // See: https://doc.rust-lang.org/src/alloc/sync.rs.html#1073
            let n: PackedFlagUint = PackedFlagUint::from_encoded(unsafe {
                let step = PackedFlagUint::FALSE_ONE.encoded_value();
                (*self.0.as_ptr())
                    .count_flag
                    .fetch_add(step, Ordering::Relaxed)
            });
            // Protect against aggressive leaking of Arcs causing us to
            // overflow. Technically, we could probably transition it to static
            // here, but I haven't thought it through.
            if n.uint_part() > RC_MAX && !n.flag_part() {
                let val = PackedFlagUint::new_raw(true, 0).encoded_value();
                unsafe {
                    (*self.0.as_ptr())
                        .count_flag
                        .fetch_or(val, Ordering::Release)
                };
                // abort();
            }
        }
        Self(self.0)
    }
}
const RC_MAX: usize = PackedFlagUint::UINT_PART_MAX / 2;

impl Drop for ArcStr {
    #[inline]
    fn drop(&mut self) {
        if Self::is_static(self) {
            return;
        }
        unsafe {
            let this = self.0.as_ptr();
            let enc = PackedFlagUint::from_encoded(
                (*this)
                    .count_flag
                    .fetch_sub(PackedFlagUint::FALSE_ONE.encoded_value(), Ordering::Release),
            );
            // Note: `enc == PackedFlagUint::FALSE_ONE`
            if enc == PackedFlagUint::FALSE_ONE {
                let _ = (*this).count_flag.load(Ordering::Acquire);
                ThinInner::destroy_cold(this)
            }
        }
    }
}
// Caveat on the `static`/`strong` fields: "is_static" indicates if we're
// located in static data (as with empty string). is_static being false meanse
// we are a normal arc-ed string.
//
// While `ArcStr` claims to hold a pointer to a `ThinInner`, for the static case
// we actually are using a pointer to a `StaticArcStrInner<[u8; N]>`. These have
// almost identical layouts, except the static contains a explicit trailing
// array, and does not have a `AtomicUsize` The issue is: We kind of want the
// static ones to not have any interior mutability, so that `const`s can use
// them, and so that they may be stored in read-only memory.
//
// We do this by keeping a flag in `len_flag` flag to indicate which case we're
// in, and maintaining the invariant that if we're a `StaticArcStrInner` **we
// may never access `.strong` in any way or produce a `&ThinInner` pointing to
// our data**.
//
// This is more subtle than you might think, sinc AFAIK we're not legally
// allowed to create an `&ThinInner` until we're 100% sure it's nonstatic, and
// prior to determining it, we are forced to work from entirely behind a raw
// pointer...
//
// That said, a bit of this hoop jumping might be not required in the future,
// but for now what we're doing works and is apparently sound:
// https://github.com/rust-lang/unsafe-code-guidelines/issues/246
#[repr(C, align(8))]
struct ThinInner {
    // Both of these are `PackedFlagUint`s that store `is_static` as the flag.
    //
    // The reason it's not just stored in len is because an ArcStr may become
    // static after creation (via `ArcStr::leak`) and we don't need to do an
    // atomic load to access the length (and not only because it would mess with
    // optimization).
    //
    // The reason it's not just stored in the count is because it may be UB to
    // do atomic loads from read-only memory. This is also the reason it's not
    // stored in a separate atomic, and why doing an atomic load to access the
    // length wouldn't be acceptable even if compilers were really good.
    len_flag: PackedFlagUint,
    count_flag: AtomicUsize,
    data: [u8; 0],
}

const OFFSET_LENFLAGS: usize = 0;
const OFFSET_COUNTFLAGS: usize = size_of::<PackedFlagUint>();
const OFFSET_DATA: usize = OFFSET_COUNTFLAGS + size_of::<AtomicUsize>();

// Not public API, exists for macros.
#[repr(C, align(8))]
#[doc(hidden)]
pub struct StaticArcStrInner<Buf> {
    pub len_flag: usize,
    pub count_flag: usize,
    pub data: Buf,
}

impl<Buf> StaticArcStrInner<Buf> {
    #[doc(hidden)]
    pub const STATIC_COUNT_VALUE: usize = PackedFlagUint::new_raw(true, 1).encoded_value();
    #[doc(hidden)]
    #[inline]
    pub const fn encode_len(v: usize) -> Option<usize> {
        match PackedFlagUint::new(true, v) {
            Some(v) => Some(v.encoded_value()),
            None => None,
        }
    }
}

const _: [(); size_of::<StaticArcStrInner<[u8; 0]>>()] = [(); 2 * size_of::<usize>()];
const _: [(); align_of::<StaticArcStrInner<[u8; 0]>>()] = [(); 8];

const _: [(); size_of::<StaticArcStrInner<[u8; 2 * size_of::<usize>()]>>()] =
    [(); 4 * size_of::<usize>()];
const _: [(); align_of::<StaticArcStrInner<[u8; 2 * size_of::<usize>()]>>()] = [(); 8];

const _: [(); size_of::<ThinInner>()] = [(); 2 * size_of::<usize>()];
const _: [(); align_of::<ThinInner>()] = [(); 8];

const _: [(); align_of::<AtomicUsize>()] = [(); align_of::<usize>()];
const _: [(); align_of::<AtomicUsize>()] = [(); size_of::<usize>()];
const _: [(); size_of::<AtomicUsize>()] = [(); size_of::<usize>()];

const _: [(); align_of::<PackedFlagUint>()] = [(); align_of::<usize>()];
const _: [(); size_of::<PackedFlagUint>()] = [(); size_of::<usize>()];

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct PackedFlagUint(usize);
impl PackedFlagUint {
    const UINT_PART_MAX: usize = (1 << (usize::BITS - 1)) - 1;
    /// Encodes `false` as the flag and `1` as the uint. Used for a few things,
    /// such as the amount we `fetch_add` by for refcounting, and so on.
    const FALSE_ONE: Self = Self::new_raw(false, 1);

    #[inline]
    const fn new(flag_part: bool, uint_part: usize) -> Option<Self> {
        if uint_part > Self::UINT_PART_MAX {
            None
        } else {
            Some(Self::new_raw(flag_part, uint_part))
        }
    }

    #[inline(always)]
    const fn new_raw(flag_part: bool, uint_part: usize) -> Self {
        Self(flag_part as usize | (uint_part << 1))
    }

    #[inline(always)]
    const fn uint_part(self) -> usize {
        self.0 >> 1
    }

    #[inline(always)]
    const fn flag_part(self) -> bool {
        (self.0 & 1) != 0
    }

    #[inline(always)]
    const fn from_encoded(v: usize) -> Self {
        Self(v)
    }

    #[inline(always)]
    const fn encoded_value(self) -> usize {
        self.0
    }

    #[inline(always)]
    #[must_use]
    const fn with_flag(self, v: bool) -> Self {
        Self(v as usize | self.0)
    }
}

const EMPTY: ArcStr = literal!("");

impl ThinInner {
    #[inline]
    fn allocate(data: &str, initially_static: bool) -> NonNull<Self> {
        match Self::try_allocate(data, initially_static) {
            Ok(v) => v,
            Err(None) => alloc_overflow(),
            Err(Some(layout)) => alloc::alloc::handle_alloc_error(layout),
        }
    }

    #[inline]
    fn data_ptr(this: NonNull<Self>) -> *mut u8 {
        unsafe { this.as_ptr().cast::<u8>().add(OFFSET_DATA) }
    }

    /// Allocates a `ThinInner` where the data segment is uninitialized or
    /// zeroed.
    ///
    /// Returns `Err(Some(layout))` if we failed to allocate that layout, and
    /// `Err(None)` for integer overflow when computing layout
    fn try_allocate_maybe_uninit(
        capacity: usize,
        initially_static: bool,
        init_how: AllocInit,
    ) -> Result<NonNull<Self>, Option<Layout>> {
        const ALIGN: usize = align_of::<ThinInner>();

        debug_assert_ne!(capacity, 0);
        if capacity >= (isize::MAX as usize) - (OFFSET_DATA + ALIGN) {
            return Err(None);
        }

        debug_assert!(Layout::from_size_align(capacity + OFFSET_DATA, ALIGN).is_ok());
        let layout = unsafe { Layout::from_size_align_unchecked(capacity + OFFSET_DATA, ALIGN) };
        let ptr = match init_how {
            AllocInit::Uninit => unsafe { alloc::alloc::alloc(layout) as *mut ThinInner },
            AllocInit::Zero => unsafe { alloc::alloc::alloc_zeroed(layout) as *mut ThinInner },
        };
        if ptr.is_null() {
            return Err(Some(layout));
        }

        // we actually already checked this above...
        debug_assert!(PackedFlagUint::new(initially_static, capacity).is_some());

        let len_flag = PackedFlagUint::new_raw(initially_static, capacity);
        debug_assert_eq!(len_flag.uint_part(), capacity);
        debug_assert_eq!(len_flag.flag_part(), initially_static);

        unsafe {
            core::ptr::addr_of_mut!((*ptr).len_flag).write(len_flag);

            let initial_count_flag = PackedFlagUint::new_raw(initially_static, 1);
            let count_flag: AtomicUsize = AtomicUsize::new(initial_count_flag.encoded_value());
            core::ptr::addr_of_mut!((*ptr).count_flag).write(count_flag);

            debug_assert_eq!(
                (ptr as *const u8).wrapping_add(OFFSET_DATA),
                (*ptr).data.as_ptr(),
            );

            Ok(NonNull::new_unchecked(ptr))
        }
    }

    // returns `Err(Some(l))` if we failed to allocate that layout, and
    // `Err(None)` for integer overflow when computing layout.
    #[inline]
    fn try_allocate(data: &str, initially_static: bool) -> Result<NonNull<Self>, Option<Layout>> {
        // Safety: we initialize the whole buffer by copying `data` into it.
        unsafe {
            // Allocate a enough space to hold the given string
            Self::try_allocate_with(
                data.len(),
                initially_static,
                AllocInit::Uninit,
                // Copy the given string into the allocation
                |uninit_slice| {
                    debug_assert_eq!(uninit_slice.len(), data.len());
                    core::ptr::copy_nonoverlapping(
                        data.as_ptr(),
                        uninit_slice.as_mut_ptr().cast::<u8>(),
                        data.len(),
                    )
                },
            )
        }
    }

    /// Safety: caller must fully initialize the provided buffer with valid
    /// UTF-8 in the `initializer` function (well, you at least need to handle
    /// it before giving it back to the user).
    #[inline]
    unsafe fn try_allocate_with(
        len: usize,
        initially_static: bool,
        init_style: AllocInit,
        initializer: impl FnOnce(&mut [core::mem::MaybeUninit<u8>]),
    ) -> Result<NonNull<Self>, Option<Layout>> {
        // Allocate a enough space to hold the given string
        let this = Self::try_allocate_maybe_uninit(len, initially_static, init_style)?;

        initializer(core::slice::from_raw_parts_mut(
            Self::data_ptr(this).cast::<MaybeUninit<u8>>(),
            len,
        ));

        Ok(this)
    }

    #[inline]
    unsafe fn get_len_flag(p: *const ThinInner) -> PackedFlagUint {
        debug_assert_eq!(OFFSET_LENFLAGS, 0);
        *p.cast()
    }

    #[cold]
    unsafe fn destroy_cold(p: *mut ThinInner) {
        let lf = Self::get_len_flag(p);
        let (is_static, len) = (lf.flag_part(), lf.uint_part());
        debug_assert!(!is_static);
        let layout = {
            let size = len + OFFSET_DATA;
            let align = align_of::<ThinInner>();
            Layout::from_size_align_unchecked(size, align)
        };
        alloc::alloc::dealloc(p as *mut _, layout);
    }
}

#[derive(Clone, Copy, PartialEq)]
enum AllocInit {
    Uninit,
    Zero,
}

#[inline(never)]
#[cold]
fn alloc_overflow() -> ! {
    panic!("overflow during Layout computation")
}

impl From<&str> for ArcStr {
    #[inline]
    fn from(s: &str) -> Self {
        if s.is_empty() {
            Self::new()
        } else {
            Self(ThinInner::allocate(s, false))
        }
    }
}

impl core::ops::Deref for ArcStr {
    type Target = str;
    #[inline]
    fn deref(&self) -> &str {
        unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
    }
}

impl Default for ArcStr {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl From<String> for ArcStr {
    #[inline]
    fn from(v: String) -> Self {
        v.as_str().into()
    }
}

impl From<&mut str> for ArcStr {
    #[inline]
    fn from(s: &mut str) -> Self {
        let s: &str = s;
        Self::from(s)
    }
}

impl From<Box<str>> for ArcStr {
    #[inline]
    fn from(s: Box<str>) -> Self {
        Self::from(&s[..])
    }
}
impl From<ArcStr> for Box<str> {
    #[inline]
    fn from(s: ArcStr) -> Self {
        s.as_str().into()
    }
}
impl From<ArcStr> for alloc::rc::Rc<str> {
    #[inline]
    fn from(s: ArcStr) -> Self {
        s.as_str().into()
    }
}
impl From<ArcStr> for alloc::sync::Arc<str> {
    #[inline]
    fn from(s: ArcStr) -> Self {
        s.as_str().into()
    }
}
impl From<alloc::rc::Rc<str>> for ArcStr {
    #[inline]
    fn from(s: alloc::rc::Rc<str>) -> Self {
        Self::from(&*s)
    }
}
impl From<alloc::sync::Arc<str>> for ArcStr {
    #[inline]
    fn from(s: alloc::sync::Arc<str>) -> Self {
        Self::from(&*s)
    }
}
impl<'a> From<Cow<'a, str>> for ArcStr {
    #[inline]
    fn from(s: Cow<'a, str>) -> Self {
        Self::from(&*s)
    }
}
impl<'a> From<&'a ArcStr> for Cow<'a, str> {
    #[inline]
    fn from(s: &'a ArcStr) -> Self {
        Cow::Borrowed(s)
    }
}

impl<'a> From<ArcStr> for Cow<'a, str> {
    #[inline]
    fn from(s: ArcStr) -> Self {
        if let Some(st) = ArcStr::as_static(&s) {
            Cow::Borrowed(st)
        } else {
            Cow::Owned(s.to_string())
        }
    }
}

impl From<&String> for ArcStr {
    #[inline]
    fn from(s: &String) -> Self {
        Self::from(s.as_str())
    }
}
impl From<&ArcStr> for ArcStr {
    #[inline]
    fn from(s: &ArcStr) -> Self {
        s.clone()
    }
}

impl core::fmt::Debug for ArcStr {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Debug::fmt(self.as_str(), f)
    }
}

impl core::fmt::Display for ArcStr {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Display::fmt(self.as_str(), f)
    }
}

impl PartialEq for ArcStr {
    #[inline]
    fn eq(&self, o: &Self) -> bool {
        ArcStr::ptr_eq(self, o) || PartialEq::eq(self.as_str(), o.as_str())
    }
    #[inline]
    fn ne(&self, o: &Self) -> bool {
        !ArcStr::ptr_eq(self, o) && PartialEq::ne(self.as_str(), o.as_str())
    }
}

impl Eq for ArcStr {}

macro_rules! impl_peq {
    (@one $a:ty, $b:ty) => {
        #[allow(clippy::extra_unused_lifetimes)]
        impl<'a> PartialEq<$b> for $a {
            #[inline]
            fn eq(&self, s: &$b) -> bool {
                PartialEq::eq(&self[..], &s[..])
            }
            #[inline]
            fn ne(&self, s: &$b) -> bool {
                PartialEq::ne(&self[..], &s[..])
            }
        }
    };
    ($(($a:ty, $b:ty),)+) => {$(
        impl_peq!(@one $a, $b);
        impl_peq!(@one $b, $a);
    )+};
}

impl_peq! {
    (ArcStr, str),
    (ArcStr, &'a str),
    (ArcStr, String),
    (ArcStr, Cow<'a, str>),
    (ArcStr, Box<str>),
    (ArcStr, alloc::sync::Arc<str>),
    (ArcStr, alloc::rc::Rc<str>),
    (ArcStr, alloc::sync::Arc<String>),
    (ArcStr, alloc::rc::Rc<String>),
}

impl PartialOrd for ArcStr {
    #[inline]
    fn partial_cmp(&self, s: &Self) -> Option<core::cmp::Ordering> {
        Some(self.as_str().cmp(s.as_str()))
    }
}

impl Ord for ArcStr {
    #[inline]
    fn cmp(&self, s: &Self) -> core::cmp::Ordering {
        self.as_str().cmp(s.as_str())
    }
}

impl core::hash::Hash for ArcStr {
    #[inline]
    fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
        self.as_str().hash(h)
    }
}

macro_rules! impl_index {
    ($($IdxT:ty,)*) => {$(
        impl core::ops::Index<$IdxT> for ArcStr {
            type Output = str;
            #[inline]
            fn index(&self, i: $IdxT) -> &Self::Output {
                &self.as_str()[i]
            }
        }
    )*};
}

impl_index! {
    core::ops::RangeFull,
    core::ops::Range<usize>,
    core::ops::RangeFrom<usize>,
    core::ops::RangeTo<usize>,
    core::ops::RangeInclusive<usize>,
    core::ops::RangeToInclusive<usize>,
}

impl AsRef<str> for ArcStr {
    #[inline]
    fn as_ref(&self) -> &str {
        self
    }
}

impl AsRef<[u8]> for ArcStr {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl core::borrow::Borrow<str> for ArcStr {
    #[inline]
    fn borrow(&self) -> &str {
        self
    }
}

impl core::str::FromStr for ArcStr {
    type Err = core::convert::Infallible;
    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(s))
    }
}

#[cfg(test)]
#[cfg(not(msrv))] // core::mem::offset_of! isn't stable in our MSRV
mod test {
    use super::*;

    fn sasi_layout_check<Buf>() {
        assert!(align_of::<StaticArcStrInner<Buf>>() >= 8);
        assert_eq!(
            core::mem::offset_of!(StaticArcStrInner<Buf>, count_flag),
            OFFSET_COUNTFLAGS
        );
        assert_eq!(
            core::mem::offset_of!(StaticArcStrInner<Buf>, len_flag),
            OFFSET_LENFLAGS
        );
        assert_eq!(
            core::mem::offset_of!(StaticArcStrInner<Buf>, data),
            OFFSET_DATA
        );
        assert_eq!(
            core::mem::offset_of!(ThinInner, count_flag),
            core::mem::offset_of!(StaticArcStrInner::<Buf>, count_flag),
        );
        assert_eq!(
            core::mem::offset_of!(ThinInner, len_flag),
            core::mem::offset_of!(StaticArcStrInner::<Buf>, len_flag),
        );
        assert_eq!(
            core::mem::offset_of!(ThinInner, data),
            core::mem::offset_of!(StaticArcStrInner::<Buf>, data),
        );
    }

    #[test]
    fn verify_type_pun_offsets_sasi_big_bufs() {
        assert_eq!(
            core::mem::offset_of!(ThinInner, count_flag),
            OFFSET_COUNTFLAGS,
        );
        assert_eq!(core::mem::offset_of!(ThinInner, len_flag), OFFSET_LENFLAGS);
        assert_eq!(core::mem::offset_of!(ThinInner, data), OFFSET_DATA);

        assert!(align_of::<ThinInner>() >= 8);

        sasi_layout_check::<[u8; 0]>();
        sasi_layout_check::<[u8; 1]>();
        sasi_layout_check::<[u8; 2]>();
        sasi_layout_check::<[u8; 3]>();
        sasi_layout_check::<[u8; 4]>();
        sasi_layout_check::<[u8; 5]>();
        sasi_layout_check::<[u8; 15]>();
        sasi_layout_check::<[u8; 16]>();
        sasi_layout_check::<[u8; 64]>();
        sasi_layout_check::<[u8; 128]>();
        sasi_layout_check::<[u8; 1024]>();
        sasi_layout_check::<[u8; 4095]>();
        sasi_layout_check::<[u8; 4096]>();
    }
}

#[cfg(all(test, loom))]
mod loomtest {
    use super::ArcStr;
    use loom::sync::Arc;
    use loom::thread;
    #[test]
    fn cloning_threads() {
        loom::model(|| {
            let a = ArcStr::from("abcdefgh");
            let addr = a.as_ptr() as usize;

            let a1 = Arc::new(a);
            let a2 = a1.clone();

            let t1 = thread::spawn(move || {
                let b: ArcStr = (*a1).clone();
                assert_eq!(b.as_ptr() as usize, addr);
            });
            let t2 = thread::spawn(move || {
                let b: ArcStr = (*a2).clone();
                assert_eq!(b.as_ptr() as usize, addr);
            });

            t1.join().unwrap();
            t2.join().unwrap();
        });
    }
    #[test]
    fn drop_timing() {
        loom::model(|| {
            let a1 = alloc::vec![
                ArcStr::from("s1"),
                ArcStr::from("s2"),
                ArcStr::from("s3"),
                ArcStr::from("s4"),
            ];
            let a2 = a1.clone();

            let t1 = thread::spawn(move || {
                let mut a1 = a1;
                while let Some(s) = a1.pop() {
                    assert!(s.starts_with("s"));
                }
            });
            let t2 = thread::spawn(move || {
                let mut a2 = a2;
                while let Some(s) = a2.pop() {
                    assert!(s.starts_with("s"));
                }
            });

            t1.join().unwrap();
            t2.join().unwrap();
        });
    }

    #[test]
    fn leak_drop() {
        loom::model(|| {
            let a1 = ArcStr::from("foo");
            let a2 = a1.clone();

            let t1 = thread::spawn(move || {
                drop(a1);
            });
            let t2 = thread::spawn(move || a2.leak());
            t1.join().unwrap();
            let leaked: &'static str = t2.join().unwrap();
            assert_eq!(leaked, "foo");
        });
    }
}