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
//! Buffers for single-buffer and vectored I/O which tracking initializedness and how much has been
//! filled. Container types pointing to possibly-uninitialized memory such as
//! `Vec<MaybeUninit<Item>>`, `IoBox<Uninitialized>`, or `Box<[MaybeUninit<Item>]>` can be
//! transformed into their initialized variants via the [`Initialize`] trait, within requiring
//! unsafe code.
//!
//! This is an implementation of an API similar to what has been proposed in [this
//! RFC](https://github.com/sfackler/rfcs/blob/read-buf/text/0000-read-buf.md).

use core::fmt;
use core::mem::MaybeUninit;

use crate::initializer::BufferInitializer;
use crate::traits::Initialize;
use crate::wrappers::AsUninit;

pub struct Buffer<T> {
    pub(crate) initializer: BufferInitializer<T>,
    pub(crate) items_filled: usize,
}

/// A reference to a [`Buffer`], which is meant be a subset of the functionality offered by the
/// fully owned buffer.
///
/// For example, it neither allows reading from the unfilled region, nor swapping out the buffer
/// pointed to, with anything else.
pub struct BufferRef<'buffer, T> {
    // NOTE: The reference here is private, and never accessed using the API, _since we don't want
    // an API user to be able to replace a `&mut Buffer` with a completely different one_.
    inner: &'buffer mut Buffer<T>,
}

impl<T> Buffer<T> {
    /// Create a new buffer from an initializer.
    #[inline]
    pub const fn from_initializer(initializer: BufferInitializer<T>) -> Self {
        Self {
            initializer,
            items_filled: 0,
        }
    }
    /// Create a new buffer, defaulting to not being initialized, nor filled. Prefer
    /// [`new`](Buffer::new) if the buffer is already initialized.
    pub const fn uninit(inner: T) -> Self {
        Self::from_initializer(BufferInitializer::uninit(inner))
    }
    /// Move out the buffer initializer, which contains the inner buffer and initialization cursor,
    /// and get the filledness cursor.
    #[inline]
    pub fn into_raw_parts(self) -> (BufferInitializer<T>, usize) {
        let Self {
            initializer,
            items_filled,
        } = self;

        (initializer, items_filled)
    }
    #[inline]
    pub fn into_initializer(self) -> BufferInitializer<T> {
        self.initializer
    }
    /// Move out the inner buffer, being uninitialized or initialized based on whatever it was when
    /// this buffer was constructed.
    ///
    /// Use [`try_into_init`] if the buffer is initialized.
    ///
    /// [`try_into_init`]: #method.try_into_init
    #[inline]
    pub fn into_inner(self) -> T {
        self.into_initializer().into_inner()
    }

    /// Get the number of items that are currently filled, within the buffer. Note that this is
    /// different from the number of initialized items; use [`items_initialized`] for that.
    ///
    /// [`items_initialized`]: #method.items_initialized
    #[inline]
    pub const fn items_filled(&self) -> usize {
        self.items_filled
    }

    #[inline]
    pub fn by_ref(&mut self) -> BufferRef<'_, T> {
        BufferRef { inner: self }
    }

    #[inline]
    pub const fn initializer(&self) -> &BufferInitializer<T> {
        &self.initializer
    }

    #[inline]
    pub fn initializer_mut(&mut self) -> &mut BufferInitializer<T> {
        &mut self.initializer
    }
}
impl<T> Buffer<AsUninit<T>>
where
    T: core::ops::Deref + core::ops::DerefMut,
{
    pub fn new(init: T) -> Self {
        Self::from_initializer(BufferInitializer::new(init))
    }
}

impl<T> Buffer<T>
where
    T: Initialize,
{
    #[inline]
    pub fn capacity(&self) -> usize {
        self.initializer.capacity()
    }

    pub(crate) fn debug_assert_validity(&self) {
        self.initializer.debug_assert_validity();
        debug_assert!(self.items_filled() <= self.capacity());
        debug_assert!(self.items_filled() <= self.initializer.items_initialized());
    }
    /// Get the number of items that may be filled before the buffer is full.
    #[inline]
    pub fn remaining(&self) -> usize {
        debug_assert!(self.capacity() >= self.items_filled);
        self.capacity().wrapping_sub(self.items_filled)
    }
    /// Check whether the buffer is completely filled, and thus also initialized.
    #[inline]
    pub fn is_full(&self) -> bool {
        self.items_filled() == self.capacity()
    }
    /// Check whether the buffer is empty. It can be partially or fully initialized however.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.items_filled() == 0
    }
    /// Retrieve a shared slice to the filled part of the buffer.
    #[inline]
    pub fn filled_part(&self) -> &[T::Item] {
        unsafe {
            self.debug_assert_validity();

            let ptr = self.initializer.all_uninit().as_ptr();
            let len = self.items_filled;

            core::slice::from_raw_parts(ptr as *const T::Item, len)
        }
    }
    /// Retrieve a mutable slice to the filled part of the buffer.
    #[inline]
    pub fn filled_part_mut(&mut self) -> &mut [T::Item] {
        let orig_ptr = unsafe { self.initializer.all_uninit_mut().as_mut_ptr() };

        unsafe {
            self.debug_assert_validity();

            let ptr = orig_ptr;
            let len = self.items_filled;

            core::slice::from_raw_parts_mut(ptr as *mut T::Item, len)
        }
    }
    /// Get a shared slice to the unfilled part, which may be uninitialized.
    #[inline]
    pub fn unfilled_part(&self) -> &[MaybeUninit<T::Item>] {
        let (orig_ptr, orig_len) = {
            let orig = self.initializer.all_uninit();

            (orig.as_ptr(), orig.len())
        };

        unsafe {
            self.debug_assert_validity();

            let ptr = orig_ptr.add(self.items_filled);
            let len = orig_len.wrapping_sub(self.items_filled);

            core::slice::from_raw_parts(ptr, len)
        }
    }
    /// Get a mutable reference to the unfilled part of the buffer, which may overlap with the
    /// initialized-but-nonfilled region.
    ///
    /// # Safety
    ///
    /// Due to the possibility of an overlap between the part that is initialized and the part that
    /// is unfilled, the caller must ensure that the resulting slice is never used to deinitialize
    /// the buffer.
    ///
    /// It is thus recommended to use [`append`](Self::append) or
    /// [`fill_by_repeating`](Self::fill_by_repeating) instead, since those are the by far most
    /// common operations to do when initializing. However, code that requires interfacing with
    /// other APIs such as system calls, need to use this function.
    ///
    /// If mutable access really is needed for the unfilled region in safe code, consider using
    /// [`all_parts_mut`](Self::all_parts_mut).
    #[inline]
    pub unsafe fn unfilled_part_mut(&mut self) -> &mut [MaybeUninit<T::Item>] {
        let (orig_ptr, orig_len) = {
            let orig = self.initializer.all_uninit_mut();
            (orig.as_mut_ptr(), orig.len())
        };

        self.debug_assert_validity();

        let ptr = orig_ptr.add(self.items_filled);
        let len = orig_len.wrapping_sub(self.items_filled);

        core::slice::from_raw_parts_mut(ptr, len)
    }
    /// Borrow both the filled and unfilled parts immutably.
    #[inline]
    pub fn filled_unfilled_parts(&self) -> (&[T::Item], &[MaybeUninit<T::Item>]) {
        (self.filled_part(), self.unfilled_part())
    }
    /// Borrow the filled part, the unfilled but initialized part, and the unfilled and
    /// uninitialized part.
    #[inline]
    pub fn all_parts(&self) -> (&[T::Item], &[T::Item], &[MaybeUninit<T::Item>]) {
        (
            self.filled_part(),
            self.unfilled_init_part(),
            self.unfilled_uninit_part(),
        )
    }

    #[inline]
    pub fn all_parts_mut(
        &mut self,
    ) -> (&mut [T::Item], &mut [T::Item], &mut [MaybeUninit<T::Item>]) {
        let (all_ptr, all_len) = unsafe {
            let all = self.initializer.all_uninit_mut();

            (all.as_mut_ptr(), all.len())
        };

        unsafe {
            self.debug_assert_validity();

            let filled_base_ptr = all_ptr as *mut T::Item;
            let filled_len = self.items_filled;

            let unfilled_init_base_ptr = all_ptr.add(self.items_filled) as *mut T::Item;
            let unfilled_init_len = self
                .initializer
                .items_initialized()
                .wrapping_sub(self.items_filled);

            let unfilled_uninit_base_ptr = all_ptr.add(self.initializer.items_initialized());
            let unfilled_uninit_len = all_len.wrapping_sub(self.initializer.items_initialized());

            let filled = core::slice::from_raw_parts_mut(filled_base_ptr, filled_len);
            let unfilled_init =
                core::slice::from_raw_parts_mut(unfilled_init_base_ptr, unfilled_init_len);
            let unfilled_uninit =
                core::slice::from_raw_parts_mut(unfilled_uninit_base_ptr, unfilled_uninit_len);

            (filled, unfilled_init, unfilled_uninit)
        }
    }

    /// Borrow both the filled and the unfilled parts, mutably.
    ///
    /// # Safety
    ///
    /// This is unsafe as the uninit part may have items in it that are tracked to be initialized.
    /// It is hence the responsibility of the caller to ensure that the buffer is not deinitialized
    /// by writing [`MaybeUninit::uninit()`] to it.
    #[inline]
    pub unsafe fn filled_unfilled_parts_mut(
        &mut self,
    ) -> (&mut [T::Item], &mut [MaybeUninit<T::Item>]) {
        let (all_ptr, all_len) = {
            let all = self.initializer.all_uninit_mut();

            (all.as_mut_ptr(), all.len())
        };

        {
            self.debug_assert_validity();

            let filled_base_ptr = all_ptr as *mut T::Item;
            let filled_len = self.items_filled;

            let unfilled_base_ptr = all_ptr.add(self.items_filled);
            let unfilled_len = all_len.wrapping_sub(self.items_filled);

            let filled = core::slice::from_raw_parts_mut(filled_base_ptr, filled_len);
            let unfilled = core::slice::from_raw_parts_mut(unfilled_base_ptr, unfilled_len);

            (filled, unfilled)
        }
    }

    #[inline]
    pub fn unfilled_init_part(&self) -> &[T::Item] {
        unsafe {
            self.debug_assert_validity();

            let all = self.initializer.all_uninit();
            let all_ptr = all.as_ptr();

            let unfilled_init_base_ptr = all_ptr.add(self.items_filled) as *const T::Item;
            let unfilled_init_len = self
                .initializer
                .items_initialized()
                .wrapping_sub(self.items_filled);

            core::slice::from_raw_parts(unfilled_init_base_ptr, unfilled_init_len)
        }
    }

    /// Get the initialized part of the unfilled part, if there is any.
    #[inline]
    pub fn unfilled_init_part_mut(&mut self) -> &mut [T::Item] {
        let (_, unfilled_init, _) = self.all_parts_mut();

        unfilled_init
    }
    #[inline]
    pub fn unfilled_uninit_part(&self) -> &[MaybeUninit<T::Item>] {
        self.initializer.uninit_part()
    }
    /// Get the uninitialized part of the unfilled part, if there is any.
    #[inline]
    pub fn unfilled_uninit_part_mut(&mut self) -> &mut [MaybeUninit<T::Item>] {
        self.initializer.uninit_part_mut()
    }

    #[inline]
    pub fn unfilled_parts(&mut self) -> (&[T::Item], &[MaybeUninit<T::Item>]) {
        let (_, init, uninit) = self.all_parts();

        (init, uninit)
    }
    #[inline]
    pub fn unfilled_parts_mut(&mut self) -> (&mut [T::Item], &mut [MaybeUninit<T::Item>]) {
        let (_, init, uninit) = self.all_parts_mut();

        (init, uninit)
    }

    /// Revert the internal cursor to 0, forgetting about the initialized items.
    #[inline]
    pub fn revert_to_start(&mut self) {
        self.by_ref().revert_to_start()
    }
    #[inline]
    pub fn append(&mut self, slice: &[T::Item])
    where
        T::Item: Copy,
    {
        unsafe {
            // TODO: If this would ever turn out to be worth it, this could be optimized as bounds
            // checking gets redundant here.
            let unfilled_part = self.unfilled_part_mut();
            assert!(slice.len() <= unfilled_part.len());
            unfilled_part[..slice.len()].copy_from_slice(crate::cast_init_to_uninit_slice(slice));

            self.assume_init(slice.len())
        }
    }
    #[inline]
    pub fn advance(&mut self, count: usize) {
        assert!(
            self.initializer
                .items_initialized()
                .wrapping_sub(self.items_filled)
                >= count,
            "advancing filledness cursor beyond the initialized region ({} + {} = {} filled > {} init)",
            self.items_filled,
            count,
            self.items_filled + count,
            self.initializer.items_initialized,
        );
        self.items_filled = self.items_filled.wrapping_add(count);
    }
    #[inline]
    pub fn advance_to_init_part(&mut self) {
        self.items_filled = self.initializer.items_initialized;
    }
    // TODO: Method for increasing the items filled, but not the items initialized?
    /// Increment the counter that marks the progress of filling, as well as the initialization
    /// progress, `count` items.
    ///
    /// # Safety
    ///
    /// This does not initialize nor fill anything, and it is hence up to the user to ensure that
    /// no uninitialized items are marked initialized.
    pub unsafe fn assume_init(&mut self, count: usize) {
        self.items_filled += count;
        self.initializer.items_initialized =
            core::cmp::max(self.items_filled, self.initializer.items_initialized);

        self.debug_assert_validity();
    }
    /// Mark the buffer as fully filled and initialized, without actually filling the buffer.
    ///
    /// # Safety
    ///
    /// For this to be safe, the caller must ensure that every single item in the slice that the
    /// buffer wraps, is initialized.
    #[inline]
    pub unsafe fn assume_init_all(&mut self) {
        self.items_filled = self.capacity();
        self.initializer.items_initialized = self.capacity();
    }
    #[inline]
    pub fn fill_by_repeating(&mut self, item: T::Item)
    where
        T::Item: Copy,
    {
        unsafe {
            crate::fill_uninit_slice(self.unfilled_part_mut(), item);
            self.assume_init_all();
        }
    }
}
impl<T> Buffer<T>
where
    T: Initialize<Item = u8>,
{
    #[inline]
    pub fn fill_by_zeroing(&mut self) {
        self.fill_by_repeating(0_u8);
    }
}
impl<'a> Buffer<AsUninit<&'a mut [u8]>> {
    // TODO: Use a trait that makes the dynamic counter statically set to full.
    #[inline]
    pub fn from_slice_mut(slice: &'a mut [u8]) -> Self {
        let mut initializer = BufferInitializer::new(slice);
        unsafe {
            initializer.advance_to_end();
        }
        Self::from_initializer(initializer)
    }
}
impl<'a> Buffer<&'a mut [MaybeUninit<u8>]> {
    #[inline]
    pub fn from_uninit_slice_mut(slice: &'a mut [MaybeUninit<u8>]) -> Self {
        Self::uninit(slice)
    }
}

impl<'buffer, T> BufferRef<'buffer, T> {
    #[inline]
    pub fn items_filled(&self) -> usize {
        self.inner.items_filled()
    }

    /// Reborrow the inner buffer, getting a buffer reference with a shorter lifetime.
    #[inline]
    pub fn by_ref(&mut self) -> BufferRef<'_, T> {
        BufferRef { inner: self.inner }
    }
}

impl<'buffer, T> BufferRef<'buffer, T>
where
    T: Initialize,
{
    #[inline]
    pub fn remaining(&self) -> usize {
        self.inner.remaining()
    }
    #[inline]
    pub fn unfilled_parts(&mut self) -> (&mut [T::Item], &mut [MaybeUninit<T::Item>]) {
        self.inner.unfilled_parts_mut()
    }
    /// Get a mutable and possibly-uninitialized reference to all of the buffer.
    ///
    /// # Safety
    ///
    /// The caller must not allow safe code to de-initialize the resulting slice.
    #[inline]
    pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<T::Item>] {
        self.inner.unfilled_part_mut()
    }
    /// Advance the counter of the number of items filled.
    ///
    /// The number of items that are initialized is also updated accordingly, so that the number of
    /// items initialized is always greater than or equal to the number of items filled.
    ///
    /// # Safety
    ///
    /// The caller must uphold the initialization invariant.
    #[inline]
    pub unsafe fn advance(&mut self, count: usize) {
        self.inner.assume_init(count)
    }
    /// Advance the counter of the number of items filled, and the number of items initialized, to
    /// the end of the buffer.
    ///
    /// # Safety
    ///
    /// The caller must uphold the initialization invariant.
    #[inline]
    pub unsafe fn advance_all(&mut self) {
        self.inner.assume_init_all();
    }
    #[inline]
    pub fn revert_to_start(&mut self) {
        self.inner.revert_to_start()
    }
    #[inline]
    pub fn fill_by_repeating(&mut self, item: T::Item)
    where
        T::Item: Copy,
    {
        self.inner.fill_by_repeating(item)
    }
    #[inline]
    pub fn append(&mut self, slice: &[T::Item])
    where
        T::Item: Copy,
    {
        self.inner.append(slice)
    }
}
impl<T> BufferRef<'_, T>
where
    T: Initialize<Item = u8>,
{
    #[inline]
    pub fn fill_by_zeroing(&mut self) {
        self.inner.fill_by_zeroing()
    }
}

impl<T> fmt::Debug for Buffer<T>
where
    T: Initialize,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ptr = self.initializer().all_uninit().as_ptr();
        let items_init = self.initializer().items_initialized();
        let items_filled = self.items_filled();
        let total = self.capacity();

        if f.alternate() {
            let init_percentage = items_init as f64 / total as f64 * 100.0;
            let filled_percentage = items_filled as f64 / total as f64 * 100.0;
            write!(
                f,
                "[buffer at {:?}, {} B filled ({:.1}%), {} B init ({:.1}%), {} B total]",
                ptr, items_filled, filled_percentage, items_init, init_percentage, total
            )
        } else {
            write!(
                f,
                "[buffer at {:?}, {}/{}/{}]",
                ptr, items_filled, items_init, total
            )
        }
    }
}

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

    #[test]
    fn basic_buffer_ops() {
        let mut buffer = Buffer::uninit([MaybeUninit::<u8>::uninit(); 32]);
        assert_eq!(buffer.capacity(), 32);
        assert_eq!(buffer.remaining(), 32);
        assert!(buffer.is_empty());
        assert!(!buffer.is_full());
        assert!(buffer.initializer().is_completely_uninit());
        assert!(!buffer.initializer().is_completely_init());
        assert_eq!(buffer.initializer().items_initialized(), 0);
        assert_eq!(buffer.initializer().remaining(), 32);
        assert_eq!(buffer.filled_part(), &[]);
        assert_eq!(buffer.unfilled_part().len(), 32);
        assert_eq!(buffer.filled_part_mut(), &mut []);
        assert_eq!(unsafe { buffer.unfilled_part_mut().len() }, 32);
        // TODO: more

        let (filled, unfilled_init, unfilled_uninit) = buffer.all_parts();
        assert_eq!(filled, &[]);
        assert_eq!(unfilled_init, &[]);
        assert_eq!(unfilled_uninit.len(), 32);

        let (filled, unfilled_init, unfilled_uninit) = buffer.all_parts_mut();
        assert_eq!(filled, &mut []);
        assert_eq!(unfilled_init, &mut []);
        assert_eq!(unfilled_uninit.len(), 32);

        let src = b"I am a really nice slice!";
        let modified = b"I am a really wise slice!";

        buffer.append(src);

        assert!(!buffer.is_empty());
        assert!(!buffer.is_full());
        assert_eq!(buffer.filled_part(), src);
        assert_eq!(buffer.filled_part_mut(), src);
        assert!(!buffer.initializer().is_completely_init());
        assert!(!buffer.initializer().is_completely_uninit());
        assert_eq!(buffer.initializer().init_part(), src);
        assert_eq!(buffer.initializer_mut().init_part_mut(), src);
        assert_eq!(buffer.items_filled(), src.len());
        assert_eq!(buffer.remaining(), 32 - src.len());

        {
            let slice_mut = buffer.filled_part_mut();
            slice_mut[14] = b'w';
            slice_mut[16] = b's';
        }

        assert!(!buffer.is_empty());
        assert!(!buffer.is_full());
        assert_eq!(buffer.filled_part(), modified);
        assert_eq!(buffer.filled_part_mut(), modified);
        assert_eq!(buffer.initializer().init_part(), modified);
        assert_eq!(buffer.initializer_mut().init_part_mut(), modified);
        assert_eq!(buffer.items_filled(), src.len());
        assert_eq!(buffer.remaining(), 32 - src.len());
        assert_eq!(buffer.unfilled_part().len(), 32 - src.len());
        assert_eq!(unsafe { buffer.unfilled_part_mut().len() }, 32 - src.len());

        let mut initializer = buffer.into_initializer();

        assert_eq!(initializer.items_initialized(), modified.len());
        assert_eq!(initializer.remaining(), 7);
        initializer.partially_fill_uninit_part(3_usize, 0xFF_u8);
        initializer.partially_zero_uninit_part(1_usize);
        assert_eq!(initializer.remaining(), 3);

        let modified_and_garbage_items = b"I am a really wise slice!\xFF\xFF\xFF\x00";
        let (init, uninit) = initializer.init_uninit_parts();
        assert_eq!(init, modified_and_garbage_items);
        assert_eq!(uninit.len(), 3);

        let (init, uninit) = initializer.init_uninit_parts_mut();
        assert_eq!(init, modified_and_garbage_items);
        init[2] = b'e';
        assert_eq!(uninit.len(), 3);

        let mut buffer = Buffer::from_initializer(initializer);
        buffer.advance(modified.len());

        let (filled, unfilled_init, unfilled_uninit) = buffer.all_parts();
        let modified_again = b"I em a really wise slice!";
        assert_eq!(filled, modified_again);
        assert_eq!(unfilled_init, b"\xFF\xFF\xFF\x00");
        assert_eq!(unfilled_uninit.len(), 3);

        let (filled, unfilled_init, unfilled_uninit) = buffer.all_parts_mut();
        assert_eq!(filled, modified_again);
        assert_eq!(unfilled_init, b"\xFF\xFF\xFF\x00");
        unfilled_init[2] = b'\x13';
        unfilled_init[3] = b'\x37';
        assert_eq!(unfilled_init, b"\xFF\xFF\x13\x37");
        assert_eq!(unfilled_uninit.len(), 3);

        let rest = b" Right?";
        buffer.append(rest);

        assert_eq!(buffer.items_filled(), 32);
        assert!(!buffer.is_empty());
        assert!(buffer.is_full());
        assert_eq!(buffer.remaining(), 0);

        let total = b"I em a really wise slice! Right?";
        assert_eq!(buffer.filled_part(), total);
        assert_eq!(buffer.filled_part_mut(), total);
        assert_eq!(buffer.initializer().remaining(), 0);
        assert_eq!(buffer.initializer().items_initialized(), 32);
        assert!(buffer.initializer().is_completely_init());
        assert!(!buffer.initializer().is_completely_uninit());

        buffer.advance_to_init_part();

        // TODO: Shorthand?
        let initialized: [u8; 32] = buffer.into_initializer().try_into_init().unwrap().into();
        assert_eq!(&initialized, total);
    }
    #[test]
    fn debug_impl() {
        let array = [MaybeUninit::<u8>::uninit(); 32];
        let mut buffer = Buffer::uninit(array);
        buffer.append(b"Hello, world!");
        buffer.initializer_mut().partially_zero_uninit_part(13);

        assert_eq!(
            format!("{:?}", buffer),
            format!(
                "[buffer at {:p}, 13/26/32]",
                buffer.initializer().all_uninit().as_ptr()
            )
        );
        assert_eq!(
            format!("{:#?}", buffer),
            format!(
                "[buffer at {:p}, 13 B filled (40.6%), 26 B init (81.2%), 32 B total]",
                buffer.initializer().all_uninit().as_ptr()
            )
        );
    }
}