@@ -7,8 +7,7 @@ use crate::cmp;
7
7
use crate :: fmt:: { self , Debug , Formatter } ;
8
8
use crate :: mem:: MaybeUninit ;
9
9
10
- // TODO docs
11
- /// A wrapper around a byte buffer that is incrementally filled and initialized.
10
+ /// A borrowed byte buffer which is incrementally filled and initialized.
12
11
///
13
12
/// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the
14
13
/// buffer that has been logically filled with data, a region that has been initialized at some point but not yet
@@ -21,9 +20,20 @@ use crate::mem::MaybeUninit;
21
20
/// [ filled | unfilled ]
22
21
/// [ initialized | uninitialized ]
23
22
/// ```
23
+ ///
24
+ /// A `BorrowBuf` is created around some existing data (or capacity for data) via a unique reference
25
+ /// (`&mut`). The `BorrowBuf` can be configured (e.g., using `clear` or `set_init`), but otherwise
26
+ /// is read-only. To write into the buffer, use `unfilled` to create a `BorrowCursor`. The cursor
27
+ /// has write-only access to the unfilled portion of the buffer (you can think of it like a
28
+ /// write-only iterator).
29
+ ///
30
+ /// The lifetime `'a` is a bound on the lifetime of the underlying data.
24
31
pub struct BorrowBuf < ' a > {
32
+ /// The buffer's underlying data.
25
33
buf : & ' a mut [ MaybeUninit < u8 > ] ,
34
+ /// The length of `self.buf` which is known to be filled.
26
35
filled : usize ,
36
+ /// The length of `self.buf` which is known to be initialized.
27
37
initialized : usize ,
28
38
}
29
39
@@ -37,7 +47,7 @@ impl Debug for BorrowBuf<'_> {
37
47
}
38
48
}
39
49
40
- /// Creates a new `BorrowBuf` from a fully initialized slice.
50
+ /// Create a new `BorrowBuf` from a fully initialized slice.
41
51
impl < ' a > From < & ' a mut [ u8 ] > for BorrowBuf < ' a > {
42
52
#[ inline]
43
53
fn from ( slice : & ' a mut [ u8 ] ) -> BorrowBuf < ' a > {
@@ -52,7 +62,7 @@ impl<'a> From<&'a mut [u8]> for BorrowBuf<'a> {
52
62
}
53
63
}
54
64
55
- /// Creates a new `BorrowBuf` from a fully uninitialized buffer.
65
+ /// Create a new `BorrowBuf` from an uninitialized buffer.
56
66
///
57
67
/// Use `set_init` if part of the buffer is known to be already initialized.
58
68
impl < ' a > From < & ' a mut [ MaybeUninit < u8 > ] > for BorrowBuf < ' a > {
@@ -90,7 +100,7 @@ impl<'a> BorrowBuf<'a> {
90
100
91
101
/// Returns a cursor over the unfilled part of the buffer.
92
102
#[ inline]
93
- pub fn unfilled < ' b > ( & ' b mut self ) -> BorrowCursor < ' a , ' b > {
103
+ pub fn unfilled < ' this > ( & ' this mut self ) -> BorrowCursor < ' this , ' a > {
94
104
BorrowCursor { start : self . filled , buf : self }
95
105
}
96
106
@@ -118,20 +128,36 @@ impl<'a> BorrowBuf<'a> {
118
128
}
119
129
}
120
130
121
- /// A cursor view of a [`BorrowBuf`](BorrowBuf).
131
+ /// A writeable view of the unfilled portion of a [`BorrowBuf`](BorrowBuf).
132
+ ///
133
+ /// Provides access to the initialized and uninitialized parts of the underlying `BorrowBuf`.
134
+ /// Data can be written directly to the cursor by using [`append`](BorrowCursor::append) or
135
+ /// indirectly by getting a slice of part or all of the cursor and writing into the slice. In the
136
+ /// indirect case, the caller must call [`advance`](BorrowCursor::advance) after writing to inform
137
+ /// the cursor how many bytes have been written.
122
138
///
123
- /// Provides mutable access to the unfilled portion (both initialised and uninitialised data) from
124
- /// the buffer.
139
+ /// Once data is written to the cursor, it becomes part of the filled portion of the underlying
140
+ /// `BorrowBuf` and can no longer be accessed or re-written by the cursor. I.e., the cursor tracks
141
+ /// the unfilled part of the underlying `BorrowBuf`.
142
+ ///
143
+ /// The `'buf` lifetime is a bound on the lifetime of the underlying buffer. `'data` is a bound on
144
+ /// that buffer's underlying data.
125
145
#[ derive( Debug ) ]
126
- pub struct BorrowCursor < ' a , ' b > {
127
- buf : & ' b mut BorrowBuf < ' a > ,
146
+ pub struct BorrowCursor < ' buf , ' data > {
147
+ /// The underlying buffer.
148
+ buf : & ' buf mut BorrowBuf < ' data > ,
149
+ /// The length of the filled portion of the underlying buffer at the time of the cursor's
150
+ /// creation.
128
151
start : usize ,
129
152
}
130
153
131
- impl < ' a , ' b > BorrowCursor < ' a , ' b > {
154
+ impl < ' buf , ' data > BorrowCursor < ' buf , ' data > {
132
155
/// Clone this cursor.
156
+ ///
157
+ /// Since a cursor maintains unique access to its underlying buffer, the cloned cursor is not
158
+ /// accessible while the clone is alive.
133
159
#[ inline]
134
- pub fn clone < ' c > ( & ' c mut self ) -> BorrowCursor < ' a , ' c > {
160
+ pub fn clone < ' this > ( & ' this mut self ) -> BorrowCursor < ' this , ' data > {
135
161
BorrowCursor { buf : self . buf , start : self . start }
136
162
}
137
163
@@ -141,14 +167,16 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
141
167
self . buf . capacity ( ) - self . buf . filled
142
168
}
143
169
144
- /// Returns the number of bytes written to this cursor.
145
- // TODO check for reuse uses
170
+ /// Returns the number of bytes written to this cursor since it was created from a `BorrowBuf`.
171
+ ///
172
+ /// Note that if this cursor is a clone of another, then the count returned is the count written
173
+ /// via either cursor, not the count since the cursor was cloned.
146
174
#[ inline]
147
175
pub fn written ( & self ) -> usize {
148
176
self . buf . filled - self . start
149
177
}
150
178
151
- /// Returns a shared reference to the initialized portion of the buffer .
179
+ /// Returns a shared reference to the initialized portion of the cursor .
152
180
#[ inline]
153
181
pub fn init_ref ( & self ) -> & [ u8 ] {
154
182
//SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -157,7 +185,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
157
185
}
158
186
}
159
187
160
- /// Returns a mutable reference to the initialized portion of the buffer .
188
+ /// Returns a mutable reference to the initialized portion of the cursor .
161
189
#[ inline]
162
190
pub fn init_mut ( & mut self ) -> & mut [ u8 ] {
163
191
//SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -168,25 +196,33 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
168
196
}
169
197
}
170
198
171
- /// Returns a mutable reference to the uninitialized part of the buffer .
199
+ /// Returns a mutable reference to the uninitialized part of the cursor .
172
200
///
173
201
/// It is safe to uninitialize any of these bytes.
174
202
#[ inline]
175
203
pub fn uninit_mut ( & mut self ) -> & mut [ MaybeUninit < u8 > ] {
176
204
& mut self . buf . buf [ self . buf . initialized ..]
177
205
}
178
206
179
- /// A view of the cursor as a mutable slice of `MaybeUninit<u8>`.
207
+ /// Returns a mutable reference to the whole cursor.
208
+ ///
209
+ /// # Safety
210
+ ///
211
+ /// The caller must not uninitialize any bytes in the initialized portion of the cursor.
180
212
#[ inline]
181
213
pub unsafe fn as_mut ( & mut self ) -> & mut [ MaybeUninit < u8 > ] {
182
214
& mut self . buf . buf [ self . buf . filled ..]
183
215
}
184
216
185
- /// Increases the size of the filled region of the buffer.
217
+ /// Advance the cursor by asserting that `n` bytes have been filled.
218
+ ///
219
+ /// After advancing, the `n` bytes are no longer accessible via the cursor and can only be
220
+ /// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements
221
+ /// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements.
186
222
///
187
223
/// # Safety
188
224
///
189
- /// The caller must ensure that the first `n` elements of the cursor have been properly
225
+ /// The caller must ensure that the first `n` bytes of the cursor have been properly
190
226
/// initialised.
191
227
#[ inline]
192
228
pub unsafe fn advance ( & mut self , n : usize ) -> & mut Self {
@@ -195,7 +231,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
195
231
self
196
232
}
197
233
198
- /// Initialised all bytes in the cursor.
234
+ /// Initializes all bytes in the cursor.
199
235
#[ inline]
200
236
pub fn ensure_init ( & mut self ) -> & mut Self {
201
237
for byte in self . uninit_mut ( ) {
@@ -208,8 +244,8 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
208
244
209
245
/// Asserts that the first `n` unfilled bytes of the cursor are initialized.
210
246
///
211
- /// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
212
- /// bytes than are already known to be initialized.
247
+ /// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when
248
+ /// called with fewer bytes than are already known to be initialized.
213
249
///
214
250
/// # Safety
215
251
///
@@ -220,7 +256,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
220
256
self
221
257
}
222
258
223
- /// Appends data to the cursor, advancing the position within its buffer.
259
+ /// Appends data to the cursor, advancing position within its buffer.
224
260
///
225
261
/// # Panics
226
262
///
0 commit comments