Skip to content

Commit ba39794

Browse files
committed
rename "filled" to "written"
1 parent 5e37261 commit ba39794

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

text/0000-read-buf.md

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ the buffer did not need to be initialized, the simpler implementation would have
149149
The `ReadBuf` type manages a *progressively initialized* buffer of bytes. It is primarily used to avoid buffer
150150
initialization overhead when working with types implementing the `Read` trait. It wraps a buffer of
151151
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.
154154

155155
Here's a small example of working with a reader using a `ReadBuf`:
156156

@@ -166,14 +166,14 @@ loop {
166166
some_reader.read_buf(&mut buf)?;
167167

168168
// If nothing was written into the buffer, we're at EOF.
169-
if buf.written().is_empty() {
169+
if buf.filled().is_empty() {
170170
break;
171171
}
172172

173173
// Otherwise, process the data.
174-
process_data(buf.written());
174+
process_data(buf.filled());
175175

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,
177177
// but preserves the memory of how much of the buffer has been initialized.
178178
buf.clear();
179179
}
@@ -192,16 +192,16 @@ impl Read for MyReader {
192192
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
193193
// Get access to the unwritten part of the buffer, making sure it has been fully initialized. Since `ReadBuf`
194194
// 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();
196196

197197
// 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() {
199199
*byte = i as u8;
200200
}
201201

202202
// 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);
205205

206206
Ok(())
207207
}
@@ -214,10 +214,10 @@ An unsafe `Read` implementation:
214214
impl Read for TcpStream {
215215
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
216216
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
218218
// responsible for ensuing that we don't "de-initialize" portions of it that have previously been
219219
// initialized.
220-
let unwritten: &mut [MaybeUninit<u8>] = buf.unwritten_mut();
220+
let unfilled: &mut [MaybeUninit<u8>] = buf.unfilled_mut();
221221

222222
// We're just delegating to the libc read function, which returns an `isize`. The return value indicates
223223
// an error if negative and the number of bytes read otherwise.
@@ -230,11 +230,11 @@ impl Read for TcpStream {
230230
let nread = nread as usize;
231231
// If the read succeeded, tell the buffer that the read-to portion has been initialized. This method is
232232
// 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);
234234
// And indicate that we've written the bytes as well. Unlike `assume_initialized`, this method is safe,
235235
// and asserts that the written portion of the buffer does not advance beyond the initialized portion of
236236
// the buffer. If we didn't call `assume_initialized` above, this call could panic.
237-
buf.add_written(nread);
237+
buf.add_filled(nread);
238238

239239
Ok(())
240240
}
@@ -246,15 +246,15 @@ impl Read for TcpStream {
246246
[reference-level-explanation]: #reference-level-explanation
247247

248248
```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.
250250
///
251251
/// 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
254254
/// subset of the initialized region.
255255
pub struct ReadBuf<'a> {
256256
buf: &'a mut [MaybeUninit<u8>],
257-
written: usize,
257+
filled: usize,
258258
initialized: usize,
259259
}
260260

@@ -273,94 +273,94 @@ impl<'a> ReadBuf<'a> {
273273
#[inline]
274274
pub fn len(&self) -> usize { ... }
275275

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.
277277
#[inline]
278-
pub fn written(&self) -> &[u8] { ... }
278+
pub fn filled(&self) -> &[u8] { ... }
279279

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.
281281
#[inline]
282-
pub fn written_mut(&mut self) -> &mut [u8] { ... }
282+
pub fn filled_mut(&mut self) -> &mut [u8] { ... }
283283

284284
/// Returns a shared reference to the initialized portion of the buffer.
285285
///
286-
/// This includes the written-to portion.
286+
/// This includes the filled portion.
287287
#[inline]
288288
pub fn initialized(&self) -> &[u8] { ... }
289289

290290
/// Returns a mutable reference to the initialized portion of the buffer.
291291
///
292-
/// This includes the written-to portion.
292+
/// This includes the filled portion.
293293
#[inline]
294294
pub fn initialized_mut(&mut self) -> &mut [u8] { ... }
295295

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
297297
/// initialized.
298298
///
299299
/// # Safety
300300
///
301301
/// The caller must not de-initialize portions of the buffer that have already been initialized.
302302
#[inline]
303-
pub unsafe fn unwritten_mut(&mut self) -> &mut [MaybeUninit<u8>] { ... }
303+
pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<u8>] { ... }
304304

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.
306306
///
307307
/// Since `ReadBuf` tracks the region of the buffer that has been initialized, this is effectively "free" after
308308
/// the first use.
309309
#[inline]
310-
pub fn initialize_unwritten(&mut self) -> &mut [u8] { ... }
310+
pub fn initialize_unfilled(&mut self) -> &mut [u8] { ... }
311311

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
313313
/// fully initialized.
314314
///
315315
/// # Panics
316316
///
317317
/// Panics if `self.remaining()` is less than `n`.
318318
#[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] { ... }
320320

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.
322322
#[inline]
323323
pub fn remaining(&self) -> usize { ... }
324324

325-
/// Clears the buffer, resetting the written-to region to empty.
325+
/// Clears the buffer, resetting the filled region to empty.
326326
///
327327
/// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
328328
#[inline]
329329
pub fn clear(&mut self) { ... }
330330

331-
/// Increases the size of the written-to region of the buffer.
331+
/// Increases the size of the filled region of the buffer.
332332
///
333333
/// The number of initialized bytes is not changed.
334334
///
335335
/// # Panics
336336
///
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.
338338
#[inline]
339-
pub fn add_written(&mut self, n: usize) { ... }
339+
pub fn add_filled(&mut self, n: usize) { ... }
340340

341-
/// Sets the size of the written-to region of the buffer.
341+
/// Sets the size of the filled region of the buffer.
342342
///
343343
/// The number of initialized bytes is not changed.
344344
///
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
346346
/// example, by a `Read` implementation that compresses data in-place).
347347
///
348348
/// # Panics
349349
///
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.
351351
#[inline]
352352
pub fn set_written(&mut self, n: usize) { ... }
353353

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.
355355
///
356356
/// `ReadBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
357357
/// bytes than are already known to be initialized.
358358
///
359359
/// # Safety
360360
///
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.
362362
#[inline]
363-
pub unsafe fn assume_initialized(&mut self, n: usize) { ... }
363+
pub unsafe fn assume_init(&mut self, n: usize) { ... }
364364

365365
/// Appends data to the buffer, advancing the written position and possibly also the initialized position.
366366
///
@@ -390,8 +390,8 @@ pub trait Read {
390390
///
391391
/// The default implementation delegates to `read`.
392392
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);
395395
Ok(())
396396
}
397397

@@ -429,14 +429,14 @@ pub trait Read {
429429
/// allow use with uninitialized buffers.
430430
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
431431
while buf.remaining() > 0 {
432-
let prev_written = buf.written().len();
432+
let prev_filled = buf.filled().len();
433433
match self.read_buf(&mut buf) {
434434
Ok(()) => {}
435435
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
436436
Err(e) => return Err(e),
437437
}
438438

439-
if buf.written.len() == prev_written {
439+
if buf.filled().len() == prev_filled {
440440
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill buffer"));
441441
}
442442
}
@@ -455,7 +455,7 @@ pub trait Read {
455455

456456
let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
457457
unsafe {
458-
read_buf.assume_initialized(initialized);
458+
read_buf.assume_init(initialized);
459459
}
460460

461461
match self.read_buf(&mut read_buf) {
@@ -464,12 +464,12 @@ pub trait Read {
464464
Err(e) => return Err(e),
465465
}
466466

467-
if read_buf.written().is_empty() {
467+
if read_buf.filled().is_empty() {
468468
break;
469469
}
470470

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();
473473
unsafe {
474474
buf.set_len(new_len);
475475
}
@@ -495,12 +495,12 @@ where
495495
Err(e) => return Err(e),
496496
};
497497

498-
if buf.written().is_empty() {
498+
if buf.filled().is_empty() {
499499
break;
500500
}
501501

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())?;
504504
buf.clear();
505505
}
506506

0 commit comments

Comments
 (0)