@@ -7,8 +7,7 @@ use crate::cmp;
77use crate :: fmt:: { self , Debug , Formatter } ;
88use crate :: mem:: MaybeUninit ;
99
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.
1211///
1312/// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the
1413/// 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;
2120/// [ filled | unfilled ]
2221/// [ initialized | uninitialized ]
2322/// ```
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.
2431pub struct BorrowBuf < ' a > {
32+ /// The buffer's underlying data.
2533 buf : & ' a mut [ MaybeUninit < u8 > ] ,
34+ /// The length of `self.buf` which is known to be filled.
2635 filled : usize ,
36+ /// The length of `self.buf` which is known to be initialized.
2737 initialized : usize ,
2838}
2939
@@ -37,7 +47,7 @@ impl Debug for BorrowBuf<'_> {
3747 }
3848}
3949
40- /// Creates a new `BorrowBuf` from a fully initialized slice.
50+ /// Create a new `BorrowBuf` from a fully initialized slice.
4151impl < ' a > From < & ' a mut [ u8 ] > for BorrowBuf < ' a > {
4252 #[ inline]
4353 fn from ( slice : & ' a mut [ u8 ] ) -> BorrowBuf < ' a > {
@@ -52,7 +62,7 @@ impl<'a> From<&'a mut [u8]> for BorrowBuf<'a> {
5262 }
5363}
5464
55- /// Creates a new `BorrowBuf` from a fully uninitialized buffer.
65+ /// Create a new `BorrowBuf` from an uninitialized buffer.
5666///
5767/// Use `set_init` if part of the buffer is known to be already initialized.
5868impl < ' a > From < & ' a mut [ MaybeUninit < u8 > ] > for BorrowBuf < ' a > {
@@ -90,7 +100,7 @@ impl<'a> BorrowBuf<'a> {
90100
91101 /// Returns a cursor over the unfilled part of the buffer.
92102 #[ inline]
93- pub fn unfilled < ' b > ( & ' b mut self ) -> BorrowCursor < ' a , ' b > {
103+ pub fn unfilled < ' this > ( & ' this mut self ) -> BorrowCursor < ' this , ' a > {
94104 BorrowCursor { start : self . filled , buf : self }
95105 }
96106
@@ -118,20 +128,36 @@ impl<'a> BorrowBuf<'a> {
118128 }
119129}
120130
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.
122138///
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.
125145#[ 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.
128151 start : usize ,
129152}
130153
131- impl < ' a , ' b > BorrowCursor < ' a , ' b > {
154+ impl < ' buf , ' data > BorrowCursor < ' buf , ' data > {
132155 /// 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.
133159 #[ inline]
134- pub fn clone < ' c > ( & ' c mut self ) -> BorrowCursor < ' a , ' c > {
160+ pub fn clone < ' this > ( & ' this mut self ) -> BorrowCursor < ' this , ' data > {
135161 BorrowCursor { buf : self . buf , start : self . start }
136162 }
137163
@@ -141,14 +167,16 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
141167 self . buf . capacity ( ) - self . buf . filled
142168 }
143169
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.
146174 #[ inline]
147175 pub fn written ( & self ) -> usize {
148176 self . buf . filled - self . start
149177 }
150178
151- /// Returns a shared reference to the initialized portion of the buffer .
179+ /// Returns a shared reference to the initialized portion of the cursor .
152180 #[ inline]
153181 pub fn init_ref ( & self ) -> & [ u8 ] {
154182 //SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -157,7 +185,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
157185 }
158186 }
159187
160- /// Returns a mutable reference to the initialized portion of the buffer .
188+ /// Returns a mutable reference to the initialized portion of the cursor .
161189 #[ inline]
162190 pub fn init_mut ( & mut self ) -> & mut [ u8 ] {
163191 //SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -168,25 +196,33 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
168196 }
169197 }
170198
171- /// Returns a mutable reference to the uninitialized part of the buffer .
199+ /// Returns a mutable reference to the uninitialized part of the cursor .
172200 ///
173201 /// It is safe to uninitialize any of these bytes.
174202 #[ inline]
175203 pub fn uninit_mut ( & mut self ) -> & mut [ MaybeUninit < u8 > ] {
176204 & mut self . buf . buf [ self . buf . initialized ..]
177205 }
178206
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.
180212 #[ inline]
181213 pub unsafe fn as_mut ( & mut self ) -> & mut [ MaybeUninit < u8 > ] {
182214 & mut self . buf . buf [ self . buf . filled ..]
183215 }
184216
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.
186222 ///
187223 /// # Safety
188224 ///
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
190226 /// initialised.
191227 #[ inline]
192228 pub unsafe fn advance ( & mut self , n : usize ) -> & mut Self {
@@ -195,7 +231,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
195231 self
196232 }
197233
198- /// Initialised all bytes in the cursor.
234+ /// Initializes all bytes in the cursor.
199235 #[ inline]
200236 pub fn ensure_init ( & mut self ) -> & mut Self {
201237 for byte in self . uninit_mut ( ) {
@@ -208,8 +244,8 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
208244
209245 /// Asserts that the first `n` unfilled bytes of the cursor are initialized.
210246 ///
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.
213249 ///
214250 /// # Safety
215251 ///
@@ -220,7 +256,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
220256 self
221257 }
222258
223- /// Appends data to the cursor, advancing the position within its buffer.
259+ /// Appends data to the cursor, advancing position within its buffer.
224260 ///
225261 /// # Panics
226262 ///
0 commit comments