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
//! Ringbuffer data storage.
//!
//! This module provides [`Storage`] and its implementors, which are used by the
//! crate's ring buffer types for the backing data buffer. Data could be stored
//! in different locations: [`ArrayStorage`] stores it on the stack, for `const`
//! capacities, while [`HeapStorage`] allocates it on the heap.
use UnsafeCell;
use MaybeUninit;
use Pin;
pub use ArrayStorage;
pub use HeapStorage;
/// Storage for a ring buffer.
///
/// The storage should represent a slice of items, some of which may not be
/// initialized. It is the responsibility of the ring buffer using the storage
/// to track which items are initialized and also to drop them appropriately.
///
/// This is an `unsafe` trait: every implementation of it must satisfy certain
/// invariants, else risk undefined behaviour:
///
/// - [`Pin`] safety: [`get()`], [`get_mut()`], and [`get_pin()`] return the
/// same slice (i.e. the same pointer address) for the same `self` address.
/// - [`get()`], [`get_mut()`], and [`get_pin()`] always return a slice with the
/// same length; the length cannot change for the duration of the object.
/// - The length of the stored slice is non-zero.
/// - The referenced slice's contents must not change externally; they are only
/// mutated by the containing ring buffer. Interior mutation is not allowed.
/// - The ring buffer is free to assume that modifications it makes to the
/// slice will persist until they are overriden by newer modifications.
///
/// [`get()`]: Self::get()
/// [`get_mut()`]: Self::get_mut()
/// [`get_pin()`]: Self::get_pin()
pub unsafe
/// A slot for storing a single ring buffer item.
///
/// According to the [`Storage`] API, individual slots in the data buffer can be
/// initialized or uninitialized, and some may be borrowed mutably. This is a
/// convienience wrapper which provides both of these properties, combining both
/// [`MaybeUninit`] and [`UnsafeCell`] with a useful API.
///
/// Slots do not require initialization, like [`MaybeUninit`]; they are valid
/// even if constructed uninitialized. This allows them to be created in arrays
/// and the like without any work.
unsafe