@@ -149,8 +149,8 @@ the buffer did not need to be initialized, the simpler implementation would have
149
149
The ` ReadBuf ` type manages a * progressively initialized* buffer of bytes. It is primarily used to avoid buffer
150
150
initialization overhead when working with types implementing the ` Read ` trait. It wraps a buffer of
151
151
possibly-uninitialized bytes and tracks how much of the buffer has been initialized and how much of the buffer has been
152
- written to . Tracking the set of initialized bytes allows initialization costs to only be paid once, even if the buffer
153
- is used repeatedly in a loop.
152
+ filled . Tracking the set of initialized bytes allows initialization costs to only be paid once, even if the buffer is
153
+ used repeatedly in a loop.
154
154
155
155
Here's a small example of working with a reader using a ` ReadBuf ` :
156
156
@@ -166,14 +166,14 @@ loop {
166
166
some_reader . read_buf (& mut buf )? ;
167
167
168
168
// If nothing was written into the buffer, we're at EOF.
169
- if buf . written (). is_empty () {
169
+ if buf . filled (). is_empty () {
170
170
break ;
171
171
}
172
172
173
173
// Otherwise, process the data.
174
- process_data (buf . written ());
174
+ process_data (buf . filled ());
175
175
176
- // And then clear the buffer out so we can read into it again. This just resets the amount of data "written" to 0,
176
+ // And then clear the buffer out so we can read into it again. This just resets the amount of filled data to 0,
177
177
// but preserves the memory of how much of the buffer has been initialized.
178
178
buf . clear ();
179
179
}
@@ -192,16 +192,16 @@ impl Read for MyReader {
192
192
fn read_buf (& mut self , buf : & mut ReadBuf <'_ >) -> io :: Result <()> {
193
193
// Get access to the unwritten part of the buffer, making sure it has been fully initialized. Since `ReadBuf`
194
194
// tracks the initialization state of the buffer, this is "free" after the first time it's called.
195
- let unwritten : & mut [u8 ] = buf . initialize_unwritten ();
195
+ let unfilled : & mut [u8 ] = buf . initialize_unfilled ();
196
196
197
197
// Fill the whole buffer with some nonsense.
198
- for (i , byte ) in unwritten . iter_mut (). enumerate () {
198
+ for (i , byte ) in unfilled . iter_mut (). enumerate () {
199
199
* byte = i as u8 ;
200
200
}
201
201
202
202
// And indicate that we've written the whole thing.
203
- let len = unwritten . len ();
204
- buf . add_written (len );
203
+ let len = unfilled . len ();
204
+ buf . add_filled (len );
205
205
206
206
Ok (())
207
207
}
@@ -214,10 +214,10 @@ An unsafe `Read` implementation:
214
214
impl Read for TcpStream {
215
215
fn read_buf (& mut self , buf : & mut ReadBuf <'_ >) -> io :: Result <()> {
216
216
unsafe {
217
- // Get access to the unwritten part of the buffer, without initializing it. This method is unsafe; we are
217
+ // Get access to the filled part of the buffer, without initializing it. This method is unsafe; we are
218
218
// responsible for ensuing that we don't "de-initialize" portions of it that have previously been
219
219
// initialized.
220
- let unwritten : & mut [MaybeUninit <u8 >] = buf . unwritten_mut ();
220
+ let unfilled : & mut [MaybeUninit <u8 >] = buf . unfilled_mut ();
221
221
222
222
// We're just delegating to the libc read function, which returns an `isize`. The return value indicates
223
223
// an error if negative and the number of bytes read otherwise.
@@ -230,11 +230,11 @@ impl Read for TcpStream {
230
230
let nread = nread as usize ;
231
231
// If the read succeeded, tell the buffer that the read-to portion has been initialized. This method is
232
232
// unsafe; we are responsible for ensuring that this portion of the buffer has actually been initialized.
233
- buf . assume_initialized (nread );
233
+ buf . assume_init (nread );
234
234
// And indicate that we've written the bytes as well. Unlike `assume_initialized`, this method is safe,
235
235
// and asserts that the written portion of the buffer does not advance beyond the initialized portion of
236
236
// the buffer. If we didn't call `assume_initialized` above, this call could panic.
237
- buf . add_written (nread );
237
+ buf . add_filled (nread );
238
238
239
239
Ok (())
240
240
}
@@ -246,15 +246,15 @@ impl Read for TcpStream {
246
246
[ reference-level-explanation ] : #reference-level-explanation
247
247
248
248
``` rust
249
- /// A wrapper around a byte buffer that is incrementally written to and initialized.
249
+ /// A wrapper around a byte buffer that is incrementally filled and initialized.
250
250
///
251
251
/// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the
252
- /// buffer that has been logically written to , a region that has been initialized at some point but not yet logically
253
- /// written to , and a region at the end that is fully uninitialized. The written-to region is guaranteed to be a
252
+ /// buffer that has been logically filled with data , a region that has been initialized at some point but not yet
253
+ /// logically filled , and a region at the end that is fully uninitialized. The filled region is guaranteed to be a
254
254
/// subset of the initialized region.
255
255
pub struct ReadBuf <'a > {
256
256
buf : & 'a mut [MaybeUninit <u8 >],
257
- written : usize ,
257
+ filled : usize ,
258
258
initialized : usize ,
259
259
}
260
260
@@ -273,94 +273,94 @@ impl<'a> ReadBuf<'a> {
273
273
#[inline]
274
274
pub fn len (& self ) -> usize { ... }
275
275
276
- /// Returns a shared reference to the written-to portion of the buffer.
276
+ /// Returns a shared reference to the filled portion of the buffer.
277
277
#[inline]
278
- pub fn written (& self ) -> & [u8 ] { ... }
278
+ pub fn filled (& self ) -> & [u8 ] { ... }
279
279
280
- /// Returns a mutable reference to the written-to portion of the buffer.
280
+ /// Returns a mutable reference to the filled portion of the buffer.
281
281
#[inline]
282
- pub fn written_mut (& mut self ) -> & mut [u8 ] { ... }
282
+ pub fn filled_mut (& mut self ) -> & mut [u8 ] { ... }
283
283
284
284
/// Returns a shared reference to the initialized portion of the buffer.
285
285
///
286
- /// This includes the written-to portion.
286
+ /// This includes the filled portion.
287
287
#[inline]
288
288
pub fn initialized (& self ) -> & [u8 ] { ... }
289
289
290
290
/// Returns a mutable reference to the initialized portion of the buffer.
291
291
///
292
- /// This includes the written-to portion.
292
+ /// This includes the filled portion.
293
293
#[inline]
294
294
pub fn initialized_mut (& mut self ) -> & mut [u8 ] { ... }
295
295
296
- /// Returns a mutable reference to the unwritten-to part of the buffer without ensuring that it has been fully
296
+ /// Returns a mutable reference to the unfilled part of the buffer without ensuring that it has been fully
297
297
/// initialized.
298
298
///
299
299
/// # Safety
300
300
///
301
301
/// The caller must not de-initialize portions of the buffer that have already been initialized.
302
302
#[inline]
303
- pub unsafe fn unwritten_mut (& mut self ) -> & mut [MaybeUninit <u8 >] { ... }
303
+ pub unsafe fn unfilled_mut (& mut self ) -> & mut [MaybeUninit <u8 >] { ... }
304
304
305
- /// Returns a mutable reference to the unwritten-to part of the buffer, ensuring it is fully initialized.
305
+ /// Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized.
306
306
///
307
307
/// Since `ReadBuf` tracks the region of the buffer that has been initialized, this is effectively "free" after
308
308
/// the first use.
309
309
#[inline]
310
- pub fn initialize_unwritten (& mut self ) -> & mut [u8 ] { ... }
310
+ pub fn initialize_unfilled (& mut self ) -> & mut [u8 ] { ... }
311
311
312
- /// Returns a mutable reference to the first `n` bytes of the unwritten-to part of the buffer, ensuring it is
312
+ /// Returns a mutable reference to the first `n` bytes of the unfilled part of the buffer, ensuring it is
313
313
/// fully initialized.
314
314
///
315
315
/// # Panics
316
316
///
317
317
/// Panics if `self.remaining()` is less than `n`.
318
318
#[inline]
319
- pub fn initialize_unwritten_to (& mut self , n : usize ) -> & mut [u8 ] { ... }
319
+ pub fn initialize_unfilled_to (& mut self , n : usize ) -> & mut [u8 ] { ... }
320
320
321
- /// Returns the number of bytes at the end of the slice that have not yet been written to .
321
+ /// Returns the number of bytes at the end of the slice that have not yet been filled .
322
322
#[inline]
323
323
pub fn remaining (& self ) -> usize { ... }
324
324
325
- /// Clears the buffer, resetting the written-to region to empty.
325
+ /// Clears the buffer, resetting the filled region to empty.
326
326
///
327
327
/// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
328
328
#[inline]
329
329
pub fn clear (& mut self ) { ... }
330
330
331
- /// Increases the size of the written-to region of the buffer.
331
+ /// Increases the size of the filled region of the buffer.
332
332
///
333
333
/// The number of initialized bytes is not changed.
334
334
///
335
335
/// # Panics
336
336
///
337
- /// Panics if the written region of the buffer would become larger than the initialized region.
337
+ /// Panics if the filled region of the buffer would become larger than the initialized region.
338
338
#[inline]
339
- pub fn add_written (& mut self , n : usize ) { ... }
339
+ pub fn add_filled (& mut self , n : usize ) { ... }
340
340
341
- /// Sets the size of the written-to region of the buffer.
341
+ /// Sets the size of the filled region of the buffer.
342
342
///
343
343
/// The number of initialized bytes is not changed.
344
344
///
345
- /// Note that this can be used to *shrink* the written region of the buffer in addition to growing it (for
345
+ /// Note that this can be used to *shrink* the filled region of the buffer in addition to growing it (for
346
346
/// example, by a `Read` implementation that compresses data in-place).
347
347
///
348
348
/// # Panics
349
349
///
350
- /// Panics if the written region of the buffer would become larger than the intialized region.
350
+ /// Panics if the filled region of the buffer would become larger than the intialized region.
351
351
#[inline]
352
352
pub fn set_written (& mut self , n : usize ) { ... }
353
353
354
- /// Asserts that the first `n` unwritten bytes of the buffer are initialized.
354
+ /// Asserts that the first `n` unfilled bytes of the buffer are initialized.
355
355
///
356
356
/// `ReadBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
357
357
/// bytes than are already known to be initialized.
358
358
///
359
359
/// # Safety
360
360
///
361
- /// The caller must ensure that `n` unwritten bytes of the buffer have already been initialized.
361
+ /// The caller must ensure that `n` unfilled bytes of the buffer have already been initialized.
362
362
#[inline]
363
- pub unsafe fn assume_initialized (& mut self , n : usize ) { ... }
363
+ pub unsafe fn assume_init (& mut self , n : usize ) { ... }
364
364
365
365
/// Appends data to the buffer, advancing the written position and possibly also the initialized position.
366
366
///
@@ -390,8 +390,8 @@ pub trait Read {
390
390
///
391
391
/// The default implementation delegates to `read`.
392
392
fn read_buf (& mut self , buf : & mut ReadBuf <'_ >) -> io :: Result <()> {
393
- let n = self . read (buf . initialize_unwritten ())? ;
394
- buf . add_written (n );
393
+ let n = self . read (buf . initialize_unfilled ())? ;
394
+ buf . add_filled (n );
395
395
Ok (())
396
396
}
397
397
@@ -429,14 +429,14 @@ pub trait Read {
429
429
/// allow use with uninitialized buffers.
430
430
fn read_buf_exact (& mut self , buf : & mut ReadBuf <'_ >) -> io :: Result <()> {
431
431
while buf . remaining () > 0 {
432
- let prev_written = buf . written (). len ();
432
+ let prev_filled = buf . filled (). len ();
433
433
match self . read_buf (& mut buf ) {
434
434
Ok (()) => {}
435
435
Err (e ) if e . kind () == io :: ErrorKind :: Interrupted => continue ,
436
436
Err (e ) => return Err (e ),
437
437
}
438
438
439
- if buf . written . len () == prev_written {
439
+ if buf . filled () . len () == prev_filled {
440
440
return Err (io :: Error :: new (io :: ErrorKind :: UnexpectedEof , " failed to fill buffer" ));
441
441
}
442
442
}
@@ -455,7 +455,7 @@ pub trait Read {
455
455
456
456
let mut read_buf = ReadBuf :: uninit (buf . spare_capacity_mut ());
457
457
unsafe {
458
- read_buf . assume_initialized (initialized );
458
+ read_buf . assume_init (initialized );
459
459
}
460
460
461
461
match self . read_buf (& mut read_buf ) {
@@ -464,12 +464,12 @@ pub trait Read {
464
464
Err (e ) => return Err (e ),
465
465
}
466
466
467
- if read_buf . written (). is_empty () {
467
+ if read_buf . filled (). is_empty () {
468
468
break ;
469
469
}
470
470
471
- initialized = read_buf . initialized (). len () - read_buf . written (). len ();
472
- let new_len = buf . len () + read_buf . written (). len ();
471
+ initialized = read_buf . initialized (). len () - read_buf . filled (). len ();
472
+ let new_len = buf . len () + read_buf . filled (). len ();
473
473
unsafe {
474
474
buf . set_len (new_len );
475
475
}
@@ -495,12 +495,12 @@ where
495
495
Err (e ) => return Err (e ),
496
496
};
497
497
498
- if buf . written (). is_empty () {
498
+ if buf . filled (). is_empty () {
499
499
break ;
500
500
}
501
501
502
- len += buf . written (). len () as u64 ;
503
- writer . write_all (buf . written ())? ;
502
+ len += buf . filled (). len () as u64 ;
503
+ writer . write_all (buf . filled ())? ;
504
504
buf . clear ();
505
505
}
506
506
0 commit comments