rtrb 0.3.3

A realtime-safe single-producer single-consumer ring buffer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! Writing and reading multiple items at once into and from a [`RingBuffer`].
//!
//! Multiple items at once can be moved from an iterator into the ring buffer by using
//! [`Producer::write_chunk_uninit()`] followed by [`WriteChunkUninit::fill_from_iter()`].
//! Alternatively, mutable access to the (uninitialized) slots of the chunk can be obtained with
//! [`WriteChunkUninit::as_mut_slices()`], which requires writing some `unsafe` code.
//! To avoid that, [`Producer::write_chunk()`] can be used,
//! which initializes all slots with their [`Default`] value
//! and provides mutable access by means of [`WriteChunk::as_mut_slices()`].
//!
//! Multiple items at once can be moved out of the ring buffer by using
//! [`Consumer::read_chunk()`] and iterating over the returned [`ReadChunk`]
//! (or by explicitly calling [`ReadChunk::into_iter()`]).
//! Immutable access to the slots of the chunk can be obtained with [`ReadChunk::as_slices()`].
//!
//! If the item type `T` implements [`Copy`], the convenience functions
//! [`Producer::push_partial_slice()`], [`Consumer::pop_partial_slice()`]
//! and [`Consumer::pop_partial_slice_uninit()`] can be used.
//!
//! # Examples
//!
//! The following examples use a single thread for simplicity, but in a real application,
//! `producer` and `consumer` would of course live on different threads:
//!
//! If the trait bound `T: Copy` is satisfied,
//! [`Producer::push_partial_slice()`] and [`Consumer::pop_partial_slice()`]
//! (and [`Consumer::pop_partial_slice_uninit()`]) can be used.
//!
//! ```
//! use rtrb::RingBuffer;
//!
//! let (mut producer, mut consumer) = RingBuffer::new(4);
//!
//! let source = vec![1, 2, 3, 4, 5, 6];
//! let (pushed, remainder) = producer.push_partial_slice(&source);
//! assert_eq!(pushed, [1, 2, 3, 4]);
//! assert_eq!(remainder, [5, 6]);
//!
//! let mut destination = vec![0; 3];
//! let (popped, remainder) = consumer.pop_partial_slice(&mut destination);
//! assert_eq!(popped, [1, 2, 3]);
//! assert_eq!(remainder, []);
//! assert_eq!(destination, [1, 2, 3]);
//!
//! let (popped, remainder) = consumer.pop_partial_slice(&mut destination);
//! assert_eq!(popped, [4]);
//! assert_eq!(remainder, [2, 3]);
//! // The returned slices are mutable sub-slices into `destination`.
//! remainder[0] = 99;
//! assert_eq!(destination, [4, 99, 3]);
//! ```
//!
//! If this convenience interface is too limited (or if `T` is not `Copy`)
//! the more fundamental methods [`Producer::write_chunk()`],
//! [`Producer::write_chunk_uninit()`] and [`Consumer::read_chunk()`] can be used.
//!
//! ```
//! use rtrb::RingBuffer;
//!
//! let (mut producer, mut consumer) = RingBuffer::new(5);
//!
//! if let Ok(chunk) = producer.write_chunk_uninit(4) {
//!     chunk.fill_from_iter([10, 11, 12]);
//!     // Note that we requested 4 slots but we've only written to 3 of them!
//! } else {
//!     unreachable!();
//! }
//!
//! assert_eq!(producer.slots(), 2);
//! assert_eq!(consumer.slots(), 3);
//!
//! if let Ok(chunk) = consumer.read_chunk(2) {
//!     assert_eq!(chunk.into_iter().collect::<Vec<_>>(), [10, 11]);
//! } else {
//!     unreachable!();
//! }
//!
//! // One element is still in the queue:
//! assert_eq!(consumer.peek(), Ok(&12));
//!
//! let data = vec![20, 21, 22, 23];
//! producer.push_entire_slice(&data).unwrap();
//!
//! assert!(producer.is_full());
//! assert_eq!(consumer.slots(), 5);
//!
//! let mut v = Vec::<i32>::with_capacity(5);
//! if let Ok(chunk) = consumer.read_chunk(5) {
//!     let (first, second) = chunk.as_slices();
//!     v.extend(first);
//!     v.extend(second);
//!     chunk.commit_all();
//! } else {
//!     unreachable!();
//! }
//! assert_eq!(v, [12, 20, 21, 22, 23]);
//! assert!(consumer.is_empty());
//! ```
//!
//! The iterator API can be used to move items from one ring buffer to another:
//!
//! ```
//! use rtrb::{Consumer, Producer};
//!
//! fn move_items<T>(src: &mut Consumer<T>, dst: &mut Producer<T>) -> usize {
//!     let n = src.slots().min(dst.slots());
//!     dst.write_chunk_uninit(n).unwrap().fill_from_iter(src.read_chunk(n).unwrap())
//! }
//! ```
//!
//! Write as many slots as possible, given an iterator
//! (and return the number of written slots):
//!
//! ```
//! use rtrb::{Producer, chunks::ChunkError::TooFewSlots};
//!
//! fn push_from_iter<T, I>(queue: &mut Producer<T>, iter: I) -> usize
//! where
//!     T: Default,
//!     I: IntoIterator<Item = T>,
//! {
//!     let iter = iter.into_iter();
//!     let n = match iter.size_hint() {
//!         (_, None) => queue.slots(),
//!         (_, Some(n)) => n,
//!     };
//!     let chunk = match queue.write_chunk_uninit(n) {
//!         Ok(chunk) => chunk,
//!         // Remaining slots are returned, this will always succeed:
//!         Err(TooFewSlots(n)) => queue.write_chunk_uninit(n).unwrap(),
//!     };
//!     chunk.fill_from_iter(iter)
//! }
//! ```

use core::fmt;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::sync::atomic::Ordering;

use crate::{Consumer, CopyToUninit, Producer};

// This is used in the documentation.
#[allow(unused_imports)]
use crate::RingBuffer;

impl<T> Producer<T> {
    /// Returns `n` slots (initially containing their [`Default`] value) for writing.
    ///
    /// [`WriteChunk::as_mut_slices()`] provides mutable access to the slots.
    /// After writing to those slots, they explicitly have to be made available
    /// to be read by the [`Consumer`] by calling [`WriteChunk::commit()`]
    /// or [`WriteChunk::commit_all()`].
    ///
    /// For an alternative that does not require the trait bound [`Default`],
    /// see [`Producer::write_chunk_uninit()`].
    ///
    /// If items are supposed to be moved from an iterator into the ring buffer,
    /// [`Producer::write_chunk_uninit()`] followed by [`WriteChunkUninit::fill_from_iter()`]
    /// can be used.
    ///
    /// # Errors
    ///
    /// If not enough slots are available, an error
    /// (containing the number of available slots) is returned.
    /// Use [`Producer::slots()`] to obtain the number of available slots beforehand.
    ///
    /// # Examples
    ///
    /// See the documentation of the [`chunks`](crate::chunks#examples) module.
    pub fn write_chunk(&mut self, n: usize) -> Result<WriteChunk<'_, T>, ChunkError>
    where
        T: Default,
    {
        self.write_chunk_uninit(n).map(WriteChunk::from)
    }

    /// Returns `n` (uninitialized) slots for writing.
    ///
    /// [`WriteChunkUninit::as_mut_slices()`] provides mutable access
    /// to the uninitialized slots.
    /// After writing to those slots, they explicitly have to be made available
    /// to be read by the [`Consumer`] by calling [`WriteChunkUninit::commit()`]
    /// or [`WriteChunkUninit::commit_all()`].
    ///
    /// Alternatively, [`WriteChunkUninit::fill_from_iter()`] can be used
    /// to move items from an iterator into the available slots.
    /// All moved items are automatically made available to be read by the [`Consumer`].
    ///
    /// # Errors
    ///
    /// If not enough slots are available, an error
    /// (containing the number of available slots) is returned.
    /// Use [`Producer::slots()`] to obtain the number of available slots beforehand.
    ///
    /// # Safety
    ///
    /// This function itself is safe, as is [`WriteChunkUninit::fill_from_iter()`].
    /// However, when using [`WriteChunkUninit::as_mut_slices()`],
    /// the user has to make sure that the relevant slots have been initialized
    /// before calling [`WriteChunkUninit::commit()`] or [`WriteChunkUninit::commit_all()`].
    ///
    /// For a safe alternative that provides mutable slices of [`Default`]-initialized slots,
    /// see [`Producer::write_chunk()`].
    ///
    /// # Examples
    ///
    /// See the documentation of the [`chunks`](crate::chunks#examples) module.
    pub fn write_chunk_uninit(&mut self, n: usize) -> Result<WriteChunkUninit<'_, T>, ChunkError> {
        let tail = self.cached_tail.get();

        // Check if the queue has *possibly* not enough slots.
        if self.buffer.capacity - self.buffer.distance(self.cached_head.get(), tail) < n {
            // Refresh the head ...
            let head = self.buffer.head.load(Ordering::Acquire);
            self.cached_head.set(head);

            // ... and check if there *really* are not enough slots.
            let slots = self.buffer.capacity - self.buffer.distance(head, tail);
            if slots < n {
                return Err(ChunkError::TooFewSlots(slots));
            }
        }
        let tail = self.buffer.collapse_position(tail);
        let first_len = n.min(self.buffer.capacity - tail);
        Ok(WriteChunkUninit {
            // SAFETY: tail has been updated to a valid position.
            first_ptr: unsafe { self.buffer.data_ptr.add(tail) },
            first_len,
            second_ptr: self.buffer.data_ptr,
            second_len: n - first_len,
            producer: self,
        })
    }
}

impl<T: Copy> Producer<T> {
    /// Copies as many items as possible from the given `slice` into the ring buffer.
    ///
    /// The written slots are automatically made available to be read by the [`Consumer`].
    ///
    /// Returns two sub-slices of `slice`:
    /// - The part that has been copied into the ring buffer (possibly empty).
    /// - The unused remainder (possibly empty).
    ///
    /// To copy an entire slice (and fail otherwise), [`Producer::push_entire_slice()`] can be used.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtrb::Producer;
    ///
    /// fn push_at_least_one_element<'a>(
    ///     p: &mut Producer<i32>,
    ///     s: &'a [i32],
    /// ) -> Result<&'a [i32], &'a [i32]> {
    ///     match p.push_partial_slice(s) {
    ///         ([], remainder) => Err(remainder),
    ///         (_, remainder) => Ok(remainder),
    ///     }
    /// }
    ///
    /// fn block_while_pushing_entire_slice(p: &mut Producer<i32>, mut s: &[i32]) {
    ///     while let (_, remainder @ [_, ..]) = p.push_partial_slice(s) {
    ///         std::thread::yield_now();
    ///         s = remainder;
    ///     }
    /// }
    /// ```
    ///
    /// For more examples, see the documentation of the [`chunks`](crate::chunks#examples) module.
    pub fn push_partial_slice<'a>(&mut self, slice: &'a [T]) -> (&'a [T], &'a [T]) {
        let slots = if self.cached_slots() < slice.len() {
            slice.len().min(self.slots())
        } else {
            slice.len()
        };
        let (pushed, remainder) = slice.split_at(slots);
        // With MSRV 1.58, unwrap_unchecked() can be used.
        match self.push_entire_slice(pushed) {
            Ok(()) => {}
            // SAFETY: The requested slots are available.
            Err(_) => unsafe { core::hint::unreachable_unchecked() },
        };
        (pushed, remainder)
    }

    /// Copies all items from the given `slice` into the ring buffer.
    ///
    /// The written slots are automatically made available to be read by the [`Consumer`].
    ///
    /// To copy only into the available slots, [`Producer::push_partial_slice()`] can be used.
    ///
    /// # Errors
    ///
    /// If not enough free space is available in the ring buffer,
    /// a [`ChunkError`] with the available slots is returned.
    pub fn push_entire_slice(&mut self, slice: &[T]) -> Result<(), ChunkError> {
        let mut chunk = self.write_chunk_uninit(slice.len())?;
        let (one, two) = chunk.as_mut_slices();
        let mid = one.len();
        // NB: If slice.is_empty(), chunk will be empty as well and the following are no-ops:
        slice[..mid].copy_to_uninit(one);
        slice[mid..].copy_to_uninit(two);
        // SAFETY: All slots have been initialized
        unsafe { chunk.commit_all() };
        Ok(())
    }
}

impl<T> Consumer<T> {
    /// Returns `n` slots for reading.
    ///
    /// [`ReadChunk::as_slices()`] provides immutable access to the slots.
    /// After reading from those slots, they explicitly have to be made available
    /// to be written again by the [`Producer`] by calling [`ReadChunk::commit()`]
    /// or [`ReadChunk::commit_all()`].
    ///
    /// Alternatively, items can be moved out of the [`ReadChunk`] using iteration
    /// because it implements [`IntoIterator`]
    /// ([`ReadChunk::into_iter()`] can be used to explicitly turn it into an [`Iterator`]).
    /// All moved items are automatically made available to be written again by the [`Producer`].
    ///
    /// # Errors
    ///
    /// If not enough slots are available, an error
    /// (containing the number of available slots) is returned.
    /// Use [`Consumer::slots()`] to obtain the number of available slots beforehand.
    ///
    /// # Examples
    ///
    /// See the documentation of the [`chunks`](crate::chunks#examples) module.
    pub fn read_chunk(&mut self, n: usize) -> Result<ReadChunk<'_, T>, ChunkError> {
        let head = self.cached_head.get();

        // Check if the queue has *possibly* not enough slots.
        if self.buffer.distance(head, self.cached_tail.get()) < n {
            // Refresh the tail ...
            let tail = self.buffer.tail.load(Ordering::Acquire);
            self.cached_tail.set(tail);

            // ... and check if there *really* are not enough slots.
            let slots = self.buffer.distance(head, tail);
            if slots < n {
                return Err(ChunkError::TooFewSlots(slots));
            }
        }

        let head = self.buffer.collapse_position(head);
        let first_len = n.min(self.buffer.capacity - head);
        Ok(ReadChunk {
            // SAFETY: head has been updated to a valid position.
            first_ptr: unsafe { self.buffer.data_ptr.add(head) },
            first_len,
            second_ptr: self.buffer.data_ptr,
            second_len: n - first_len,
            consumer: self,
        })
    }
}

impl<T: Copy> Consumer<T> {
    /// Copies as many items as possible from the ring buffer to the given `slice`.
    ///
    /// The copied slots are automatically made available to be written again by the [`Producer`].
    ///
    /// Returns two sub-slices of `slice`:
    /// - The part that has been filled with data from the ring buffer (possibly empty).
    /// - The unused remainder (possibly empty).
    ///
    /// To copy an entire slice (and fail otherwise), [`Consumer::pop_entire_slice()`] can be used.
    /// To copy into an uninitialized slice, [`Consumer::pop_partial_slice_uninit()`] can be used.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtrb::Consumer;
    ///
    /// fn pop_at_least_one_element<'a>(
    ///     c: &mut Consumer<i32>,
    ///     s: &'a mut [i32],
    /// ) -> Result<&'a mut [i32], &'a mut [i32]> {
    ///     match c.pop_partial_slice(s) {
    ///         ([], remainder) => Err(remainder),
    ///         (_, remainder) => Ok(remainder),
    ///     }
    /// }
    ///
    /// fn block_while_popping_entire_slice(c: &mut Consumer<i32>, mut s: &mut [i32]) {
    ///     while let (_, remainder @ [_, ..]) = c.pop_partial_slice(s) {
    ///         std::thread::yield_now();
    ///         s = remainder;
    ///     }
    /// }
    /// ```
    ///
    /// For more examples, see the documentation of the [`chunks`](crate::chunks#examples) module.
    pub fn pop_partial_slice<'a>(&mut self, slice: &'a mut [T]) -> (&'a mut [T], &'a mut [T]) {
        // SAFETY: Transmuting &mut [T] to &mut [MaybeUninit<T>] is generally unsafe!
        // However, since we can guarantee that only valid T values will ever be written,
        // and the reference never leaves our control, it should be fine.
        let (popped, remainder) =
            unsafe { self.pop_partial_slice_uninit(&mut *(slice as *mut [_] as *mut _)) };
        // NB: This can be replaced by `assume_init_mut()` once stabilized:
        // SAFETY: `remainder` is a subslice of the original initialized buffer.
        (popped, unsafe { &mut *(remainder as *mut _ as *mut [_]) })
    }

    /// Copies as many items as possible from the ring buffer to the given uninitialized `slice`.
    ///
    /// The copied slots are automatically made available to be written again by the [`Producer`].
    ///
    /// Returns two sub-slices of `slice`:
    /// - The part that has been filled with data from the ring buffer (possibly empty).
    /// - The unused remainder (possibly empty).
    ///
    /// The first of the returned slices has been initialized,
    /// while the second one remains uninitialized.
    ///
    /// To copy an entire slice (and fail otherwise),
    /// [`Consumer::pop_entire_slice_uninit()`] can be used.
    /// To copy into an initialized slice, [`Consumer::pop_partial_slice()`] can be used.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::mem::MaybeUninit;
    ///
    /// use rtrb::Consumer;
    ///
    /// fn pop_at_least_one_element_uninit<'a>(
    ///     c: &mut Consumer<i32>,
    ///     s: &'a mut [MaybeUninit<i32>],
    /// ) -> Result<&'a mut [MaybeUninit<i32>], &'a mut [MaybeUninit<i32>]> {
    ///     match c.pop_partial_slice_uninit(s) {
    ///         ([], remainder) => Err(remainder),
    ///         (_, remainder) => Ok(remainder),
    ///     }
    /// }
    ///
    /// fn block_while_popping_entire_slice_uninit(
    ///     c: &mut Consumer<i32>,
    ///     mut s: &mut [MaybeUninit<i32>],
    /// ) {
    ///     while let (_, remainder @ [_, ..]) = c.pop_partial_slice_uninit(s) {
    ///         std::thread::yield_now();
    ///         s = remainder;
    ///     }
    /// }
    /// ```
    ///
    /// Typically, we might get an uninitialized buffer via FFI,
    /// but in this example we are using the uninitialized part of a [`Vec`]:
    ///
    /// ```
    /// use std::mem::MaybeUninit;
    ///
    /// use rtrb::RingBuffer;
    ///
    /// let (mut producer, mut consumer) = RingBuffer::new(4);
    /// let (_, remainder) = producer.push_partial_slice(&[1, 2, 3]);
    /// assert!(remainder.is_empty());
    /// let mut buffer = Vec::with_capacity(5);
    /// let buffer_uninit = buffer.spare_capacity_mut();
    /// let (popped, remainder) = consumer.pop_partial_slice_uninit(buffer_uninit);
    /// assert_eq!(popped, [1, 2, 3]);
    /// // The returned slices are mutable ...
    /// popped[0] = -42;
    /// // ... but the second one is still uninitialized:
    /// remainder[0] = MaybeUninit::new(99);
    /// // All this happened in the uninitialized part of the buffer,
    /// // which is still "officially" empty:
    /// assert!(buffer.is_empty());
    /// // SAFETY: The first 4 elements have been initialized.
    /// unsafe {
    ///     buffer.set_len(4);
    /// }
    /// assert_eq!(buffer, [-42, 2, 3, 99]);
    /// ```
    #[inline]
    pub fn pop_partial_slice_uninit<'a>(
        &mut self,
        slice: &'a mut [MaybeUninit<T>],
    ) -> (&'a mut [T], &'a mut [MaybeUninit<T>]) {
        let slots = if self.cached_slots() < slice.len() {
            slice.len().min(self.slots())
        } else {
            slice.len()
        };
        let (buffer, remainder) = slice.split_at_mut(slots);
        // With MSRV 1.58, unwrap_unchecked() can be used.
        let popped = match self.pop_entire_slice_uninit(buffer) {
            Ok(popped) => popped,
            // SAFETY: The requested slots are available.
            Err(_) => unsafe { core::hint::unreachable_unchecked() },
        };
        (popped, remainder)
    }

    /// Copies as many items from the ring buffer as to fill the given `slice`.
    ///
    /// The copied slots are automatically made available to be written again by the [`Producer`].
    ///
    /// # Errors
    ///
    /// If not enough data is available in the ring buffer, no items are copied and
    /// a [`ChunkError`] with the available items is returned.
    ///
    /// To copy only the available slots, [`Consumer::pop_partial_slice()`] can be used.
    /// To copy into an uninitialized slice, [`Consumer::pop_entire_slice_uninit()`] can be used.
    pub fn pop_entire_slice(&mut self, slice: &mut [T]) -> Result<(), ChunkError> {
        // SAFETY: Transmuting &mut [T] to &mut [MaybeUninit<T>] is generally unsafe!
        // However, since we can guarantee that only valid T values will ever be written,
        // and the reference never leaves our control, it should be fine.
        let _ = unsafe { self.pop_entire_slice_uninit(&mut *(slice as *mut [_] as *mut _))? };
        Ok(())
    }

    /// Copies as many items from the ring buffer as to fill the given uninitialized `slice`.
    ///
    /// The copied slots are automatically made available to be written again by the [`Producer`].
    ///
    /// Returns the given slice, but now initialized.
    ///
    /// # Errors
    ///
    /// If not enough data is available in the ring buffer, no items are copied and
    /// a [`ChunkError`] with the available items is returned.
    ///
    /// To copy only the available slots, [`Consumer::pop_partial_slice_uninit()`] can be used.
    /// To copy into an initialized slice, [`Consumer::pop_entire_slice()`] can be used.
    pub fn pop_entire_slice_uninit<'a>(
        &mut self,
        slice: &'a mut [MaybeUninit<T>],
    ) -> Result<&'a mut [T], ChunkError> {
        let chunk = self.read_chunk(slice.len())?;
        let (one, two) = chunk.as_slices();
        let mid = one.len();
        // NB: If slice.is_empty(), chunk will be empty as well and the following are no-ops:
        one.copy_to_uninit(&mut slice[..mid]);
        two.copy_to_uninit(&mut slice[mid..]);
        chunk.commit_all();
        // NB: This can be replaced by `assume_init_mut()` once stabilized:
        // SAFETY: The entire `slice` has been initialized above.
        Ok(unsafe { &mut *(slice as *mut _ as *mut [_]) })
    }
}

/// Structure for writing into multiple ([`Default`]-initialized) slots in one go.
///
/// This is returned from [`Producer::write_chunk()`].
///
/// To obtain uninitialized slots, use [`Producer::write_chunk_uninit()`] instead,
/// which also allows moving items from an iterator into the ring buffer
/// by means of [`WriteChunkUninit::fill_from_iter()`].
#[derive(Debug, PartialEq, Eq)]
pub struct WriteChunk<'a, T>(Option<WriteChunkUninit<'a, T>>, PhantomData<T>);

impl<T> Drop for WriteChunk<'_, T> {
    fn drop(&mut self) {
        // NB: If `commit()` or `commit_all()` has been called, `self.0` is `None`.
        if let Some(mut chunk) = self.0.take() {
            // No part of the chunk has been committed, all slots are dropped.
            // SAFETY: All slots have been initialized in From::from().
            unsafe { chunk.drop_suffix(0) };
        }
    }
}

impl<'a, T> From<WriteChunkUninit<'a, T>> for WriteChunk<'a, T>
where
    T: Default,
{
    /// Fills all slots with the [`Default`] value.
    fn from(chunk: WriteChunkUninit<'a, T>) -> Self {
        for i in 0..chunk.first_len {
            // SAFETY: i is in a valid range.
            unsafe { chunk.first_ptr.add(i).write(Default::default()) };
        }
        for i in 0..chunk.second_len {
            // SAFETY: i is in a valid range.
            unsafe { chunk.second_ptr.add(i).write(Default::default()) };
        }
        WriteChunk(Some(chunk), PhantomData)
    }
}

impl<T> WriteChunk<'_, T>
where
    T: Default,
{
    /// Returns two slices for writing to the requested slots.
    ///
    /// All slots are initially filled with their [`Default`] value.
    ///
    /// The first slice can only be empty if `0` slots have been requested.
    /// If the first slice contains all requested slots, the second one is empty.
    ///
    /// After writing to the slots, they are *not* automatically made available
    /// to be read by the [`Consumer`].
    /// This has to be explicitly done by calling [`commit()`](WriteChunk::commit)
    /// or [`commit_all()`](WriteChunk::commit_all).
    /// If items are written but *not* committed afterwards,
    /// they will *not* become available for reading and
    /// they will eventually be dropped (if `T` implements [`Drop`]).
    pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
        // self.0 is always Some(chunk).
        let chunk = self.0.as_ref().unwrap();
        // SAFETY: The pointers and lengths have been computed correctly in write_chunk_uninit()
        // and all slots have been initialized in From::from().
        unsafe {
            (
                core::slice::from_raw_parts_mut(chunk.first_ptr, chunk.first_len),
                core::slice::from_raw_parts_mut(chunk.second_ptr, chunk.second_len),
            )
        }
    }

    /// Makes the first `n` slots of the chunk available for reading.
    ///
    /// The rest of the chunk is dropped.
    ///
    /// # Panics
    ///
    /// Panics if `n` is greater than the number of slots in the chunk.
    pub fn commit(mut self, n: usize) {
        // self.0 is always Some(chunk).
        let mut chunk = self.0.take().unwrap();
        // SAFETY: All slots have been initialized in From::from().
        unsafe {
            // Slots at index `n` and higher are dropped ...
            chunk.drop_suffix(n);
            // ... everything below `n` is committed.
            chunk.commit(n);
        }
        // `self` is dropped here, with `self.0` being set to `None`.
    }

    /// Makes the whole chunk available for reading.
    pub fn commit_all(mut self) {
        // self.0 is always Some(chunk).
        let chunk = self.0.take().unwrap();
        // SAFETY: All slots have been initialized in From::from().
        unsafe { chunk.commit_all() };
        // `self` is dropped here, with `self.0` being set to `None`.
    }

    /// Returns the number of slots in the chunk.
    #[must_use]
    pub fn len(&self) -> usize {
        // self.0 is always Some(chunk).
        self.0.as_ref().unwrap().len()
    }

    /// Returns `true` if the chunk contains no slots.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        // self.0 is always Some(chunk).
        self.0.as_ref().unwrap().is_empty()
    }
}

/// Structure for writing into multiple (uninitialized) slots in one go.
///
/// This is returned from [`Producer::write_chunk_uninit()`].
#[derive(Debug, PartialEq, Eq)]
pub struct WriteChunkUninit<'a, T> {
    first_ptr: *mut T,
    first_len: usize,
    second_ptr: *mut T,
    second_len: usize,
    producer: &'a Producer<T>,
}

// SAFETY: WriteChunkUninit only exists while a unique reference to the Producer is held.
// It is therefore safe to move it to another thread.
unsafe impl<T: Send> Send for WriteChunkUninit<'_, T> {}

impl<T> WriteChunkUninit<'_, T> {
    /// Returns two slices for writing to the requested slots.
    ///
    /// The first slice can only be empty if `0` slots have been requested.
    /// If the first slice contains all requested slots, the second one is empty.
    ///
    /// The extension trait [`CopyToUninit`] can be used to safely copy data into those slices.
    ///
    /// After writing to the slots, they are *not* automatically made available
    /// to be read by the [`Consumer`].
    /// This has to be explicitly done by calling [`commit()`](WriteChunkUninit::commit)
    /// or [`commit_all()`](WriteChunkUninit::commit_all).
    /// If items are written but *not* committed afterwards,
    /// they will *not* become available for reading and
    /// they will be leaked (which is only relevant if `T` implements [`Drop`]).
    pub fn as_mut_slices(&mut self) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>]) {
        // SAFETY: The pointers and lengths have been computed correctly in write_chunk_uninit().
        unsafe {
            (
                core::slice::from_raw_parts_mut(self.first_ptr.cast(), self.first_len),
                core::slice::from_raw_parts_mut(self.second_ptr.cast(), self.second_len),
            )
        }
    }

    /// Makes the first `n` slots of the chunk available for reading.
    ///
    /// # Panics
    ///
    /// Panics if `n` is greater than the number of slots in the chunk.
    ///
    /// # Safety
    ///
    /// The caller must make sure that the first `n` elements have been initialized.
    pub unsafe fn commit(self, n: usize) {
        assert!(n <= self.len(), "cannot commit more than chunk size");
        // SAFETY: Delegated to the caller.
        unsafe { self.commit_unchecked(n) };
    }

    /// Makes the whole chunk available for reading.
    ///
    /// # Safety
    ///
    /// The caller must make sure that all elements have been initialized.
    pub unsafe fn commit_all(self) {
        let slots = self.len();
        // SAFETY: Delegated to the caller.
        unsafe { self.commit_unchecked(slots) };
    }

    unsafe fn commit_unchecked(self, n: usize) -> usize {
        let p = self.producer;
        let tail = p.buffer.increment(p.cached_tail.get(), n);
        p.buffer.tail.store(tail, Ordering::Release);
        p.cached_tail.set(tail);
        n
    }

    /// Moves items from an iterator into the (uninitialized) slots of the chunk.
    ///
    /// The number of moved items is returned.
    ///
    /// All moved items are automatically made availabe to be read by the [`Consumer`].
    ///
    /// # Examples
    ///
    /// If the iterator contains too few items, only a part of the chunk
    /// is made available for reading:
    ///
    /// ```
    /// use rtrb::{RingBuffer, PopError};
    ///
    /// let (mut p, mut c) = RingBuffer::new(4);
    ///
    /// if let Ok(chunk) = p.write_chunk_uninit(3) {
    ///     assert_eq!(chunk.fill_from_iter([10, 20]), 2);
    /// } else {
    ///     unreachable!();
    /// }
    /// assert_eq!(p.slots(), 2);
    /// assert_eq!(c.pop(), Ok(10));
    /// assert_eq!(c.pop(), Ok(20));
    /// assert_eq!(c.pop(), Err(PopError::Empty));
    /// ```
    ///
    /// If the chunk size is too small, some items may remain in the iterator.
    /// To be able to keep using the iterator after the call,
    /// `&mut` (or [`Iterator::by_ref()`]) can be used.
    ///
    /// ```
    /// use rtrb::{RingBuffer, PopError};
    ///
    /// let (mut p, mut c) = RingBuffer::new(4);
    ///
    /// let mut it = vec![10, 20, 30].into_iter();
    /// if let Ok(chunk) = p.write_chunk_uninit(2) {
    ///     assert_eq!(chunk.fill_from_iter(&mut it), 2);
    /// } else {
    ///     unreachable!();
    /// }
    /// assert_eq!(c.pop(), Ok(10));
    /// assert_eq!(c.pop(), Ok(20));
    /// assert_eq!(c.pop(), Err(PopError::Empty));
    /// assert_eq!(it.next(), Some(30));
    /// ```
    pub fn fill_from_iter<I>(self, iter: I) -> usize
    where
        I: IntoIterator<Item = T>,
    {
        let mut iter = iter.into_iter();
        let mut iterated = 0;
        'outer: for &(ptr, len) in &[
            (self.first_ptr, self.first_len),
            (self.second_ptr, self.second_len),
        ] {
            for i in 0..len {
                match iter.next() {
                    Some(item) => {
                        // SAFETY: It is allowed to write to this memory slot
                        unsafe { ptr.add(i).write(item) };
                        iterated += 1;
                    }
                    None => break 'outer,
                }
            }
        }
        // SAFETY: iterated slots have been initialized above
        unsafe { self.commit_unchecked(iterated) }
    }

    /// Returns the number of slots in the chunk.
    #[must_use]
    pub fn len(&self) -> usize {
        self.first_len + self.second_len
    }

    /// Returns `true` if the chunk contains no slots.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.first_len == 0
    }

    /// Drops all elements starting from index `n`.
    ///
    /// All of those slots must be initialized.
    unsafe fn drop_suffix(&mut self, n: usize) {
        // NB: If n >= self.len(), the loops are not entered.
        for i in n..self.first_len {
            // SAFETY: The caller must make sure that all slots are initialized.
            unsafe { self.first_ptr.add(i).drop_in_place() };
        }
        for i in n.saturating_sub(self.first_len)..self.second_len {
            // SAFETY: The caller must make sure that all slots are initialized.
            unsafe { self.second_ptr.add(i).drop_in_place() };
        }
    }
}

/// Structure for reading from multiple slots in one go.
///
/// This is returned from [`Consumer::read_chunk()`].
#[derive(Debug, PartialEq, Eq)]
pub struct ReadChunk<'a, T> {
    // Must be "mut" for drop_in_place()
    first_ptr: *mut T,
    first_len: usize,
    // Must be "mut" for drop_in_place()
    second_ptr: *mut T,
    second_len: usize,
    consumer: &'a Consumer<T>,
}

// SAFETY: ReadChunk only exists while a unique reference to the Consumer is held.
// It is therefore safe to move it to another thread.
unsafe impl<T: Send> Send for ReadChunk<'_, T> {}

impl<T> ReadChunk<'_, T> {
    /// Returns two slices for reading from the requested slots.
    ///
    /// The first slice can only be empty if `0` slots have been requested.
    /// If the first slice contains all requested slots, the second one is empty.
    ///
    /// The provided slots are *not* automatically made available
    /// to be written again by the [`Producer`].
    /// This has to be explicitly done by calling [`commit()`](ReadChunk::commit)
    /// or [`commit_all()`](ReadChunk::commit_all).
    /// Note that this runs the destructor of the committed items (if `T` implements [`Drop`]).
    /// You can "peek" at the contained values by simply not calling any of the "commit" methods.
    #[must_use]
    pub fn as_slices(&self) -> (&[T], &[T]) {
        // SAFETY: The pointers and lengths have been computed correctly in read_chunk().
        unsafe {
            (
                core::slice::from_raw_parts(self.first_ptr, self.first_len),
                core::slice::from_raw_parts(self.second_ptr, self.second_len),
            )
        }
    }

    /// Returns two mutable slices for reading from the requested slots.
    ///
    /// This has the same semantics as [`as_slices()`](ReadChunk::as_slices),
    /// except that it returns mutable slices and requires a mutable reference
    /// to the chunk.
    ///
    /// In the vast majority of cases, mutable access is not required when
    /// reading data and the immutable version should be preferred. However,
    /// there are some scenarios where it might be desirable to perform
    /// operations on the data in-place without copying it to a separate buffer
    /// (e.g. streaming decryption), in which case this version can be used.
    #[must_use]
    pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
        // SAFETY: The pointers and lengths have been computed correctly in read_chunk().
        unsafe {
            (
                core::slice::from_raw_parts_mut(self.first_ptr, self.first_len),
                core::slice::from_raw_parts_mut(self.second_ptr, self.second_len),
            )
        }
    }

    /// Drops the first `n` slots of the chunk, making the space available for writing again.
    ///
    /// # Panics
    ///
    /// Panics if `n` is greater than the number of slots in the chunk.
    ///
    /// # Examples
    ///
    /// The following example shows that items are dropped when "committed"
    /// (which is only relevant if `T` implements [`Drop`]).
    ///
    /// ```
    /// use rtrb::RingBuffer;
    ///
    /// // Static variable to count all drop() invocations
    /// static mut DROP_COUNT: i32 = 0;
    /// #[derive(Debug)]
    /// struct Thing;
    /// impl Drop for Thing {
    ///     fn drop(&mut self) { unsafe { DROP_COUNT += 1; } }
    /// }
    ///
    /// // Scope to limit lifetime of ring buffer
    /// {
    ///     let (mut p, mut c) = RingBuffer::new(2);
    ///
    ///     assert!(p.push(Thing).is_ok()); // 1
    ///     assert!(p.push(Thing).is_ok()); // 2
    ///     if let Ok(thing) = c.pop() {
    ///         // "thing" has been *moved* out of the queue but not yet dropped
    ///         assert_eq!(unsafe { DROP_COUNT }, 0);
    ///     } else {
    ///         unreachable!();
    ///     }
    ///     // First Thing has been dropped when "thing" went out of scope:
    ///     assert_eq!(unsafe { DROP_COUNT }, 1);
    ///     assert!(p.push(Thing).is_ok()); // 3
    ///
    ///     if let Ok(chunk) = c.read_chunk(2) {
    ///         assert_eq!(chunk.len(), 2);
    ///         assert_eq!(unsafe { DROP_COUNT }, 1);
    ///         chunk.commit(1); // Drops only one of the two Things
    ///         assert_eq!(unsafe { DROP_COUNT }, 2);
    ///     } else {
    ///         unreachable!();
    ///     }
    ///     // The last Thing is still in the queue ...
    ///     assert_eq!(unsafe { DROP_COUNT }, 2);
    /// }
    /// // ... and it is dropped when the ring buffer goes out of scope:
    /// assert_eq!(unsafe { DROP_COUNT }, 3);
    /// ```
    pub fn commit(self, n: usize) {
        assert!(n <= self.len(), "cannot commit more than chunk size");
        // SAFETY: self.len() initialized elements have been obtained in read_chunk().
        unsafe { self.commit_unchecked(n) };
    }

    /// Drops all slots of the chunk, making the space available for writing again.
    pub fn commit_all(self) {
        let slots = self.len();
        // SAFETY: self.len() initialized elements have been obtained in read_chunk().
        unsafe { self.commit_unchecked(slots) };
    }

    unsafe fn commit_unchecked(self, n: usize) -> usize {
        let first_len = self.first_len.min(n);
        for i in 0..first_len {
            // SAFETY: The caller must make sure that there are n initialized elements.
            unsafe { self.first_ptr.add(i).drop_in_place() };
        }
        let second_len = self.second_len.min(n - first_len);
        for i in 0..second_len {
            // SAFETY: The caller must make sure that there are n initialized elements.
            unsafe { self.second_ptr.add(i).drop_in_place() };
        }
        let c = self.consumer;
        let head = c.buffer.increment(c.cached_head.get(), n);
        c.buffer.head.store(head, Ordering::Release);
        c.cached_head.set(head);
        n
    }

    /// Returns the number of slots in the chunk.
    #[must_use]
    pub fn len(&self) -> usize {
        self.first_len + self.second_len
    }

    /// Returns `true` if the chunk contains no slots.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.first_len == 0
    }
}

impl<'a, T> IntoIterator for ReadChunk<'a, T> {
    type Item = T;
    type IntoIter = ReadChunkIntoIter<'a, T>;

    /// Turns a [`ReadChunk`] into an iterator.
    ///
    /// When the iterator is dropped, all iterated slots are made available for writing again.
    /// Non-iterated items remain in the ring buffer.
    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            chunk: self,
            iterated: 0,
        }
    }
}

/// An iterator that moves out of a [`ReadChunk`].
///
/// This `struct` is created by the [`into_iter()`](ReadChunk::into_iter) method
/// on [`ReadChunk`] (provided by the [`IntoIterator`] trait).
///
/// When this `struct` is dropped, the iterated slots are made available for writing again.
/// Non-iterated items remain in the ring buffer.
#[derive(Debug)]
pub struct ReadChunkIntoIter<'a, T> {
    chunk: ReadChunk<'a, T>,
    iterated: usize,
}

impl<T> Drop for ReadChunkIntoIter<'_, T> {
    /// Makes all iterated slots available for writing again.
    ///
    /// Non-iterated items remain in the ring buffer and are *not* dropped.
    fn drop(&mut self) {
        let c = &self.chunk.consumer;
        let head = c.buffer.increment(c.cached_head.get(), self.iterated);
        c.buffer.head.store(head, Ordering::Release);
        c.cached_head.set(head);
    }
}

impl<T> Iterator for ReadChunkIntoIter<'_, T> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let ptr = if self.iterated < self.chunk.first_len {
            // SAFETY: first_len is valid.
            unsafe { self.chunk.first_ptr.add(self.iterated) }
        } else if self.iterated < self.chunk.first_len + self.chunk.second_len {
            // SAFETY: first_len and second_len are valid.
            unsafe {
                self.chunk
                    .second_ptr
                    .add(self.iterated - self.chunk.first_len)
            }
        } else {
            return None;
        };
        self.iterated += 1;
        // SAFETY: ptr points to an initialized slot.
        Some(unsafe { ptr.read() })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.chunk.first_len + self.chunk.second_len - self.iterated;
        (remaining, Some(remaining))
    }
}

impl<T> ExactSizeIterator for ReadChunkIntoIter<'_, T> {}

impl<T> core::iter::FusedIterator for ReadChunkIntoIter<'_, T> {}

#[cfg(feature = "std")]
impl std::io::Write for Producer<u8> {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }
        match self.push_partial_slice(buf) {
            ([], _) => Err(std::io::ErrorKind::WouldBlock.into()),
            (pushed, _) => Ok(pushed.len()),
        }
    }

    #[inline]
    fn flush(&mut self) -> std::io::Result<()> {
        // Nothing to do here.
        Ok(())
    }
}

#[cfg(feature = "std")]
impl std::io::Read for Consumer<u8> {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }
        match self.pop_partial_slice(buf) {
            ([], _) => Err(std::io::ErrorKind::WouldBlock.into()),
            (popped, _) => Ok(popped.len()),
        }
    }
}

/// Error type for [`Consumer::read_chunk()`], [`Consumer::pop_entire_slice()`],
/// [`Consumer::pop_entire_slice_uninit()`], [`Producer::write_chunk()`],
/// [`Producer::write_chunk_uninit()`] and [`Producer::push_entire_slice()`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ChunkError {
    /// Fewer than the requested number of slots were available.
    ///
    /// Contains the number of slots that were available.
    TooFewSlots(usize),
}

#[cfg(feature = "std")]
impl std::error::Error for ChunkError {}

impl fmt::Display for ChunkError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ChunkError::TooFewSlots(n) => {
                alloc::format!("only {} slots available in ring buffer", n).fmt(f)
            }
        }
    }
}