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
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
use std::{borrow::Borrow, cmp::Ordering, fmt::{self, Debug, Formatter, Pointer}, hash::{Hash, Hasher}, marker::PhantomData, mem::{forget, MaybeUninit}, ops::{Bound, Deref, Index}, ptr::NonNull, str::Utf8Error};
use crate::{InnerHeader, SrcIndex, SrcSlice, SrcTarget, UninitSrc, UniqueSrc, WeakSrc};
/// A single-threaded sliceable reference-counting pointer. 'Src' stands for 'Slice Reference Counted'.
///
/// See [`std::rc`] for details about general reference-counting pointers.
///
/// `Src` is a variation on [`Rc`](std::rc::Rc); the functionality that differentiates them can be accessed with the method [`Src::slice`].
///
/// Many of the inherent methods of `Src` are associated functions, which means that you have to call them as e.g.,
/// [`Src::downgrade(&value)`](Src::downgrade) instead of `value.downgrade()`;
/// this avoids conflicts with methods of the inner type `T`.
/// However, some methods, e.g. [`Src::slice`], are permitted to remain as methods because the inner type is known not to have a conflicting method,
/// and some, e.g. [`Src::len`], intentionally shadow a known method of the inner type because they use a more efficient computation for the same result.
pub struct Src<T: SrcTarget + ?Sized> {
// SAFETY:
// requires:
// * initialized from InnerHeader::new_inner::<T::Item>(_)
pub(crate) header: NonNull<InnerHeader>,
// SAFETY:
// requires:
// * initialized from either InnerHeader::get_body_ptr::<T::Item>(self.header) or InnerHeader::get_elem_ptr::<T::Item>(self.header, i) where 0 <= i <= InnerHeader::get_header(self.header).len()
// * all body elements have been properly initialized (e.g., self.start.as_ref() will not cause UB)
pub(crate) start: NonNull<T::Item>,
// SAFETY:
// requires when T: SrcSlice:
// * self.start.add(self.len) <= InnerHeader::get_body_ptr::<T::Item>(self.header).add(InnerHeader::get_header(self.header).len())
// requires when T: Sized:
// * self.start < InnerHeader::get_body_ptr::<T::Item>(self.header).add(InnerHeader::get_header(self.header).len())
pub(crate) len: T::Len,
pub(crate) _phantom: PhantomData<*const T>,
}
impl<T: SrcTarget + ?Sized> Src<T> {
fn header(&self) -> &InnerHeader {
// SAFETY:
// * the invariant for self.header guarantees that it is from InnerHeader::new_inner::<T::Item>
// * the header is only accessed from InnerHeader::get_header
unsafe { InnerHeader::get_header(self.header) }
}
/// Returns `true` if the two `Src`s point to slices starting at the same location in memory, akin to [`ptr::eq`](std::ptr::eq).
///
/// ```rust
/// use slice_rc::Src;
///
/// let slice = Src::from_array([1, 2, 3]);
/// let same_slice = Src::clone(&slice);
/// let sub_slice = slice.slice(1..);
/// let other_slice = Src::from_array([1, 2, 3]);
///
/// assert!(Src::ptr_eq(&slice, &same_slice));
/// assert!(!Src::ptr_eq(&slice, &sub_slice));
/// assert!(!Src::ptr_eq(&slice, &other_slice));
/// ```
///
/// If `Src::ptr_eq(&a, &b)` returns true, then <code>Src::[same_root](Src::same_root)(&a, &b)</code> will also be true.
///
/// The type parameter, `U`, is to allow `Src`s of different types that _could_ be of the same allocation, and therefore, _could_ be equal by pointer, to be compared, e.g.:
/// ```rust
/// # use slice_rc::Src;
/// let single: Src<i32> = Src::single(4);
/// let slice: Src<[i32]> = Src::as_slice(&single);
///
/// assert!(Src::ptr_eq(&single, &slice));
/// ```
///
/// Note that this method currently ignores the length of the slice:
/// ```rust
/// # use slice_rc::Src;
/// let root = Src::from_array([1, 2, 3]);
/// let first = root.slice(0);
///
/// assert!(Src::ptr_eq(&root, &first));
///
/// let mid_to_end_slice = root.slice(1..);
/// let mid_slice = root.slice(1..=1);
///
/// assert!(Src::ptr_eq(&mid_to_end_slice, &mid_slice));
/// ```
/// It is undecided whether this behavior is desireable, and as such, it may change;
/// notably, [`Rc::ptr_eq`](std::rc::Rc::ptr_eq) does ignore metadata for `?Sized` types
/// (though that's irrelevant for slices because [`Rc`]s can only point to the whole slice, and therefore the length will always be the same for [`Rc`]s that point to the same allocation),
/// while [`ptr::eq`](std::ptr::eq) does consider the metadata (which causes inconsistent results for trait objects, but that is irrelevant here because `Src`s don't support trait objects).
///
/// See also [`Src::same_root`].
///
/// [`Rc`]: std::rc::Rc
#[inline]
pub fn ptr_eq<U: SrcTarget<Item = T::Item> + ?Sized>(this: &Src<T>, other: &Src<U>) -> bool {
this.start == other.start
}
/// Returns `true` if the two `Src`s share the same [root](crate#root) (i.e., they point to parts of the same allocation).
///
/// ```rust
/// use slice_rc::Src;
///
/// let slice = Src::from_array([1, 2, 3]);
/// let same_slice = Src::clone(&slice);
/// let other_slice = Src::from_array([1, 2, 3]);
///
/// assert!(Src::same_root(&slice, &same_slice));
/// assert!(!Src::same_root(&slice, &other_slice));
/// ```
///
/// Notably, neither slice has to be the root, nor do they need to overlap at all:
/// ```rust
/// # use slice_rc::Src;
/// let root = Src::from_array([1, 2, 3]);
/// let a = root.slice(..1);
/// let b = root.slice(2..);
///
/// assert!(Src::same_root(&a, &b));
/// ```
///
/// The type parameter, `U`, is to allow `Src`s of different types that _could_ share the same root, to be compared, e.g.:
/// ```rust
/// # use slice_rc::Src;
/// let single: Src<i32> = Src::single(4);
/// let slice: Src<[i32]> = Src::as_slice(&single);
///
/// assert!(Src::same_root(&single, &slice));
/// ```
///
/// This method ignores the length of the slices in question, but unlike [`Src::ptr_eq`], this will not change,
/// as the roots remains the same regardless of which parts of it are included in these slices.
///
/// See also [`Src::ptr_eq`], [`Src::is_root`], and [`Src::root`].
#[inline]
pub fn same_root<U: SrcTarget<Item = T::Item> + ?Sized>(this: &Src<T>, other: &Src<U>) -> bool {
this.header == other.header
}
/// Returns `true` if this `Src` contains its [root](crate#root) (i.e., it references its entire allocation).
/// Notably, this `Src` does not have to be the first one that was initialized, it just has to cover the entire allocation.
///
/// ```rust
/// use slice_rc::Src;
///
/// let root = Src::from_array([1, 2, 3]);
/// let also_root = root.slice(..);
/// let slice = root.slice(1..);
///
/// assert!(Src::is_root(&root));
/// assert!(Src::is_root(&also_root));
/// assert!(!Src::is_root(&slice));
/// ```
///
/// See also [`Src::same_root`] and [`Src::root`].
#[inline]
pub fn is_root(this: &Src<T>) -> bool {
// SAFETY:
// * the invariant for this.header guarantees that it is from InnerHeader::new_inner::<T::Item>
// * the header is only accessed from InnerHeader::get_header
let root_start = unsafe { InnerHeader::get_body_ptr(this.header) };
this.start == root_start && T::len_as_usize(this.len) == this.header().len()
}
/// Creates a [`WeakSrc`] pointer to this slice.
/// The [`WeakSrc`] refers to the same slice as this `Src`, and therefore, refers to the [root](crate#root) if and only if this `Src` does.
///
/// ```rust
/// use slice_rc::Src;
///
/// let root = Src::from_array([1, 2, 3]);
///
/// let weak_root = Src::downgrade(&root);
/// ```
pub fn downgrade(this: &Src<T>) -> WeakSrc<T> {
this.header().inc_weak_count();
WeakSrc {
// SAFETY: this.header's invariant implies _.header's invariant
header: this.header,
// SAFETY: this.header's invariant implies _.header's invariant
start: this.start,
// SAFETY: this.header's invariant implies _.header's invariant
len: this.len,
_phantom: PhantomData,
}
}
/// Gets the number of strong (`Src`) pointers to any part of this allocation.
///
/// ```rust
/// use slice_rc::Src;
///
/// let root = Src::from_array([1, 2, 3]);
/// let _slice = root.slice(1..);
///
/// assert_eq!(2, Src::strong_count(&root));
/// ```
pub fn strong_count(this: &Src<T>) -> usize {
this.header().strong_count()
}
/// Gets the number of [`WeakSrc`] pointers to any part of this allocation.
///
/// ```rust
/// use slice_rc::Src;
///
/// let root = Src::from_array([1, 2, 3]);
/// let slice = root.slice(1..);
/// let _weak_slice = Src::downgrade(&slice);
///
/// assert_eq!(1, Src::weak_count(&root));
/// ```
pub fn weak_count(this: &Src<T>) -> usize {
this.header().weak_count() - 1
}
/// Turns this `Src` into a [`UniqueSrc`], if it has only one strong reference.
///
/// Otherwise, an [`Err`] is returned withe the same `Src` that was passed in.
///
/// This will succeed even if there are outstanding weak references,
/// though those references will not be able to [`upgrade`](WeakSrc::upgrade) while this allocation is managed by a [`UniqueSrc`].
///
/// ```rust
/// use slice_rc::Src;
///
/// let x = Src::single(3);
/// assert_eq!(*Src::into_unique(x).unwrap(), 3);
///
/// let x = Src::single(4);
/// let _y = Src::clone(&x);
/// assert_eq!(*Src::into_unique(x).unwrap_err(), 4);
/// ```
///
/// See also [`Src::make_unique`].
///
/// Note that this method (and [`Src::make_unique`]) can currently be used to construct non-[root](crate#root) [`UniqueSrc`]s;
/// this behavior has not been thouroghly considered and may be changed or removed in the future.
/// As it is, the [`UniqueSrc`] will retain unique ownership of the whole allocation, even the parts it doesn't contain.
/// This means that a [`WeakSrc`] to any part of the allocation will not be able to [`upgrade`](WeakSrc::upgrade),
/// even if that [`WeakSrc`] does not overlap with the [`UniqueSrc`].
pub fn into_unique(this: Src<T>) -> Result<UniqueSrc<T>, Src<T>> {
let header = this.header();
if header.strong_count() == 1 {
// decrease the strong count to 0, but don't drop;
// obviously, the UniqueSrc still needs the body to exist, so it shouldn't be dropped,
// but the strong count should be 0 so that the weak references can't upgrade (because that Src would be an alias of the UniqueSrc, which violates UniqueSrc's invariant)
header.dec_strong_count();
let this2 = UniqueSrc {
// SAFETY: this.header has the same safety invariant as this2.header, in addition to the requirement that no other strong Src has access to this allocation, which is checked by header.strong_count() == 1
header: this.header,
// SAFETY: this.start has the same safety invariant as this2.start
start: this.start,
// SAFETY: this.len has the same safety invariant as this2.len
len: this.len,
_phantom: PhantomData,
};
forget(this); // as stated above, don't drop; additionally, now that the strong count has already been set to 0, dropping this would actually cause a panic on debug and probably big problems on release because it would cause integer overflow
Ok(this2)
} else {
Err(this)
}
}
/// If this is the only strong reference to this allocation, then it is turned into a [`UniqueSrc`].
/// Otherwise, the contents are cloned and return in a new [`UniqueSrc`].
///
/// This is somewhat like [`Rc::make_mut`](std::rc::Rc::make_mut), but with some subtle differences.
///
/// ```rust
/// use slice_rc::{Src, UniqueSrc};
///
/// let data = Src::single(5);
/// let mut data = Src::make_unique(data); // Won't clone anything
/// *data += 1;
/// let data = UniqueSrc::into_shared(data);
/// let other_data = Src::clone(&data); // Won't clone inner data
/// let mut data = Src::make_unique(data); // Clones inner data
/// *data += 1;
/// let data = UniqueSrc::into_shared(data);
/// let mut data = Src::make_unique(data); // Won't clone anything
/// *data += 1;
/// let data = UniqueSrc::into_shared(data);
/// let mut other_data = Src::make_unique(other_data); // Won't clone anything
/// *other_data *= 2;
/// let other_data = UniqueSrc::into_shared(other_data);
///
/// // Now `data` and `other_data` point to different allocations.
/// assert_eq!(*data, 8);
/// assert_eq!(*other_data, 12);
/// ```
///
/// If this is the only `Src` pointer to this allocation,
/// but there are some [`WeakSrc`] pointers, then the [`WeakSrc`] pointers will be carried over to the [`UniqueSrc`].
/// ```rust
/// # use slice_rc::{Src, UniqueSrc};
/// let data = Src::single(75);
/// let weak = Src::downgrade(&data);
///
/// assert_eq!(75, *data);
/// assert_eq!(75, *weak.upgrade().unwrap());
///
/// let mut data = Src::make_unique(data);
/// *data += 1;
///
/// assert_eq!(76, *data);
/// assert!(weak.upgrade().is_none());
///
/// let data = UniqueSrc::into_shared(data);
///
/// assert_eq!(76, *data);
/// assert_eq!(76, *weak.upgrade().unwrap());
/// ```
/// However, if there are other `Src` pointers to this allocation, any [`WeakSrc`] pointers will remain with the old allocation.
/// ```rust
/// # use slice_rc::{Src, UniqueSrc};
/// let data = Src::single(75);
/// let other_data = Src::clone(&data);
/// let weak = Src::downgrade(&data);
///
/// assert_eq!(75, *data);
/// assert_eq!(75, *other_data);
/// assert_eq!(75, *weak.upgrade().unwrap());
///
/// let mut data = Src::make_unique(data);
/// *data += 1;
///
/// assert_eq!(76, *data);
/// assert_eq!(75, *other_data);
/// assert_eq!(75, *weak.upgrade().unwrap());
///
/// let data = UniqueSrc::into_shared(data);
///
/// assert_eq!(76, *data);
/// assert_eq!(75, *other_data);
/// assert_eq!(75, *weak.upgrade().unwrap());
/// ```
///
/// See also [`Src::into_unique`].
///
/// Note that this method (and [`Src::into_unique`]) can currently be used to construct non-[root](crate#root) [`UniqueSrc`]s;
/// this behavior has not been thouroghly considered and may be changed or removed in the future.
/// As it is, the [`UniqueSrc`] will retain unique ownership of the whole allocation, even the parts it doesn't contain.
/// This means that a [`WeakSrc`] to any part of the allocation will not be able to [`upgrade`](WeakSrc::upgrade),
/// even if that [`WeakSrc`] does not overlap with the [`UniqueSrc`].
pub fn make_unique(this: Src<T>) -> UniqueSrc<T> where T::Item: Clone {
Src::into_unique(this).unwrap_or_else(|this| T::new_unique_from_clone(&*this))
}
/// Returns an `Src` pointer that refers to this `Src`'s [root](crate#root) (i.e., the entire allocation).
/// ```rust
/// use slice_rc::Src;
///
/// let root = Src::from_array([1, 2, 3]);
/// let slice = root.slice(1..);
/// drop(root);
///
/// assert_eq!(*slice, [2, 3]);
///
/// let new_root = Src::root(&slice);
///
/// assert_eq!(*new_root, [1, 2, 3]);
/// ```
///
/// This method returns an <code>[Src]\<\[T::[Item](crate::SrcTarget::Item)]></code> rather than an <code>[Src]\<T></code> for two reasons:
/// * If <code>T: [Sized]</code>, then the root can only be a <code>[Src]\<T></code> if its total length is is `1`, which would prevent situations like this:
/// ```rust
/// # use slice_rc::Src;
/// let root: Src<[i32]> = Src::from_array([1, 2, 3]);
/// let slice: Src<i32> = root.slice(1);
/// let new_root: Src<[i32]> = Src::root(&slice);
///
/// assert_eq!(*new_root, [1, 2, 3]);
/// ```
/// * If <code>T = [str]</code>, it could be a UTF-8 slice of a larger allocation that is not entirely UTF-8, which would violate the safety invariant of [`str`]:
/// ```rust
/// # use slice_rc::Src;
/// let root: Src<[u8]> = Src::copied(b"\xFFhello");
/// let s: Src<str> = Src::from_utf8(root.slice(1..)).unwrap();
/// let new_root: Src<[u8]> = Src::root(&s);
///
/// assert_eq!(&*s, "hello");
/// assert!(Src::from_utf8(new_root).is_err());
/// ```
pub fn root(this: &Src<T>) -> Src<[T::Item]> {
let header = this.header();
// SAFETY:
// * the invariant for self.header guarantees that it is from InnerHeader::new_inner::<T::Item>
// * the header is only accessed from InnerHeader::get_header
let start = unsafe { InnerHeader::get_body_ptr::<T::Item>(this.header) };
header.inc_strong_count();
Src {
// SAFETY: self.header has the same safety invariant as this.header
header: this.header,
// SAFETY: start was just aquired from InnerHeader::get_body_ptr::<T::Item>(self.header), which, with the assertions, meets the safety requirement
start,
// SAFETY: header.len() meets the safety requirements by definition
len: header.len(),
_phantom: PhantomData,
}
}
}
impl<T: SrcSlice + ?Sized> Src<T> {
/// Constructs a new [root](crate#root) `Src` of length `0`.
/// Note that, because `Src`s are not growable like [`Vec`]s are, this allocation will never become larger.
/// Every reference to this allocation is a [root](crate#root).
///
/// ```rust
/// use slice_rc::Src;
///
/// let s = Src::<[i32]>::empty();
///
/// assert_eq!(s.len(), 0);
/// ```
#[inline]
pub fn empty() -> Src<T> {
UniqueSrc::into_shared(UniqueSrc::empty())
}
/// Returns the number of elements in this `Src`.
/// This method deliberately shadows [`<[T]>::len`](slice::len) and [`str::len`] because this method provides a (slightly) simpler and more efficient implementation.
///
/// This method only returns the length of the whole allocation if `self` is a [root](crate#root) `Src`.
///
/// ```rust
/// use slice_rc::Src;
///
/// let s = Src::from_array([1, 2, 3]);
/// assert_eq!(s.len(), 3);
/// ```
#[inline]
pub fn len(&self) -> usize {
self.len
}
/// Returns `true` if this `Src` has a length of `0`.
/// This method deliberately shadows [`<[T]>::is_empty`](slice::is_empty) and [`str::is_empty`] because this method provides a (slightly) simpler and more efficient implementation.
///
/// Note that this method does not imply that this `Src` was constructed via [`Src::empty`].
/// Similarly, it does not imply that the entire allocation is empty, unless `self` is a [root](crate#root) `Src`.
///
/// ```rust
/// use slice_rc::Src;
///
/// let a = Src::from_array([1, 2, 3]);
/// assert!(!a.is_empty());
///
/// let b = Src::<[i32]>::from_array([]);
/// assert!(b.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Returns an `Src` pointer to an element or subslice depending on the type of index.
/// * If given a position (only applicable where `Self = Src<[U]>`), returns an `Src<U>` to the element at that position.
/// * If given a range, returns the subslice corresponding to that range.
///
/// # Panics
/// If the index is in some way out of bounds, or if <code>Self = [Src]\<[str]></code> and the indices are not at [char boundaries](str::is_char_boundary).
///
/// # Examples
/// ```rust
/// use slice_rc::Src;
///
/// let v = Src::from_array([10, 40, 30]);
/// assert_eq!(Src::single(40), v.slice(1));
/// assert_eq!(Src::from_array([10, 40]), v.slice(0..2));
/// ```
/// Panics:
/// ```should_panic
/// # use slice_rc::Src;
/// let v = Src::from_array([10, 40, 30]);
/// let _ = v.slice(3);
/// let _ = v.slice(0..4);
/// ```
#[inline]
pub fn slice<I: SrcIndex<T>>(&self, index: I) -> Src<I::Output> {
index.get(self.clone())
}
pub(crate) fn into_item(self, index: usize) -> Src<T::Item> {
let header = self.header();
assert!(index < header.len(), "index {index} out of range for slice of length {}", header.len());
// SAFETY: the safety invariant of self.start implies this safety requirement, given the assertion that index <= header.len()
let start_ptr = unsafe { self.start.add(index) };
let this = Src {
// SAFETY: self.header has the same safety invariant as this.header
header: self.header,
// SAFETY: start_ptr was just aquired from InnerHeader::get_elem_ptr::<T::Item>(self.header, start_inc), which, with the assertions, meets the safety requirement
start: start_ptr,
// SAFETY: the assertions guarantee the safety requirements
len: (),
_phantom: PhantomData,
};
forget(self); // don't modify the strong count because this is logically the same Src
this
}
pub(crate) fn into_slice_from_bounds(self, start: Bound<usize>, end: Bound<usize>) -> Src<T> {
let header = self.header();
let start_inc = match start {
Bound::Excluded(val) => val + 1,
Bound::Included(val) => val,
Bound::Unbounded => 0,
};
let end_exc = match end {
Bound::Excluded(val) => val,
Bound::Included(val) => val + 1,
Bound::Unbounded => header.len(),
};
assert!(start_inc <= end_exc, "slice index starts at {start_inc} but ends at {end_exc}");
assert!(end_exc <= header.len(), "range end index {end_exc} out of range for slice of length {}", header.len());
T::validate_range(&self, start_inc..end_exc);
let len = end_exc - start_inc;
// SAFETY: the safety invariant of self.start implies this safety requirement, given the assertion that start_inc <= header.len()
let start_ptr = unsafe { self.start.add(start_inc) };
let this = Src {
// SAFETY: self.header has the same safety invariant as this.header
header: self.header,
// SAFETY: start_ptr was just aquired from InnerHeader::get_elem_ptr::<T::Item>(self.header, start_inc), which, with the assertions, meets the safety requirement
start: start_ptr,
// SAFETY: the assertions guarantee the safety requirements
len,
_phantom: PhantomData,
};
forget(self); // don't modify the strong count because this is logically the same Src
this
}
}
impl<T: Sized> Src<T> {
/// Constructs a new [root](crate#root) `Src` that contains only the given value.
///
/// ```rust
/// use slice_rc::Src;
///
/// let s = Src::single(42);
/// assert_eq!(*s, 42);
/// assert_eq!(Src::root(&s).len(), 1);
/// ```
#[inline]
pub fn single(value: T) -> Src<T> {
UninitSrc::single().init(value)
}
/// Constructs a new [root](crate#root) `Src` that contains only the value returned from the given function `f`.
/// The [`WeakSrc`] that `f` is given will be a weak reference to this allocation, which allows constructing a self-referential value;
/// it will return [`None`] from [`WeakSrc::upgrade`] until after `single_cyclic` has returned.
///
/// This is a convienience method for a specific subset of behavior that can be obtained via [`UninitSrc`].
///
/// ```rust
/// use slice_rc::{Src, WeakSrc};
///
/// struct S {
/// me: WeakSrc<S>,
/// }
///
/// let s = Src::single_cyclic(|me| S { me: me.clone() });
///
/// assert!(Src::ptr_eq(&s, &s.me.upgrade().unwrap()));
/// ```
#[inline]
pub fn single_cyclic<F: FnOnce(&WeakSrc<T>) -> T>(f: F) -> Src<T> {
UniqueSrc::into_shared(UniqueSrc::single_cyclic(f))
}
/// Constructs a new [root](crate#root) `Src` of length `1` with uninitialized contents.
///
/// ```rust
/// use slice_rc::{Src, UniqueSrc};
///
/// let five = Src::<i32>::single_uninit();
///
/// let mut five = Src::into_unique(five).unwrap();
/// five.write(5);
/// let five = UniqueSrc::into_shared(five);
///
/// let five = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5);
/// ```
#[inline]
pub fn single_uninit() -> Src<MaybeUninit<T>> {
UniqueSrc::into_shared(UniqueSrc::single_uninit())
}
/// Constructs a new [root](crate#root) `Src` of length `1` with uninitialized contents, with the memory being filled with `0` bytes.
///
/// See [`MaybeUninit::zeroed`] for examples of correct and incorrect usage of this method.
///
/// ```rust
/// use slice_rc::Src;
///
/// let zero = Src::<i32>::single_zeroed();
/// let zero = unsafe { zero.assume_init() };
///
/// assert_eq!(*zero, 0);
/// ```
#[inline]
pub fn single_zeroed() -> Src<MaybeUninit<T>> {
UniqueSrc::into_shared(UniqueSrc::single_zeroed())
}
/// Returns an `Src` equivalent to this one, but typed as a slice rather than a single element.
/// The returned slice will have a length of `1`, and its element `0` will be at the same location in memory as `self`'s value.
///
/// ```rust
/// use slice_rc::Src;
/// use std::ptr;
///
/// let single = Src::single(42);
/// let slice = Src::as_slice(&single);
///
/// assert!(Src::ptr_eq(&single, &slice));
/// assert!(ptr::eq(&*single, &slice[0]));
/// ```
#[inline]
pub fn as_slice(this: &Src<T>) -> Src<[T]> {
this.header().inc_strong_count();
Src {
// SAFETY: this.header has the same invariant as this2
header: this.header,
// SAFETY: this.start has the same invariant as this2
start: this.start,
// SAFETY: this.len's invariant implies this2.len's invariant
len: 1,
_phantom: PhantomData,
}
}
}
impl<T> Src<[T]> {
/// Constructs a new [root](crate#root) `Src` of the given length with uninitialized contents.
///
/// ```rust
/// use slice_rc::{Src, UniqueSrc};
///
/// let fives = Src::<[i32]>::new_uninit(3);
///
/// let mut fives = Src::into_unique(fives).unwrap();
/// fives[0].write(5);
/// fives[1].write(5);
/// fives[2].write(5);
/// let fives = UniqueSrc::into_shared(fives);
///
/// let fives = unsafe { fives.assume_init() };
///
/// assert_eq!(*fives, [5, 5, 5]);
/// ```
#[inline]
pub fn new_uninit(len: usize) -> Src<[MaybeUninit<T>]> {
UniqueSrc::into_shared(UniqueSrc::new_uninit(len))
}
/// Constructs a new [root](crate#root) `Src` of the given length with uninitialized contents, with the memory being filled with `0` bytes.
///
/// See [`MaybeUninit::zeroed`] for examples of correct and incorrect usage of this method.
///
/// ```rust
/// use slice_rc::Src;
///
/// let zeroes = Src::<[i32]>::new_zeroed(3);
/// let zeroes = unsafe { zeroes.assume_init() };
///
/// assert_eq!(*zeroes, [0, 0, 0]);
/// ```
#[inline]
pub fn new_zeroed(len: usize) -> Src<[MaybeUninit<T>]> {
UniqueSrc::into_shared(UniqueSrc::new_zeroed(len))
}
/// Constructs a new [root](crate#root) `Src` of the given length where each element is produced by calling `f` with that element's index while walking forward through the slice.
///
/// This essentially the same as writing
/// ```text
/// Src::from_array([f(0), f(1), f(2), ..., f(len - 2), f(len - 1)])
/// ```
/// and is similar to `(0..len).map(f)`, just for `Src`s rather than iterators.
///
/// If `len == 0`, this produces an empty `Src` without ever calling `f`.
///
/// ```rust
/// use slice_rc::Src;
///
/// let slice = Src::from_fn(5, |i| i);
/// assert_eq!(*slice, [0, 1, 2, 3, 4]);
///
/// let slice2 = Src::from_fn(8, |i| i * 2);
/// assert_eq!(*slice2, [0, 2, 4, 6, 8, 10, 12, 14]);
///
/// let bool_slice = Src::from_fn(5, |i| i % 2 == 0);
/// assert_eq!(*bool_slice, [true, false, true, false, true]);
/// ```
///
/// You can also capture things, so you can use closures with mutable state.
/// The slice is generated in ascending index order, starting from the front and going towards the back.
/// ```rust
/// # use slice_rc::Src;
/// let mut state = 1;
/// let s = Src::from_fn(6, |_| { let x = state; state *= 2; x });
/// assert_eq!(*s, [1, 2, 4, 8, 16, 32]);
/// ```
///
/// # Panics
///
/// Panics if `f` panics; in this event, any elements that have been initialized will be properly dropped.
/// ```rust
/// # use slice_rc::Src;
/// # use std::cell::Cell;
/// thread_local! {
/// static DROPPED: Cell<usize> = Cell::new(0);
/// }
///
/// struct Droppable;
///
/// impl Drop for Droppable {
/// fn drop(&mut self) {
/// DROPPED.with(|dropped| dropped.update(|x| x + 1));
/// }
/// }
///
/// let _ = std::panic::catch_unwind(|| {
/// Src::from_fn(10, |i| {
/// if i >= 5 { panic!() }
/// Droppable
/// })
/// });
///
/// assert_eq!(DROPPED.get(), 5);
/// ```
#[inline]
pub fn from_fn<F: FnMut(usize) -> T>(len: usize, f: F) -> Src<[T]> {
UninitSrc::new(len).init_from_fn(f)
}
/// Constructs a new [root](crate#root) `Src` of the given length where each element is produced by calling `f` with a root [`WeakSrc`] pointer to the new allocation and that element's index while walking forward through the slice.
///
/// This method is like [`Src::from_fn`], but in this the function `f` is passed a root [`WeakSrc`] pointer to the allocation to allow constructing self-referential elements.
///
/// This is a convienience method for a specific subset of behavior that can be obtained via [`UninitSrc`].
///
/// ```rust
/// use slice_rc::{Src, WeakSrc};
///
/// struct S {
/// val: usize,
/// root: WeakSrc<[S]>,
/// }
///
/// let root = Src::cyclic_from_fn(5, |root, i| S {
/// val: i * 2,
/// root: root.clone(),
/// });
///
/// assert_eq!(root.iter().map(|s| s.val).collect::<Vec<_>>(), vec![0, 2, 4, 6, 8]);
/// assert!(root.iter().all(|s| Src::ptr_eq(&root, &s.root.upgrade().unwrap())));
/// ```
///
/// It is possible to obtain a `WeakSrc` to the individual element that is being initialized via [`WeakSrc::slice`]:
///
/// ```rust
/// # use slice_rc::{Src, WeakSrc};
/// struct S {
/// val: usize,
/// me: WeakSrc<S>,
/// }
///
/// let root = Src::cyclic_from_fn(5, |root, i| S {
/// val: i * 2,
/// me: root.slice(i),
/// });
///
/// assert!(root.iter().enumerate().all(|(i, s)| Src::ptr_eq(&root.slice(i), &s.me.upgrade().unwrap())));
/// ```
pub fn cyclic_from_fn<F: FnMut(&WeakSrc<[T]>, usize) -> T>(len: usize, mut f: F) -> Src<[T]> {
let this = UninitSrc::new(len);
let weak = this.downgrade();
this.init_from_fn(|i| f(&weak, i))
}
/// Constructs a new [root](crate#root) `Src` from the given iterator.
///
/// This method is essentially shorthand for
/// ```rust
/// use slice_rc::Src;
///
/// # fn f<T>(iter: impl IntoIterator<Item = T, IntoIter: std::iter::ExactSizeIterator>) -> Src<[T]> {
/// let mut iter = iter.into_iter();
/// Src::from_fn(iter.len(), |_| iter.next().unwrap())
/// # }
/// ```
///
/// The iterator must be [`ExactSizeIterator`] because `Src`s cannot be resized,
/// so the number of elements must be known at allocation-time, i.e., before any of the elements are initialized.
/// If you want to use a non-[`ExactSizeIterator`], use <code>iter.[collect](Iterator::collect)::\<[Vec]\<_>>()</code>.
pub fn from_iter<I: IntoIterator<Item = T, IntoIter: ExactSizeIterator>>(iter: I) -> Src<[T]> {
let mut iter = iter.into_iter();
Self::from_fn(iter.len(), |_| iter.next().unwrap())
}
/// Constructs a new [root](crate#root) `Src` from the given array.
///
/// This method is effectively equivalent to passing an array to [`Src::from_iter`], but it is more efficient.
/// As such, it is effectively shorthand for <code>Src::[from_fn](N, |i| values\[i])</code>, but again, more efficient
/// (though not by enough to make <code>Src::from_array([array::from_fn]::<_, N, _>(f))</code> any better than <code>Src::[from_fn](N, f)</code>).
///
/// Note that the my assertions about efficiency are not based any kind of benchmarking,
/// just the fact that this method uses a single [`ptr::write`] where [`Src::from_fn`] and [`Src::from_iter`] use `N` arbitrary function calls and `N` [`ptr::write`]s.
/// As <code>[array::from_fn]</code> re-introduces at least the `N` arbitrary function calls, its difference (again, without benchmarking) is negligible.
///
/// [from_fn]: Src::from_fn
/// [`ptr::write`]: std::ptr::write
/// [array::from_fn]: std::array::from_fn
#[inline]
pub fn from_array<const N: usize>(values: [T; N]) -> Src<[T]> {
UniqueSrc::into_shared(UniqueSrc::from_array(values))
}
/// Constructs a new [root](crate#root) `Src` of the given length where each element is the type's [default](Default::default).
///
/// This method is essentially equivalent to <code>Src::[from_fn](Src::from_fn)(len, |_| [Default::default]\())</code>.
#[inline]
pub fn from_default(len: usize) -> Src<[T]> where T: Default {
Self::from_fn(len, |_| Default::default())
}
/// Constructs a new [root](crate#root) `Src` of the given length where each element is a clone of `value`.
///
/// This method is essentially equivalent to <code>Src::[from_fn](Src::from_fn)(len, |_| value.[clone](Clone::clone)())</code>.
#[inline]
pub fn filled(len: usize, value: &T) -> Src<[T]> where T: Clone {
Self::from_fn(len, |_| value.clone())
}
/// Constructs a new [root](crate#root) `Src` of the given length where each element is a clone of the value returned from `f`.
/// `f` is passed a root [`WeakSrc`] pointer to the allocation; this can be used to make self-referential structures.
///
/// ```rust
/// use slice_rc::{Src, WeakSrc};
///
/// #[derive(Clone)]
/// struct S {
/// val: i32,
/// root: WeakSrc<[S]>,
/// }
///
/// let root = Src::filled_cyclic(5, |root| S { val: 42, root: root.clone() });
/// assert!(root.iter().all(|s| s.val == 42));
/// assert!(root.iter().all(|s| Src::ptr_eq(&root, &s.root.upgrade().unwrap())));
/// ```
#[inline]
pub fn filled_cyclic<F: FnOnce(&WeakSrc<[T]>) -> T>(len: usize, f: F) -> Src<[T]> where T: Clone {
UniqueSrc::into_shared(UniqueSrc::filled_cyclic(len, f))
}
/// Constructs a new [root](crate#root) `Src` as a clone of the given slice.
///
/// This method is essentially shorthand for <code>Src::[from_fn](Src::from_fn)(values.[len](slice::len)(), |i| values\[i].clone())</code>,
/// but without the implicit bounds checking for slice indexing.
#[inline]
pub fn cloned(values: &[T]) -> Src<[T]> where T: Clone {
UniqueSrc::into_shared(UniqueSrc::cloned(values))
}
/// Constructs a new [root](crate#root) `Src` as a copy of the given slice.
///
/// This method is functionally shorthand for <code>Src::[from_fn](Src::from_fn)(values.[len](slice::len)(), |i| values\[i])</code>,
/// but without the implicit bounds checking for slice indexing.
///
/// This method is fairly efficient, as it is basically just an allocation (requisite for any `Src` constructor) and a [`memcpy`](std::ptr::copy_nonoverlapping).
#[inline]
pub fn copied(values: &[T]) -> Src<[T]> where T: Copy {
UniqueSrc::into_shared(UniqueSrc::copied(values))
}
}
impl<T> Src<MaybeUninit<T>> {
/// Converts to `Src<T>`.
///
/// # Safety
///
/// As with [`MaybeUninit::assume_init`], it is up to the caller to guarantee that the inner value really is in an initialized state.
/// Calling this when the content is not yet fully initialized causes immediate undefined behavior.
///
/// # Examples
///
/// ```rust
/// use slice_rc::Src;
///
/// let zero = Src::<i32>::single_zeroed();
/// let zero = unsafe { zero.assume_init() };
///
/// assert_eq!(*zero, 0);
/// ```
pub unsafe fn assume_init(self) -> Src<T> {
// TODO: rephrase the safety requirements for InnerHeader to explicitly allow punning between T and type with T-like layout
let this = Src {
// SAFETY: self.header has *almost* the same safety invariant as this.header: the only difference is that self uses MaybeUninit<T> where this expects T
header: self.header,
// SAFETY: self.start has *almost* the same safety invariant as this.start: the only difference is that self uses MaybeUninit<T> where this expects T
start: self.start.cast(),
// SAFETY: self.len has *almost* the same safety invariant as this.len: the only difference is that self uses MaybeUninit<T> where this expects T
len: self.len,
_phantom: PhantomData,
};
forget(self); // don't decrease the strong counter because this is logically the same Src
this
}
}
impl<T> Src<[MaybeUninit<T>]> {
/// Converts to `Src<[T]>`.
///
/// # Safety
///
/// As with [`MaybeUninit::assume_init`], it is up to the caller to guarantee that the inner value really is in an initialized state.
/// Calling this when the content is not yet fully initialized causes immediate undefined behavior.
///
/// # Examples
///
/// ```rust
/// use slice_rc::Src;
///
/// let zeroes = Src::<[i32]>::new_zeroed(3);
/// let zeroes = unsafe { zeroes.assume_init() };
///
/// assert_eq!(*zeroes, [0, 0, 0]);
/// ```
pub unsafe fn assume_init(self) -> Src<[T]> {
// TODO: rephrase the safety requirements for InnerHeader to explicitly allow punning between T and type with T-like layout
let this = Src {
// SAFETY: self.header has *almost* the same safety invariant as this.header: the only difference is that self uses MaybeUninit<T> where this expects T
header: self.header,
// SAFETY: self.start has *almost* the same safety invariant as this.start: the only difference is that self uses MaybeUninit<T> where this expects T
start: self.start.cast(),
// SAFETY: self.len has *almost* the same safety invariant as this.len: the only difference is that self uses MaybeUninit<T> where this expects T
len: self.len,
_phantom: PhantomData,
};
forget(self); // don't decrease the strong counter because this is logically the same Src
this
}
}
impl Src<str> {
/// Constructs a new [`root`](crate#root) `Src` as a copy of the given string.
///
/// ```rust
/// use slice_rc::Src;
///
/// let hello = Src::new("Hello World!");
///
/// assert_eq!(&*hello, "Hello World!");
/// ```
#[inline]
pub fn new(s: impl AsRef<str>) -> Src<str> {
UniqueSrc::into_shared(UniqueSrc::new(s))
}
/// Converts an `Src` of bytes to a string `Src`.
///
/// [`str`] and [`[u8]`](slice) are both slices of bytes, so this function converts between the two.
/// Not all byte slices are valid string slices, however: [`str`] must be valid UTF-8.
/// This method checks to ensure that the bytes are valid UTF-8, and then does the conversion.
///
/// If you are sure that the byte slice is valid UTF-8, and you don't want to incur the overhead of the validity check,
/// there is an unsafe version of this method, [`from_utf8_unchecked`](Src::from_utf8_unchecked),
/// which has the same behavior but skips the check.
///
/// # Errors
///
/// Returns `Err` if the slice is not UTF-8 with a description as to why the provided slice is not UTF-8.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use slice_rc::Src;
///
/// let sparkle_heart = Src::from_array([240, 159, 146, 150]);
///
/// let sparkle_heart = Src::from_utf8(sparkle_heart)?;
///
/// assert_eq!("💖", &*sparkle_heart);
/// # Ok::<_, std::str::Utf8Error>(())
/// ```
///
/// Incorrect bytes:
///
/// ```rust
/// # use slice_rc::Src;
/// let sparkle_heart = Src::from_array([0, 159, 146, 150]);
///
/// assert!(Src::from_utf8(sparkle_heart).is_err());
/// ```
#[inline]
pub fn from_utf8(v: Src<[u8]>) -> Result<Src<str>, Utf8Error> {
let _: &str = str::from_utf8(&*v)?;
// SAFETY: <str>::from_utf8() guarantees that the contents are UTF-8
Ok(unsafe { Src::from_utf8_unchecked(v) })
}
/// Converts an `Src` of bytes to a string `Src` without checking that the string contains valid UTF-8.
///
/// See the safe version, [`from_utf8`](Src::from_utf8), for more information.
///
/// # Safety
///
/// The bytes passed in must be valid UTF-8.
///
/// # Examples
///
/// ```rust
/// use slice_rc::Src;
///
/// let sparkle_heart = Src::from_array([240, 159, 146, 150]);
///
/// let sparkle_heart = unsafe { Src::from_utf8_unchecked(sparkle_heart) };
///
/// assert_eq!("💖", &*sparkle_heart);
/// ```
#[inline]
pub unsafe fn from_utf8_unchecked(v: Src<[u8]>) -> Src<str> {
// TODO: rephrase the safety requirements for InnerHeader to explicitly allow punning between T and type with T-like layout
let this = Src {
// SAFETY: v.header has *almost* the same safety invariant as this.header: the only difference is that v uses [u8] where this expects str;
// the pun from [u8] to str adds the safety requirement that v's content is valid UTF-8, but this requirement is passed on to the caller
header: v.header,
// SAFETY: v.start has *almost* the same safety invariant as this.start: the only difference is that v uses [u8] where this expects str;
// the pun from [u8] to str adds the safety requirement that v's content is valid UTF-8, but this requirement is passed on to the caller
start: v.start,
// SAFETY: v.len has *almost* the same safety invariant as this.len: the only difference is that v uses [u8] where this expects str;
// the pun from [u8] to str adds the safety requirement that v's content is valid UTF-8, but this requirement is passed on to the caller
len: v.len,
_phantom: PhantomData,
};
forget(v);
this
}
/// Converts a string `Src` to a `Src` of bytes.
/// To convert the the bytes back to a string, use the [`from_utf8`](Src::from_utf8) method.
///
/// # Examples
///
/// ```rust
/// use slice_rc::Src;
///
/// let bytes = Src::as_bytes(&Src::new("bors"));
/// assert_eq!(b"bors", &*bytes);
/// ```
#[inline]
pub fn as_bytes(this: &Src<str>) -> Src<[u8]> {
this.header().inc_strong_count();
// TODO: rephrase the safety requirements for InnerHeader to explicitly allow punning between T and type with T-like layout
Src {
// SAFETY: this.header has *almost* the same safety invariant as this2.header: the only difference is that this uses str where this2 expects [u8];
// the pun from str to [u8] relaxes the safety requirement that this's content is valid UTF-8
header: this.header,
// SAFETY: this.start has *almost* the same safety invariant as this2.start: the only difference is that this uses str where this2 expects [u8];
// the pun from str to [u8] relaxes the safety requirement that this's content is valid UTF-8
start: this.start,
// SAFETY: this.len has *almost* the same safety invariant as this2.len: the only difference is that this uses str where this2 expects [u8];
// the pun from str to [u8] relaxes the safety requirement that this's content is valid UTF-8
len: this.len,
_phantom: PhantomData,
}
}
}
impl<T: SrcTarget + ?Sized> Clone for Src<T> {
#[inline]
fn clone(&self) -> Self {
self.header().inc_strong_count();
Self {
// SAFETY: this.header has the same safety invariant as _.header
header: self.header,
// SAFETY: this.start has the same safety invariant as _.start
start: self.start,
// SAFETY: this.len has the same safety invariant as _.len
len: self.len,
_phantom: PhantomData,
}
}
}
impl<T: Default> Default for Src<T> {
#[inline]
fn default() -> Self {
Self::single(T::default())
}
}
impl<T: SrcTarget + ?Sized> Deref for Src<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
T::get(self)
}
}
impl<T: SrcTarget + ?Sized> Borrow<T> for Src<T> {
#[inline]
fn borrow(&self) -> &T {
&**self
}
}
impl<T: SrcTarget + ?Sized> AsRef<T> for Src<T> {
#[inline]
fn as_ref(&self) -> &T {
&**self
}
}
impl<T: SrcTarget + Index<I> + ?Sized, I> Index<I> for Src<T> {
type Output = T::Output;
#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.deref()[index]
}
}
impl<T: Hash + SrcTarget + ?Sized> Hash for Src<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
T::hash(&**self, state);
}
}
impl<T: PartialEq<U> + SrcTarget + ?Sized, U: SrcTarget + ?Sized> PartialEq<Src<U>> for Src<T> {
#[inline]
fn eq(&self, other: &Src<U>) -> bool {
T::eq(&**self, &**other)
}
#[inline]
fn ne(&self, other: &Src<U>) -> bool {
T::ne(&**self, &**other)
}
}
impl<T: Eq + SrcTarget + ?Sized> Eq for Src<T> {}
impl<T: PartialOrd<U> + SrcTarget + ?Sized, U: SrcTarget + ?Sized> PartialOrd<Src<U>> for Src<T> {
#[inline]
fn ge(&self, other: &Src<U>) -> bool {
T::ge(&**self, &**other)
}
#[inline]
fn gt(&self, other: &Src<U>) -> bool {
T::gt(&**self, &**other)
}
#[inline]
fn le(&self, other: &Src<U>) -> bool {
T::le(&**self, &**other)
}
#[inline]
fn lt(&self, other: &Src<U>) -> bool {
T::lt(&**self, &**other)
}
#[inline]
fn partial_cmp(&self, other: &Src<U>) -> Option<Ordering> {
T::partial_cmp(&**self, &**other)
}
}
impl<T: Ord + SrcTarget + ?Sized> Ord for Src<T> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
T::cmp(&**self, &**other)
}
}
impl<T: Debug + SrcTarget + ?Sized> Debug for Src<T> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
T::fmt(self, f)
}
}
impl<T: SrcTarget + ?Sized> Pointer for Src<T> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Pointer::fmt(&self.start, f)
}
}
impl<T: SrcTarget + ?Sized> Drop for Src<T> {
fn drop(&mut self) {
// SAFETY:
// * all constructor fns for Src initialize header from InnerHeader::new_inner::<T::Item>
// * all constructor fns for Src initialize the elements
// * the header is only accessed from InnerHeader::get_header
// * this is one of two callsites for InnerHeader::drop_strong::<T::Item>, the other being UniqueSrc::drop;
// as a UniqueSrc cannot co-exist (for the same allocation) with an Src, this will be the last strong reference iff this is the last Src with access to this allocation's body
unsafe { InnerHeader::drop_strong::<T::Item>(self.header); }
}
}
#[cfg(test)]
mod tests {
use std::{cell::Cell, mem::MaybeUninit, ops::Deref, panic::{catch_unwind, AssertUnwindSafe}, str::Utf8Error};
use crate::*;
#[test]
fn ptr_eq() {
let s1: Src<[u8]> = Src::from_default(0);
let s2: Src<[u8]> = Src::clone(&s1);
assert_eq!(s1, s2);
assert!(Src::ptr_eq(&s1, &s2));
let s3: Src<[u8]> = Src::from_default(0);
assert_eq!(s1, s3);
assert_eq!(s2, s3);
assert!(!Src::ptr_eq(&s1, &s3));
assert!(!Src::ptr_eq(&s2, &s3));
}
#[test]
fn same_root() {
let s1: Src<[u8]> = Src::from_default(1);
let s2: Src<[u8]> = s1.slice(1..);
assert_ne!(s1, s2);
assert!(!Src::ptr_eq(&s1, &s2));
assert!(Src::same_root(&s1, &s2));
let s3: Src<[u8]> = Src::from_default(1);
let s4: Src<[u8]> = s3.slice(1..);
assert_eq!(s1, s3);
assert_ne!(s3, s4);
assert_eq!(s2, s4);
assert!(!Src::ptr_eq(&s1, &s3));
assert!(!Src::ptr_eq(&s2, &s4));
assert!(!Src::ptr_eq(&s2, &s4));
assert!(!Src::same_root(&s1, &s3));
assert!(!Src::same_root(&s2, &s4));
assert!(Src::same_root(&s3, &s4));
}
#[test]
fn is_root() {
let s1: Src<[u8]> = Src::from_default(1);
let s2: Src<[u8]> = s1.slice(..);
let s3: Src<[u8]> = s1.slice(1..);
assert!(Src::is_root(&s1));
assert!(Src::is_root(&s2));
assert!(!Src::is_root(&s3));
}
#[test]
fn downgrade() {
let s1: Src<[u8]> = Src::from_default(0);
let w: WeakSrc<[u8]> = Src::downgrade(&s1);
let s2: Src<[u8]> = w.upgrade().unwrap();
assert_eq!(s1, s2);
}
#[test]
fn strong_count() {
let s1: Src<[u8]> = Src::from_default(0);
assert_eq!(Src::strong_count(&s1), 1);
let s2: Src<[u8]> = Src::clone(&s1);
assert_eq!(Src::strong_count(&s1), 2);
assert_eq!(Src::strong_count(&s2), 2);
let w1: WeakSrc<[u8]> = Src::downgrade(&s1);
assert_eq!(Src::strong_count(&s1), 2);
assert_eq!(Src::strong_count(&s2), 2);
assert_eq!(w1.strong_count(), 2);
let w2: WeakSrc<[u8]> = Src::downgrade(&s1);
assert_eq!(Src::strong_count(&s1), 2);
assert_eq!(Src::strong_count(&s2), 2);
assert_eq!(w1.strong_count(), 2);
assert_eq!(w2.strong_count(), 2);
std::mem::drop(s1);
assert_eq!(Src::strong_count(&s2), 1);
assert_eq!(w1.strong_count(), 1);
assert_eq!(w2.strong_count(), 1);
std::mem::drop(s2);
assert_eq!(w1.strong_count(), 0);
assert_eq!(w2.strong_count(), 0);
}
#[test]
fn weak_count() {
let s1: Src<[u8]> = Src::from_default(0);
assert_eq!(Src::weak_count(&s1), 0);
let s2: Src<[u8]> = Src::clone(&s1);
assert_eq!(Src::weak_count(&s1), 0);
assert_eq!(Src::weak_count(&s2), 0);
let w1: WeakSrc<[u8]> = Src::downgrade(&s1);
assert_eq!(Src::weak_count(&s1), 1);
assert_eq!(Src::weak_count(&s2), 1);
assert_eq!(w1.weak_count(), 1);
let w2: WeakSrc<[u8]> = w1.clone();
assert_eq!(Src::weak_count(&s1), 2);
assert_eq!(Src::weak_count(&s2), 2);
assert_eq!(w1.weak_count(), 2);
assert_eq!(w2.weak_count(), 2);
std::mem::drop(s1);
assert_eq!(Src::weak_count(&s2), 2);
assert_eq!(w1.weak_count(), 2);
assert_eq!(w2.weak_count(), 2);
std::mem::drop(w1);
assert_eq!(Src::weak_count(&s2), 1);
assert_eq!(w2.weak_count(), 1);
std::mem::drop(s2);
assert_eq!(w2.weak_count(), 0);
}
#[test]
fn into_unique() {
let s1: Src<[u8]> = Src::from_default(0);
let s2: Src<[u8]> = Src::clone(&s1);
let s1: Src<[u8]> = Src::into_unique(s1).unwrap_err();
std::mem::drop(s2);
let w: WeakSrc<[u8]> = Src::downgrade(&s1);
assert!(w.upgrade().is_some());
let u: UniqueSrc<[u8]> = Src::into_unique(s1).unwrap();
assert!(w.upgrade().is_none());
let s1: Src<[u8]> = UniqueSrc::into_shared(u);
assert!(w.upgrade().is_some());
_ = s1;
}
#[test]
fn make_unique() {
{ // non-unique
let s1: Src<[u8]> = Src::from_default(0);
let s2: Src<[u8]> = Src::clone(&s1);
let w1: WeakSrc<[u8]> = Src::downgrade(&s1);
let u: UniqueSrc<[u8]> = Src::make_unique(s1);
let w2: WeakSrc<[u8]> = UniqueSrc::downgrade(&u);
assert!(!w1.same_root(&w2));
_ = s2;
}
{ // unique
let s: Src<[u8]> = Src::from_default(0);
let w1: WeakSrc<[u8]> = Src::downgrade(&s);
let u: UniqueSrc<[u8]> = Src::make_unique(s);
let w2: WeakSrc<[u8]> = UniqueSrc::downgrade(&u);
assert!(w1.same_root(&w2));
}
}
#[test]
fn empty() {
let s: Src<[u8]> = Src::empty();
assert!(s.is_empty());
assert_eq!(s.len(), 0);
let s: Src<str> = Src::empty();
assert!(s.is_empty());
assert_eq!(s.len(), 0);
}
#[test]
fn len() {
let s: Src<[u8]> = Src::from_default(0);
assert_eq!(s.len(), 0);
let s: Src<[u8]> = Src::from_default(1);
assert_eq!(s.len(), 1);
let s: Src<[u8]> = Src::from_default(17);
assert_eq!(s.len(), 17);
let s: Src<[u8]> = s.slice(3..14);
assert_eq!(s.len(), 11);
let s: Src<[u8]> = s.slice(3..3);
assert_eq!(s.len(), 0);
}
#[test]
fn is_empty() {
let s: Src<[u8]> = Src::from_default(0);
assert!(s.is_empty());
let s: Src<[u8]> = Src::from_default(1);
assert!(!s.is_empty());
let s: Src<[u8]> = Src::from_default(17);
assert!(!s.is_empty());
let s: Src<[u8]> = s.slice(3..14);
assert!(!s.is_empty());
let s: Src<[u8]> = s.slice(3..3);
assert!(s.is_empty());
}
#[test]
fn root() {
let s1: Src<[u8]> = Src::from_default(1);
assert!(Src::is_root(&s1));
let s1: Src<[u8]> = s1.slice(1..);
assert!(!Src::is_root(&s1));
let s2: Src<[u8]> = Src::root(&s1);
assert!(Src::is_root(&s2));
assert!(Src::same_root(&s1, &s2));
}
#[test]
fn slice() {
{ // slice
let s1: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(&*s1, &[1, 2, 3]);
let s1: Src<[u8]> = s1.slice(1..);
assert_eq!(&*s1, &[2, 3]);
let s2: Src<[u8]> = s1.slice(..1);
assert_eq!(&*s2, &[2]);
assert!(Src::same_root(&s1, &s2));
}
{ // item 1
let s1: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(&*s1, &[1, 2, 3]);
let s2: Src<u8> = s1.slice(2);
assert_eq!(&*s2, &3);
let s2: Src<[u8]> = Src::as_slice(&s2);
assert_eq!(&*s2, &[3]);
assert!(Src::same_root(&s1, &s2));
}
{ // item 2
let s1: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(&*s1, &[1, 2, 3]);
let s1: Src<[u8]> = s1.slice(1..);
assert_eq!(&*s1, &[2, 3]);
let s2: Src<u8> = s1.slice(0);
assert_eq!(&*s2, &2);
let s2: Src<[u8]> = Src::as_slice(&s2);
assert_eq!(&*s2, &[2]);
assert!(Src::same_root(&s1, &s2));
}
}
#[test]
fn single() {
let s: Src<u8> = Src::single(42);
assert_eq!(*s, 42);
}
#[test]
fn single_cyclic() {
{ // non-cyclic
let s: Src<u8> = Src::single_cyclic(|_| 42);
assert_eq!(*s, 42);
}
{ // cyclic
struct S {
this: WeakSrc<S>,
i: usize,
}
let s: Src<S> = Src::single_cyclic(|weak| S { this: weak.clone(), i: 42 });
assert_eq!(s.i, 42);
let w: WeakSrc<S> = Src::downgrade(&s);
assert!(WeakSrc::ptr_eq(&s.this, &w));
}
}
#[test]
fn single_uninit() {
let s: Src<MaybeUninit<u8>> = Src::single_uninit();
let mut u: UniqueSrc<MaybeUninit<u8>> = Src::make_unique(s);
u.write(42);
let s: Src<MaybeUninit<u8>> = UniqueSrc::into_shared(u);
// SAFETY: just initialized it with u.write()
let s: Src<u8> = unsafe { s.assume_init() };
assert_eq!(*s, 42);
}
#[test]
fn single_zeroed() {
let s: Src<MaybeUninit<u8>> = Src::single_zeroed();
// SAFETY: u8 is a zeroable type
let s: Src<u8> = unsafe { s.assume_init() };
assert_eq!(*s, 0);
}
#[test]
fn as_slice() {
{ // single root
let s1: Src<u8> = Src::single(42);
let s2: Src<[u8]> = Src::as_slice(&s1);
assert_eq!([*s1], *s2);
}
{ // from slice
let s1: Src<[u8]> = Src::from_array([1, 2, 3]);
let s2: Src<u8> = s1.slice(1);
let s3: Src<[u8]> = Src::as_slice(&s2);
assert_eq!(s1[1], *s2);
assert_eq!([*s2], *s3);
}
}
#[test]
fn new_uninit() {
let s: Src<[MaybeUninit<u8>]> = Src::new_uninit(3);
assert_eq!(s.len(), 3);
let mut u: UniqueSrc<[MaybeUninit<u8>]> = Src::make_unique(s);
for (i, elem) in u.iter_mut().enumerate() {
elem.write(i as _);
}
let s: Src<[MaybeUninit<u8>]> = UniqueSrc::into_shared(u);
// SAFETY: just initialized it with all the elem.write()s
let s: Src<[u8]> = unsafe { s.assume_init() };
assert_eq!(*s, [0, 1, 2]);
}
#[test]
fn new_zeroed() {
let s: Src<[MaybeUninit<u8>]> = Src::new_zeroed(3);
assert_eq!(s.len(), 3);
// SAFETY: u8 is a zeroable type
let s: Src<[u8]> = unsafe { s.assume_init() };
assert_eq!(*s, [0, 0, 0]);
}
#[test]
fn from_fn() {
{ // normal
let s: Src<[usize]> = Src::from_fn(3, |i| i * 2);
assert_eq!(*s, [0, 2, 4]);
}
{ // panic
let drop_flags: [_; 6] = std::array::from_fn(|_| AssertUnwindSafe(Cell::new(false)));
struct DropFlagger<'a>(&'a Cell<bool>);
impl Drop for DropFlagger<'_> {
fn drop(&mut self) {
self.0.update(|v| !v)
}
}
let _: Result<_, _> = catch_unwind(|| {
let _: Src<[DropFlagger<'_>]> = Src::from_fn(drop_flags.len(), |i| {
if i >= 3 { panic!() }
DropFlagger(&drop_flags[i])
});
});
assert!(drop_flags[..3].iter().map(Deref::deref).all(Cell::get));
assert!(!drop_flags[3..].iter().map(Deref::deref).any(Cell::get));
}
}
#[test]
fn cyclic_from_fn() {
{ // normal, not cyclic
let s: Src<[usize]> = Src::cyclic_from_fn(3, |_, i| i * 2);
assert_eq!(*s, [0, 2, 4]);
}
{ // normal, cyclic
struct S {
all: WeakSrc<[S]>,
i: usize,
}
let s1: Src<[S]> = Src::cyclic_from_fn(3, |w, i| S { all: w.clone(), i: i * 2 });
assert_eq!(s1[0].i, 0);
assert_eq!(s1[1].i, 2);
assert_eq!(s1[2].i, 4);
let s2: Src<[S]> = s1[0].all.upgrade().unwrap();
assert!(Src::ptr_eq(&s1, &s2));
}
{ // panic
let drop_flags: [_; 6] = std::array::from_fn(|_| AssertUnwindSafe(Cell::new(false)));
struct DropFlagger<'a>(&'a Cell<bool>);
impl Drop for DropFlagger<'_> {
fn drop(&mut self) {
self.0.update(|v| !v)
}
}
let _: Result<_, _> = catch_unwind(|| {
let _: Src<[DropFlagger<'_>]> = Src::cyclic_from_fn(drop_flags.len(), |_, i| {
if i >= 3 { panic!() }
DropFlagger(&drop_flags[i])
});
});
assert!(drop_flags[..3].iter().map(Deref::deref).all(Cell::get));
assert!(!drop_flags[3..].iter().map(Deref::deref).any(Cell::get));
}
}
#[test]
fn from_iter() {
let s: Src<[u8]> = Src::from_iter(vec![1, 2, 3].into_iter().map(|i| i * 2));
assert_eq!(*s, [2, 4, 6]);
}
#[test]
fn from_array() {
let s: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(*s, [1, 2, 3]);
}
#[test]
fn from_default() {
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
struct D42(u8);
impl Default for D42 {
fn default() -> Self {
Self(42)
}
}
let s: Src<[u8]> = Src::from_default(3);
assert_eq!(*s, [0, 0, 0]);
let s: Src<[D42]> = Src::from_default(3);
assert_eq!(*s, [D42(42), D42(42), D42(42)]);
}
#[test]
fn filled() {
let s: Src<[u8]> = Src::filled(3, &42);
assert_eq!(*s, [42, 42, 42]);
}
#[test]
fn filled_cyclic() {
{ // non-cyclic
let s: Src<[u8]> = Src::filled_cyclic(3, |_| 42);
assert_eq!(*s, [42, 42, 42]);
}
{ // cyclic
#[derive(Clone)]
struct S {
all: WeakSrc<[S]>,
i: usize,
}
let s: Src<[S]> = Src::filled_cyclic(3, |weak| S { all: weak.clone(), i: 42 });
assert_eq!(s[0].i, 42);
assert_eq!(s[1].i, 42);
assert_eq!(s[2].i, 42);
let w: WeakSrc<[S]> = Src::downgrade(&s);
assert!(WeakSrc::ptr_eq(&s[0].all, &w));
}
}
#[test]
fn cloned() {
#[derive(Clone, Eq, PartialEq, Debug)]
struct NonCopy(u8);
let s: Src<[NonCopy]> = Src::cloned(&[NonCopy(1), NonCopy(2), NonCopy(3)]);
assert_eq!(*s, [NonCopy(1), NonCopy(2), NonCopy(3)]);
}
#[test]
fn copied() {
let s: Src<[u8]> = Src::copied(&[1, 2, 3]);
assert_eq!(*s, [1, 2, 3]);
}
#[test]
fn assume_init_single() {
let s: Src<MaybeUninit<u8>> = Src::single_zeroed();
// SAFETY: u8 is a zeroable type
let s: Src<u8> = unsafe { s.assume_init() };
assert_eq!(*s, 0);
}
#[test]
fn assume_init_slice() {
let s: Src<[MaybeUninit<u8>]> = Src::new_zeroed(3);
// SAFETY: u8 is a zeroable type
let s: Src<[u8]> = unsafe { s.assume_init() };
assert_eq!(*s, [0, 0, 0]);
}
#[test]
fn new() {
let s: Src<str> = Src::new("Hello World!");
assert_eq!(&*s, "Hello World!");
}
#[test]
fn from_utf8() {
{ // UTF-8
let s: Src<[u8]> = Src::copied(b"Hello World!");
let s: Src<str> = Src::from_utf8(s).unwrap();
assert_eq!(&*s, "Hello World!");
}
{ // not UTF-8
let s: Src<[u8]> = Src::copied(&[0xFF]);
let _: Utf8Error = Src::from_utf8(s).unwrap_err();
}
}
#[test]
fn from_utf8_unchecked() {
let s: Src<[u8]> = Src::copied(b"Hello World!");
// SAFETY: just got the bytes from a str
let s: Src<str> = unsafe { Src::from_utf8_unchecked(s) };
assert_eq!(&*s, "Hello World!");
}
#[test]
fn as_bytes() {
let s: Src<str> = Src::new("Hello World!");
let s: Src<[u8]> = Src::as_bytes(&s);
assert_eq!(&*s, b"Hello World!");
}
#[test]
fn clone() {
let s1: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(Src::strong_count(&s1), 1);
let s2: Src<[u8]> = s1.clone();
assert_eq!(Src::strong_count(&s1), 2);
assert_eq!(*s1, *s2);
assert!(Src::ptr_eq(&s1, &s2));
}
#[test]
fn deref() {
let s: Src<[u8]> = Src::from_array([1, 2, 3]);
assert_eq!(Deref::deref(&s), &[1, 2, 3]);
}
#[test]
fn drop() {
let drop_flags: [_; 3] = std::array::from_fn(|_| Cell::new(false));
struct DropFlagger<'a>(&'a Cell<bool>);
impl Drop for DropFlagger<'_> {
fn drop(&mut self) {
self.0.update(|v| !v)
}
}
assert!(!drop_flags.iter().any(Cell::get));
let s1: Src<[DropFlagger<'_>]> = Src::from_iter(drop_flags.iter().map(DropFlagger));
assert!(!drop_flags.iter().any(Cell::get));
assert_eq!(Src::strong_count(&s1), 1);
let s2: Src<[DropFlagger<'_>]> = s1.clone();
assert!(!drop_flags.iter().any(Cell::get));
assert_eq!(Src::strong_count(&s1), 2);
assert_eq!(Src::strong_count(&s2), 2);
std::mem::drop(s1);
assert!(!drop_flags.iter().any(Cell::get));
assert_eq!(Src::strong_count(&s2), 1);
std::mem::drop(s2);
assert!(drop_flags.iter().all(Cell::get));
}
}