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
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
use std::{fmt::{self, Debug, Formatter, Pointer}, marker::PhantomData, mem::forget, ptr::NonNull};

use crate::{inner::AllocUninit, InnerHeader, Src, SrcSlice, SrcTarget, UniqueSrc, WeakSrc};

/// `UninitSrc` is a version of [`Src`] that uniquely owns a new allocation before its body is initialized.
/// This is primarily used to construct self-referential data structures:
/// the [`downgrade`](UninitSrc::downgrade) method allows aquiring [weak references](WeakSrc) to this allocation before its body is initialized.
/// 
/// There are several helper methods for [`Src`] and [`UniqueSrc`] such as [`cyclic_from_fn`](Src::cyclic_from_fn) that may be simpler than `UninitSrc` for specific use cases.
/// 
/// `UninitSrc` pointers are always [root](crate#root)s,
/// because there is (at least at present) no way to track which elements are initialized and which are not,
/// and a non-[root](crate#root) `UninitSrc` would be useless if you couldn't initialize it.
/// 
/// Note that there is no way to construct or initialize an <code>UninitSrc\<[str]></code>;
/// however, as a [`str`] cannot contain a <code>[WeakSrc]\<[str]></code>, this type is useless with [`str`] anyway.
pub struct UninitSrc<T: SrcTarget + ?Sized> {
  
  // SAFETY:
  // requires:
  // * initialized from InnerHeader::new_inner::<T::Item>(_)
  pub(crate) header: NonNull<InnerHeader>,
  // 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> UninitSrc<T> {
  
  fn header(&self) -> &InnerHeader {
    // SAFETY:
    // * all constructor fns for Src initialize header from InnerHeader::new_inner::<T::Item>
    // * the header is only accessed from InnerHeader::get_header
    unsafe { InnerHeader::get_header(self.header) }
  }
  
  /// Creates a [root](crate#root) [`WeakSrc`] pointer to this allocation.
  /// 
  /// ```rust
  /// use slice_rc::{Src, UninitSrc, WeakSrc};
  /// 
  /// struct S {
  ///   
  ///   me: WeakSrc<S>,
  ///   
  /// }
  /// 
  /// let uninit = UninitSrc::single();
  /// let s = S { me: uninit.downgrade() };
  /// let s = uninit.init(s);
  /// assert!(Src::ptr_eq(&s, &s.me.upgrade().unwrap()));
  /// ```
  pub fn downgrade(&self) -> WeakSrc<T> {
    // safety note: the strong count is 0 until this UninitSrc is initialized into a Src, so the WeakSrc will never read or write from the body during the lifetime of the UninitSrc
    self.header().inc_weak_count();
    // SAFETY:
    // * all constructor fns for UninitSrc<T> initialize self.header from InnerHeader::new_inner::<T>
    // * the header is only accessed from InnerHeader::get_header
    let start = unsafe { InnerHeader::get_body_ptr::<T::Item>(self.header) };
    WeakSrc {
      // SAFETY: the safety invariant for self.header implies that of _.header
      header: self.header,
      // SAFETY: the start we just calculated meets the safety invariant by definition
      start,
      // SAFETY: the safety invariant for self.len implies tha tof _.len
      len: self.len,
      _phantom: PhantomData,
    }
  }
  
}

impl<T: SrcSlice + ?Sized> UninitSrc<T> {
  
  /// Returns the number of (uninitialized) elements in this `UninitSrc`.
  /// Because `UninitSrc` pointers are always [root](crate#root),
  /// this is also the total number of elements in this allocation.
  /// 
  /// ```rust
  /// use slice_rc::UninitSrc;
  /// 
  /// let uninit = UninitSrc::<[i32]>::new(3);
  /// assert_eq!(uninit.len(), 3);
  /// ```
  #[inline]
  pub fn len(&self) -> usize {
    self.len
  }
  
  /// Returns `true` if this `UninitSrc` has a length of `0`.
  /// 
  /// ```rust
  /// use slice_rc::UninitSrc;
  /// 
  /// let a = UninitSrc::<[i32]>::new(3);
  /// assert!(!a.is_empty());
  /// 
  /// let b = UninitSrc::<[i32]>::new(0);
  /// assert!(b.is_empty());
  /// ```
  #[inline]
  pub fn is_empty(&self) -> bool {
    self.len == 0
  }
  
}

impl<T: Sized> UninitSrc<T> {
  
  /// Constructs a new `UninitSrc` for a single value.
  /// 
  /// ```rust
  /// use slice_rc::{Src, UninitSrc};
  /// 
  /// let uninit = UninitSrc::<i32>::single();
  /// assert_eq!(uninit.as_slice().len(), 1);
  /// ```
  #[inline]
  pub fn single() -> UninitSrc<T> {
    let this = UninitSrc::<[T]>::new(1);
    debug_assert_eq!(this.len, 1);
    let this2 = UninitSrc {
      // SAFETY: the safety invariant of this.header is the same as this2.header
      header: this.header,
      // SAFETY: the safety invariant of this.len is implies that of this2.len
      len: (),
      _phantom: PhantomData,
    };
    forget(this);
    this2
  }
  
  /// Initializes this `UninitSrc` into an [`Src`] with the given value.
  /// 
  /// ```rust
  /// use slice_rc::{Src, UninitSrc};
  /// 
  /// let uninit = UninitSrc::single();
  /// let s: Src<_> = uninit.init(42);
  /// assert_eq!(*s, 42);
  /// assert_eq!(Src::root(&s).len(), 1);
  /// ```
  #[inline]
  pub fn init(self, value: T) -> Src<T> {
    UniqueSrc::into_shared(self.init_unique(value))
  }
  
  /// Initializes this `UninitSrc` into a [`UniqueSrc`] with the given value.
  /// 
  /// ```rust
  /// use slice_rc::{UninitSrc, UniqueSrc};
  /// 
  /// let uninit = UninitSrc::single();
  /// let s: UniqueSrc<_> = uninit.init_unique(42);
  /// assert_eq!(*s, 42);
  /// ```
  pub fn init_unique(self, value: T) -> UniqueSrc<T> {
    // SAFETY:
    // * all constructor fns for UninitSrc<T> initialize self.header from InnerHeader::new_inner::<T>
    // * the header is only accessed from InnerHeader::get_header
    let start = unsafe { InnerHeader::get_body_ptr::<T>(self.header) };
    // SAFETY: no one else has seen the body of the allocation (because the weaks only look at the header after the strong count has been initialized), so this write is okay
    unsafe { start.write(value); }
    let this = UniqueSrc {
      // SAFETY: the safety invariant of self.header implies that of this.header
      header: self.header,
      // SAFETY: after being initialized by start.write(_), the safety invariant of this.start is fulfilled by definition
      start,
      // SAFETY: the safety invariant of self.len implies that of this.len
      len: self.len,
      _phantom: PhantomData,
    };
    forget(self); // don't drop the weak held by the UninitSrc; it logically transfers to the Src
    this
  }
  
  /// Returns a `UninitSrc` 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::{UninitSrc, WeakSrc};
  /// 
  /// let single = UninitSrc::<i32>::single();
  /// let single_weak: WeakSrc<i32> = single.downgrade();
  /// let slice = single.as_slice();
  /// let slice_weak: WeakSrc<[i32]> = slice.downgrade();
  /// assert!(WeakSrc::ptr_eq(&single_weak, &slice_weak));
  /// ```
  #[inline]
  pub fn as_slice(self) -> UninitSrc<[T]> {
    let this = UninitSrc {
      // SAFETY: the safety invariant of self.header is the same as this.header
      header: self.header,
      // SAFETY: the safety invariant of self.len implies that of this.len
      len: 1,
      _phantom: PhantomData,
    };
    forget(self); // don't modify the weak count because this is logically the same UninitSrc
    this
  }
  
}

impl<T> UninitSrc<[T]> {
  
  /// Constructs a new `UninitSrc` for a slice of `len` values.
  /// 
  /// ```rust
  /// use slice_rc::UninitSrc;
  /// 
  /// let uninit = UninitSrc::<[i32]>::new(3);
  /// assert_eq!(uninit.len(), 3);
  /// ```
  #[inline]
  pub fn new(len: usize) -> UninitSrc<[T]> {
    let header = InnerHeader::new_inner::<T, AllocUninit>(len);
    Self {
      // SAFETY: the safety invariant of _.header is fulfilled by definition
      header,
      // SAFETY: the safety invariant of _.len is fulfilled by definition
      len,
      _phantom: PhantomData,
    }
  }
  
  /// Initializes this `UninitSrc` into an [`Src`] where each element is produced by calling `f` with that element's index while walking forward through the slice.
  /// 
  /// If `len == 0`, this produces an empty [`Src`] without ever calling `f`.
  /// 
  /// ```rust
  /// use slice_rc::UninitSrc;
  /// 
  /// let uninit = UninitSrc::new(5);
  /// let slice = uninit.init_from_fn(|i| i);
  /// assert_eq!(*slice, [0, 1, 2, 3, 4]);
  /// 
  /// let uninit2 = UninitSrc::new(8);
  /// let slice2 = uninit2.init_from_fn(|i| i * 2);
  /// assert_eq!(*slice2, [0, 2, 4, 6, 8, 10, 12, 14]);
  /// 
  /// let bool_uninit = UninitSrc::new(5);
  /// let bool_slice = bool_uninit.init_from_fn(|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::UninitSrc;
  /// let uninit = UninitSrc::new(6);
  /// let mut state = 1;
  /// let s = uninit.init_from_fn(|_| { 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::UninitSrc;
  /// # 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(move || {
  ///   let uninit = UninitSrc::new(10);
  ///   uninit.init_from_fn(|i| {
  ///     if i >= 5 { panic!() }
  ///     Droppable
  ///   })
  /// });
  /// 
  /// assert_eq!(DROPPED.get(), 5);
  /// ```
  #[inline]
  pub fn init_from_fn<F: FnMut(usize) -> T>(self, f: F) -> Src<[T]> {
    UniqueSrc::into_shared(self.init_unique_from_fn(f))
  }
  
  /// Initializes this `UninitSrc` into a [`UniqueSrc`] where each element is produced by calling `f` with that element's index while walking forward through the slice.
  /// 
  /// If `len == 0`, this produces an empty [`UniqueSrc`] without ever calling `f`.
  /// 
  /// ```rust
  /// use slice_rc::UninitSrc;
  /// 
  /// let uninit = UninitSrc::new(5);
  /// let slice = uninit.init_unique_from_fn(|i| i);
  /// assert_eq!(*slice, [0, 1, 2, 3, 4]);
  /// 
  /// let uninit2 = UninitSrc::new(8);
  /// let slice2 = uninit2.init_unique_from_fn(|i| i * 2);
  /// assert_eq!(*slice2, [0, 2, 4, 6, 8, 10, 12, 14]);
  /// 
  /// let bool_uninit = UninitSrc::new(5);
  /// let bool_slice = bool_uninit.init_unique_from_fn(|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::UninitSrc;
  /// let uninit = UninitSrc::new(6);
  /// let mut state = 1;
  /// let s = uninit.init_unique_from_fn(|_| { 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::UninitSrc;
  /// # 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(move || {
  ///   let uninit = UninitSrc::new(10);
  ///   uninit.init_unique_from_fn(|i| {
  ///     if i >= 5 { panic!() }
  ///     Droppable
  ///   })
  /// });
  /// 
  /// assert_eq!(DROPPED.get(), 5);
  /// ```
  pub fn init_unique_from_fn<F: FnMut(usize) -> T>(self, mut f: F) -> UniqueSrc<[T]> {
    let header = self.header();
    // SAFETY:
    // * all constructor fns for UninitSrc<T> initialize self.header from InnerHeader::new_inner::<T>
    // * the header is only accessed from InnerHeader::get_header
    let start = unsafe { InnerHeader::get_body_ptr::<T>(self.header) };
    let mut guard = PartialInitGuard::<T> { header: self.header, initialized: 0, _phantom: PhantomData };
    for i in 0..header.len() {
      // SAFETY:
      // * all constructor fns for UninitSrc<T> initialize self.header from InnerHeader::new_inner::<T>
      // * the header is only accessed from InnerHeader::get_header
      let ptr = unsafe { InnerHeader::get_elem_ptr::<T>(self.header, i) };
      let val = f(i);
      // SAFETY: no one else has seen the body of the allocation (because the weaks only look at the header after the strong count has been initialized), so this write is okay
      unsafe { ptr.write(val) };
      guard.initialized += 1;
    }
    // if all elements are successfully initialized, then forget the drop guard; in other words, the guard only drops the contents if a panic occurs part way through initialization
    forget(guard);
    let this = UniqueSrc {
      // SAFETY: the safety invariant of self.header is the same as this.header
      header: self.header,
      // SAFETY: after a successful initialization, the safety invariant of this.start is fulfilled by definition
      start,
      // SAFETY: the safety invariant of self.len is the same as this.len
      len: self.len,
      _phantom: PhantomData,
    };
    forget(self); // don't drop the weak held by the UninitSrc; it logically transfers to the Src
    this
  }
  
  /// Initializes this `UninitSrc` into an [`Src`] where each element is the type's [default](Default::default).
  /// 
  /// This method is essentially equivalent to <code>self.[init_from_fn](UninitSrc::init_from_fn)(|_| [Default::default]\())</code>.
  #[inline]
  pub fn init_from_default(self) -> Src<[T]> where T: Default {
    self.init_from_fn(|_| T::default())
  }
  
  /// Initializes this `UninitSrc` into a [`UniqueSrc`] where each element is the type's [default](Default::default).
  /// 
  /// This method is essentially equivalent to <code>self.[init_unique_from_fn](UninitSrc::init_unique_from_fn)(|_| [Default::default]\())</code>.
  #[inline]
  pub fn init_unique_from_default(self) -> UniqueSrc<[T]> where T: Default {
    self.init_unique_from_fn(|_| T::default())
  }
  
  /// Initializes this `UninitSrc` into an [`Src`] where each element is a clone of `value`.
  /// 
  /// This method is essentially equivalent to <code>self.[init_from_fn](UninitSrc::init_from_fn)(|_| value.[Clone::clone]\())</code>.
  #[inline]
  pub fn init_filled(self, value: &T) -> Src<[T]> where T: Clone {
    self.init_from_fn(|_| value.clone())
  }
  
  /// Initializes this `UninitSrc` into an [`UniqueSrc`] where each element is a clone of `value`.
  /// 
  /// This method is essentially equivalent to <code>self.[init_unique_from_fn](UninitSrc::init_unique_from_fn)(|_| value.[Clone::clone]\())</code>.
  #[inline]
  pub fn init_unique_filled(self, value: &T) -> UniqueSrc<[T]> where T: Clone {
    self.init_unique_from_fn(|_| value.clone())
  }
  
}

impl<T: SrcTarget + ?Sized> Debug for UninitSrc<T> {
  
  #[inline]
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "(UninitSrc)")
  }
  
}

impl<T: SrcTarget + ?Sized> Pointer for UninitSrc<T> {
  
  #[inline]
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    // SAFETY:
    // * all constructor fns for Src initialize header from InnerHeader::new_inner::<T::Item>
    // * the header is only accessed from InnerHeader::get_header
    // NOTE: the body is not expected to be initialized, but it is also not used
    let start = unsafe { InnerHeader::get_body_ptr::<T::Item>(self.header) };
    Pointer::fmt(&start, f)
  }
  
}

impl<T: SrcTarget + ?Sized> Drop for UninitSrc<T> {
  
  fn drop(&mut self) {
    // SAFETY:
    // * all constructor fns for UninitSrc initialize header from InnerHeader::new_inner::<T::Item>
    // * the header is only accessed from InnerHeader::get_header
    // NOTE: the UninitSrc logically holds one weak reference
    unsafe { InnerHeader::drop_weak::<T::Item>(self.header); }
  }
  
}

struct PartialInitGuard<T> {
  
  header: NonNull<InnerHeader>,
  initialized: usize,
  _phantom: PhantomData<*const T>,
  
}

impl<T> Drop for PartialInitGuard<T> {
  
  fn drop(&mut self) {
    // SAFETY:
    // * by the contract of this type, self.header is from an initialization fn from UninitSrc; all constructor fns for UninitSrc<T> initialize self.header from InnerHeader::new_inner::<T>
    // * by the contract of this type, the first self.initialized elements have been initialized
    // * the header is only accessed from InnerHeader::get_header
    // * by the contract of this type, self.header is from an initialization fn from UninitSrc that is panicking; therefore, no one else has seen or will see the body
    unsafe { InnerHeader::drop_body_up_to::<T>(self.header, self.initialized); }
  }
  
}

#[cfg(test)]
mod tests {
  
  use std::{cell::Cell, ops::Deref, panic::{catch_unwind, AssertUnwindSafe}};
  use crate::*;
  
  #[test]
  fn downgrade() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let w: WeakSrc<[u8]> = u.downgrade();
    assert!(w.upgrade().is_none());
    let s1: Src<[u8]> = u.init_from_default();
    let s2: Src<[u8]> = w.upgrade().unwrap();
    assert_eq!(s1, s2);
    assert!(Src::ptr_eq(&s1, &s2));
  }
  
  #[test]
  fn len() {
    let u: UninitSrc<[u8]> = UninitSrc::new(0);
    assert_eq!(u.len(), 0);
    let u: UninitSrc<[u8]> = UninitSrc::new(1);
    assert_eq!(u.len(), 1);
    let u: UninitSrc<[u8]> = UninitSrc::new(17);
    assert_eq!(u.len(), 17);
  }
  
  #[test]
  fn is_empty() {
    let u: UninitSrc<[u8]> = UninitSrc::new(0);
    assert!(u.is_empty());
    let u: UninitSrc<[u8]> = UninitSrc::new(1);
    assert!(!u.is_empty());
    let u: UninitSrc<[u8]> = UninitSrc::new(17);
    assert!(!u.is_empty());
  }
  
  #[test]
  fn single() {
    let u: UninitSrc<u8> = UninitSrc::single();
    let s: Src<u8> = u.init(42);
    assert!(Src::is_root(&s));
    let s: Src<[u8]> = Src::as_slice(&s);
    assert_eq!(s.len(), 1);
  }
  
  #[test]
  fn init() {
    let u: UninitSrc<u8> = UninitSrc::single();
    let s: Src<u8> = u.init(42);
    assert_eq!(*s, 42);
  }
  
  #[test]
  fn init_unique() {
    let u: UninitSrc<u8> = UninitSrc::single();
    let u: UniqueSrc<u8> = u.init_unique(42);
    assert_eq!(*u, 42);
  }
  
  #[test]
  fn as_slice() {
    let u: UninitSrc<u8> = UninitSrc::single();
    let u: UninitSrc<[u8]> = u.as_slice();
    assert_eq!(u.len(), 1);
  }
  
  #[test]
  fn new() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let s: Src<[u8]> = u.init_from_fn(|i| i as _);
    assert!(Src::is_root(&s));
    assert_eq!(s.len(), 3);
  }
  
  #[test]
  fn init_from_fn() {
    { // normal
      let u: UninitSrc<[u8]> = UninitSrc::new(3);
      let s: Src<[u8]> = u.init_from_fn(|i| i as _);
      assert_eq!(*s, [0, 1, 2]);
    }
    { // 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 u: UninitSrc<[DropFlagger<'_>]> = UninitSrc::new(drop_flags.len());
        let _: Src<[DropFlagger<'_>]> = u.init_from_fn(|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 init_unique_from_fn() {
    { // normal
      let u: UninitSrc<[u8]> = UninitSrc::new(3);
      let u: UniqueSrc<[u8]> = u.init_unique_from_fn(|i| i as _);
      assert_eq!(*u, [0, 1, 2]);
    }
    { // 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 u: UninitSrc<[DropFlagger<'_>]> = UninitSrc::new(drop_flags.len());
        let _: UniqueSrc<[DropFlagger<'_>]> = u.init_unique_from_fn(|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 init_from_default() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let s: Src<[u8]> = u.init_from_default();
    assert_eq!(*s, [0, 0, 0]);
  }
  
  #[test]
  fn init_unique_from_default() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let u: UniqueSrc<[u8]> = u.init_unique_from_default();
    assert_eq!(*u, [0, 0, 0]);
  }
  
  #[test]
  fn init_filled() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let s: Src<[u8]> = u.init_filled(&42);
    assert_eq!(*s, [42, 42, 42]);
  }
  
  #[test]
  fn init_unique_filled() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let u: UniqueSrc<[u8]> = u.init_unique_filled(&42);
    assert_eq!(*u, [42, 42, 42]);
  }
  
  #[test]
  fn drop() {
    let u: UninitSrc<[u8]> = UninitSrc::new(3);
    let w: WeakSrc<[u8]> = u.downgrade();
    assert!(w.upgrade().is_none());
    std::mem::drop(u);
    assert!(w.upgrade().is_none());
  }
  
}