slice-rc 0.2.1

A variety of reference-counted pointers with better support for slices than std::rc::Rc
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
#![warn(missing_docs)]

//! `slice-rc` provides a variant of the `std`'s [`Rc`](std::rc::Rc) type, and more generally the contents of the module [`std::rc`].
//! 
//! If you are not familiar with those, you should read their documentation before trying to use this crate.
//! 
//! * [`Src<T>`] (which, as the crate name suggests, stands for 'Slice Reference Counted') is a variant of [`std::rc::Rc<T>`]
//! * [`WeakSrc<T>`] is a variant of [`std::rc::Weak<T>`]
//! * [`UniqueSrc<T>`] is a variant of the currently-unstable [`std::rc::UniqueRc<T>`]
//! * [`UninitSrc<T>`] is a variant of the as-yet-only-hypothetical `std::rc::UninitRc<T>`
//! 
//! ### How do this crate's types differ from the [`std::rc`] ones?
//! 
//! Notably, <code>[Rc](std::rc::Rc)\<[\[T\]](prim@slice)></code> is already a valid, usable type; why do we need a special slice [`Rc`](std::rc::Rc) when [`Rc`](std::rc::Rc) already supports slices?
//! 
//! The answer can be found in one method (well, two: one on [`Src`](Src::slice), and its analog on [`WeakSrc`](WeakSrc::slice)):
//! [`Src::slice`] allows us to acquire a sub-slice of the contents of the [`Src`] which still uses the reference-counting mechanism rather than using lifetime-style references;
//! e.g.:
//! 
//! ```rust
//! use slice_rc::Src;
//! 
//! let whole: Src<str> = Src::new("Hello World!");
//! let world: Src<str> = whole.slice(6..=10);
//! assert_eq!(&*world, "World");
//! ```
//! 
//! This is useful because:
//! * <code>[Src]\<T>: \'static where T: \'static</code>, and therefore this is useful where lifetimes are difficult or impossible to accomodate.
//! * Via [`WeakSrc`] and constructor functions like [`Src::cyclic_from_fn`], it allows constructing collections of self-referential objects,
//!   and unlike e.g. <code>[Vec]\<[Rc](std::rc::Rc)\<T>></code>, in a <code>[Src]\<[\[T\]](prim@slice)></code>, the actual `T`'s are contiguous, which makes it possible to obtain a [`&[T]`](prim@slice).
//! 
//! ### Root
//! 
//! Many methods in this crate refer to a "root" pointer (e.g., root `Src`).
//! This refers to any `Src`-family pointer that refers to the whole allocation;
//! its [`len`](Src::len) is the number of elements in the whole allocation,
//! and every non-root pointer to the allocation references either an element or a subslice of if.
//! 
//! Note that it is not just the "original" `Src` that can be root:
//! 
//! ```rust
//! # use slice_rc::Src;
//! let root = Src::from_array([1, 2, 3]);
//! 
//! assert!(Src::is_root(&root));
//! 
//! let slice = root.slice(1..);
//! 
//! assert!(!Src::is_root(&slice));
//! 
//! let also_root = Src::root(&slice);
//! 
//! assert!(Src::is_root(&also_root));
//! ```

mod inner;
mod strong;
mod uninit;
mod unique;
mod weak;

use std::{ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}, slice, usize};

use inner::*;
pub use strong::*;
pub use uninit::*;
pub use unique::*;
pub use weak::*;

/// A helper trait for [`Src::slice`].
/// Analagous to [`SliceIndex`](std::slice::SliceIndex).
pub trait SrcIndex<T: SrcSlice + ?Sized> {
  
  /// The output type returned by methods.
  type Output: SrcTarget + ?Sized;
  
  /// Returns an [`Src`] pointer to the output at this location, panicking if out of bounds.
  fn get(self, slice: Src<T>) -> Src<Self::Output>;
  
}

impl<T> SrcIndex<[T]> for usize {
  
  type Output = T;
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    Src::into_item(slice, self)
  }
  
}

impl<T> SrcIndex<[T]> for (Bound<usize>, Bound<usize>) {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let (start, end) = self;
    Src::into_slice_from_bounds(slice, start, end)
  }
  
}

impl<T> SrcIndex<[T]> for Range<usize> {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let Range { start, end } = self;
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Excluded(end))
  }
  
}

impl<T> SrcIndex<[T]> for RangeFrom<usize> {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let RangeFrom { start } = self;
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Unbounded)
  }
  
}

impl<T> SrcIndex<[T]> for RangeFull {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let RangeFull = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Unbounded)
  }
  
}

impl<T> SrcIndex<[T]> for RangeInclusive<usize> {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let (start, end) = self.into_inner();
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Included(end))
  }
  
}

impl<T> SrcIndex<[T]> for RangeTo<usize> {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let RangeTo { end } = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Excluded(end))
  }
  
}

impl<T> SrcIndex<[T]> for RangeToInclusive<usize> {
  
  type Output = [T];
  
  #[inline]
  fn get(self, slice: Src<[T]>) -> Src<Self::Output> {
    let RangeToInclusive { end } = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Included(end))
  }
  
}

impl SrcIndex<str> for (Bound<usize>, Bound<usize>) {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let (start, end) = self;
    Src::into_slice_from_bounds(slice, start, end)
  }
  
}

impl SrcIndex<str> for Range<usize> {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let Range { start, end } = self;
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Excluded(end))
  }
  
}

impl SrcIndex<str> for RangeFrom<usize> {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let RangeFrom { start } = self;
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Unbounded)
  }
  
}

impl SrcIndex<str> for RangeFull {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let RangeFull = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Unbounded)
  }
  
}

impl SrcIndex<str> for RangeInclusive<usize> {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let (start, end) = self.into_inner();
    Src::into_slice_from_bounds(slice, Bound::Included(start), Bound::Included(end))
  }
  
}

impl SrcIndex<str> for RangeTo<usize> {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let RangeTo { end } = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Excluded(end))
  }
  
}

impl SrcIndex<str> for RangeToInclusive<usize> {
  
  type Output = str;
  
  #[inline]
  fn get(self, slice: Src<str>) -> Src<Self::Output> {
    let RangeToInclusive { end } = self;
    Src::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Included(end))
  }
  
}

/// A helper trait for [`WeakSrc::slice`].
pub trait WeakSrcIndex<T: SrcSlice + ?Sized>: SrcIndex<T> {
  
  /// Returns a [`WeakSrc`] pointer to the output at this location, panicking if out of bounds.
  fn get_weak(self, slice: WeakSrc<T>) -> WeakSrc<Self::Output>;
  
}

impl<T> WeakSrcIndex<[T]> for usize {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    WeakSrc::into_item(slice, self)
  }
  
}

impl<T> WeakSrcIndex<[T]> for (Bound<usize>, Bound<usize>) {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let (start, end) = self;
    WeakSrc::into_slice_from_bounds(slice, start, end)
  }
  
}

impl<T> WeakSrcIndex<[T]> for Range<usize> {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let Range { start, end } = self;
    WeakSrc::into_slice_from_bounds(slice, Bound::Included(start), Bound::Excluded(end))
  }
  
}

impl<T> WeakSrcIndex<[T]> for RangeFrom<usize> {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let RangeFrom { start } = self;
    WeakSrc::into_slice_from_bounds(slice, Bound::Included(start), Bound::Unbounded)
  }
  
}

impl<T> WeakSrcIndex<[T]> for RangeFull {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let RangeFull = self;
    WeakSrc::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Unbounded)
  }
  
}

impl<T> WeakSrcIndex<[T]> for RangeInclusive<usize> {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let (start, end) = self.into_inner();
    WeakSrc::into_slice_from_bounds(slice, Bound::Included(start), Bound::Included(end))
  }
  
}

impl<T> WeakSrcIndex<[T]> for RangeTo<usize> {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let RangeTo { end } = self;
    WeakSrc::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Excluded(end))
  }
  
}

impl<T> WeakSrcIndex<[T]> for RangeToInclusive<usize> {
  
  #[inline]
  fn get_weak(self, slice: WeakSrc<[T]>) -> WeakSrc<Self::Output> {
    let RangeToInclusive { end } = self;
    WeakSrc::into_slice_from_bounds(slice, Bound::Unbounded, Bound::Included(end))
  }
  
}

/// A sealed trait to encode types that can can be used in the `Src`-family of pointers.
/// 
/// Either <code>Self: [Sized]</code> or <code>Self: [SrcSlice]</code>.
pub trait SrcTarget: private::SealedSrcTarget {
  
  /// The type of each element of a <code>[Src]\<Self></code>.
  /// * Where <code>Self: [Sized]</code>: `Self::Item = Self`
  /// * Where <code>Self = [\[T\]](prim@slice)</code>: `Self::Item = T`
  /// * Where <code>Self = [str]</code>: <code>Self::Item = [u8]</code>
  ///   (because a [`str`] is just a [`[u8]`](prim@slice) which must be UTF-8)
  type Item: Sized;
  
}

#[diagnostic::do_not_recommend]
impl<T> SrcTarget for T {
  
  type Item = T;
  
}

#[diagnostic::do_not_recommend]
impl<T> private::SealedSrcTarget for T {
  
  type Len = ();
  
  #[inline]
  fn len_as_usize((): Self::Len) -> usize {
    1
  }
  
  fn get(rc: &Src<Self>) -> &Self {
    // SAFETY:
    // all constructor fns of Src guarantee initialization of all elements
    unsafe { rc.start.as_ref() }
  }
  
  fn get_unique(rc: &UniqueSrc<Self>) -> &Self {
    // SAFETY:
    // all constructor fns of UniqueSrc guarantee initialization of all elements
    unsafe { rc.start.as_ref() }
  }
  
  fn get_unique_mut(rc: &mut UniqueSrc<Self>) -> &mut Self {
    // SAFETY:
    // all constructor fns of UniqueSrc guarantee initialization of all elements
    unsafe { rc.start.as_mut() }
  }
  
  #[inline]
  fn new_unique_from_clone(&self) -> UniqueSrc<Self> where <Self as SrcTarget>::Item: Clone {
    UniqueSrc::single(T::clone(self))
  }
  
}

impl<T> SrcTarget for [T] {
  
  type Item = T;
  
}

impl<T> private::SealedSrcTarget for [T] {
  
  type Len = usize;
  
  #[inline]
  fn len_as_usize(len: Self::Len) -> usize {
    len
  }
  
  fn get(rc: &Src<Self>) -> &Self {
    let start = rc.start.as_ptr().cast_const();
    let len = rc.len;
    // SAFETY:
    // * all constructor fns of Src guarantee initialization of all elements; if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    unsafe { slice::from_raw_parts(start, len) }
  }
  
  fn get_unique(rc: &UniqueSrc<Self>) -> &Self {
    let start = rc.start.as_ptr().cast_const();
    let len = rc.len;
    // SAFETY:
    // * all constructor fns of Src guarantee initialization of all elements; if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    unsafe { slice::from_raw_parts(start, len) }
  }
  
  fn get_unique_mut(rc: &mut UniqueSrc<Self>) -> &mut Self {
    let start = rc.start.as_ptr();
    let len = rc.len;
    // SAFETY:
    // * all constructor fns of Src guarantee initialization of all elements; if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    unsafe { slice::from_raw_parts_mut(start, len) }
  }
  
  #[inline]
  fn new_unique_from_clone(&self) -> UniqueSrc<Self> where <Self as SrcTarget>::Item: Clone {
    UniqueSrc::cloned(self)
  }
  
}

impl SrcTarget for str {
  
  type Item = u8;
  
}

impl private::SealedSrcTarget for str {
  
  type Len = usize;
  
  #[inline]
  fn len_as_usize(len: Self::Len) -> usize {
    len
  }
  
  fn get(rc: &Src<Self>) -> &Self {
    let start = rc.start.as_ptr().cast_const();
    let len = rc.len;
    // SAFETY: all constructor fns of Src guarantee initialization of all elements (well, one of them unsafely passes that requirement on to the caller); if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    let slice: &[u8] = unsafe { slice::from_raw_parts(start, len) };
    // SAFETY: all constructor fns of Src<str> guarantee the contents are UTF8
    unsafe { str::from_utf8_unchecked(slice) }
  }
  
  fn get_unique(rc: &UniqueSrc<Self>) -> &Self {
    let start = rc.start.as_ptr().cast_const();
    let len = rc.len;
    // SAFETY: all constructor fns of Src guarantee initialization of all elements (well, one of them unsafely passes that requirement on to the caller); if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    let slice: &[u8] = unsafe { slice::from_raw_parts(start, len) };
    // SAFETY: all constructor fns of Src<str> guarantee the contents are UTF8
    unsafe { str::from_utf8_unchecked(slice) }
  }
  
  fn get_unique_mut(rc: &mut UniqueSrc<Self>) -> &mut Self {
    let start = rc.start.as_ptr();
    let len = rc.len;
    // SAFETY: all constructor fns of Src guarantee initialization of all elements (well, one of them unsafely passes that requirement on to the caller); if this is a sliced clone, Src::clone_from_bounds guarantees that start and len will still be valid
    let slice: &mut [u8] = unsafe { slice::from_raw_parts_mut(start, len) };
    // SAFETY: all constructor fns of Src<str> guarantee the contents are UTF8
    unsafe { str::from_utf8_unchecked_mut(slice) }
  }
  
  #[inline]
  fn new_unique_from_clone(&self) -> UniqueSrc<Self> {
    UniqueSrc::new(self)
  }
  
}

/// A sealed trait to encode the subset of [`SrcTarget`] that can contain multiple elements.
/// 
/// Either <code>Self = [\[T\]](prim@slice)</code> or <code>Self = [str]</code>.
pub trait SrcSlice: SrcTarget + private::SealedSrcSlice {}

impl<T> SrcSlice for [T] {}

impl<T> private::SealedSrcSlice for [T] {
  
  #[inline]
  fn validate_range(_this: &Src<Self>, _range: Range<usize>) {}
  
}

impl SrcSlice for str {}

impl private::SealedSrcSlice for str {
  
  fn validate_range(this: &Src<Self>, range: Range<usize>) {
    let s: &str = &**this;
    let _ = s[range]; // construct the slice just to trigger the appropriate errors if these indices are not at char boundaries
  }
  
}

mod private {
  
  use std::ops::Range;
  
  pub trait SealedSrcTarget {
    
    type Len: Copy + Default;
    
    fn len_as_usize(len: Self::Len) -> usize;
    
    fn get(rc: &super::Src<Self>) -> &Self where Self: super::SrcTarget;
    
    fn get_unique(rc: &super::UniqueSrc<Self>) -> &Self where Self: super::SrcTarget;
    
    fn get_unique_mut(rc: &mut super::UniqueSrc<Self>) -> &mut Self where Self: super::SrcTarget;
    
    fn new_unique_from_clone(&self) -> super::UniqueSrc<Self> where Self: super::SrcTarget, Self::Item: Clone;
    
  }
  
  pub trait SealedSrcSlice: SealedSrcTarget<Len = usize> {
    
    fn validate_range(this: &super::Src<Self>, range: Range<usize>) where Self: super::SrcSlice;
    
  }
  
}