Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 16 pull requests #101114

Closed
wants to merge 57 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e540425
Add a `File::create_new` constructor
joshtriplett Jul 2, 2022
c1aae4d
std::io: migrate ReadBuf to BorrowBuf/BorrowCursor
nrc May 13, 2022
b56cf67
Add some docs for BorrowBuf
nrc May 23, 2022
1a2122f
non-linux platforms
nrc Jun 7, 2022
c04568b
Stabilize `std::io::read_to_string`
camelid Aug 9, 2022
7b4cd17
Start uplifting `clippy::for_loops_over_fallibles`
WaffleLapkin Jul 17, 2022
810cf60
Use structured suggestions for `for_loop_over_fallibles` lint
WaffleLapkin Jul 18, 2022
b661157
`for_loop_over_fallibles`: Suggest removing `.next()`
WaffleLapkin Jul 24, 2022
7cf94ad
`for_loop_over_fallibles`: suggest `while let` loop
WaffleLapkin Jul 24, 2022
14b8f24
`for_loop_over_fallibles`: suggest using `?` in some cases
WaffleLapkin Jul 24, 2022
2bf213b
`for_loop_over_fallibles`: remove duplication from the message
WaffleLapkin Jul 24, 2022
5128140
Add a test for the `for_loop_over_fallibles` lint
WaffleLapkin Jul 24, 2022
34815a9
`for_loop_over_fallibles`: fix suggestion for "remove `.next()`" case
WaffleLapkin Jul 24, 2022
c4ab59e
`for_loop_over_fallibles`: don't use `MachineApplicable`
WaffleLapkin Jul 24, 2022
41fccb1
allow or avoid for loops over option in compiler and tests
WaffleLapkin Jul 26, 2022
86360f4
allow `for_loop_over_fallibles` in a `core` test
WaffleLapkin Jul 26, 2022
d7b8a65
Edit documentation for `for_loop_over_fallibles` lint
WaffleLapkin Aug 14, 2022
aed1ae4
remove an infinite loop
WaffleLapkin Aug 15, 2022
71b8c89
fix `for_loop_over_fallibles` lint docs
WaffleLapkin Aug 18, 2022
ac70aea
Address reviewer comments
nrc Aug 11, 2022
85b3df2
Export Cancel from std::os::fortanix_sgx::usercalls::raw
mzohreva Aug 22, 2022
2a26987
Add GDB/LLDB pretty-printers for NonZero types
artemmukhin Jun 20, 2022
36c42fa
Use `DisplayBuffer` for socket addresses.
reitermarkus Aug 16, 2022
63700a8
Add tests for `SockAddr` `Display`.
reitermarkus Aug 19, 2022
89c74e8
Move `net::parser` into `net::addr` module.
reitermarkus Aug 19, 2022
d61ecec
Flatten `net` module again.
reitermarkus Aug 24, 2022
80442f3
error::Error: rename the chain method to sources
nrc Aug 24, 2022
b556a5b
error::Error: rename the Demand arguments from req to demand
nrc Aug 24, 2022
b508b50
translations: rename warn_ to warning
beowolx Aug 24, 2022
4e97626
Call them constants instead of types
compiler-errors Aug 12, 2022
4ff5872
Note binding obligation causes for const equate errors
compiler-errors Aug 12, 2022
d464d3a
Add test for #100414
compiler-errors Aug 12, 2022
8189a45
Use ExprItemObligation and ExprBindingObligation too
compiler-errors Aug 23, 2022
9372c4f
error::Error: remove some comments
nrc Aug 25, 2022
252c65e
Fix clippy tests that trigger `for_loop_over_fallibles` lint
WaffleLapkin Aug 25, 2022
7529029
Provide structured suggestion for `hashmap[idx] = val`
estebank Aug 25, 2022
c3f568b
Do not report too many expr field candidates
compiler-errors Aug 23, 2022
b33c3d6
use smaller span for suggestions
TaKO8Ki Aug 26, 2022
aa76e13
extend attrs if local_def_id exists
TaKO8Ki Aug 27, 2022
fc3f3c3
rustc_middle: Remove `Visibility::Invisible`
petrochenkov Aug 27, 2022
4a82c21
unstable-book-gen: use std::fs::write
est31 Aug 27, 2022
084bf8d
Rollup merge of #97015 - nrc:read-buf-cursor, r=Mark-Simulacrum
matthiaskrgr Aug 28, 2022
e5ca6a6
Rollup merge of #98301 - ortem:pretty-printers-nonzero, r=wesleywiser
matthiaskrgr Aug 28, 2022
be9ed40
Rollup merge of #98801 - joshtriplett:file-create-new, r=thomcc
matthiaskrgr Aug 28, 2022
049c73e
Rollup merge of #99696 - WaffleLapkin:uplift, r=fee1-dead
matthiaskrgr Aug 28, 2022
b87b660
Rollup merge of #100337 - camelid:stabilize-io_read_to_string, r=John…
matthiaskrgr Aug 28, 2022
ea30a62
Rollup merge of #100437 - compiler-errors:better-const-mismatch-err, …
matthiaskrgr Aug 28, 2022
09496a8
Rollup merge of #100640 - reitermarkus:socket-display-buffer, r=thomcc
matthiaskrgr Aug 28, 2022
9ccd9c3
Rollup merge of #100885 - mzohreva:mz/sgx-export-cancel-type, r=Mark-…
matthiaskrgr Aug 28, 2022
f83d275
Rollup merge of #100898 - compiler-errors:too-many-expr-fields, r=spa…
matthiaskrgr Aug 28, 2022
0b8e6a2
Rollup merge of #100955 - nrc:chain, r=joshtriplett
matthiaskrgr Aug 28, 2022
ac11fcb
Rollup merge of #100959 - LuisCardosoOliveira:translation-rename-attr…
matthiaskrgr Aug 28, 2022
f37d3ea
Rollup merge of #101002 - estebank:hashmap-idx, r=davidtwco
matthiaskrgr Aug 28, 2022
df64d82
Rollup merge of #101055 - TaKO8Ki:use-smaller-span, r=compiler-errors
matthiaskrgr Aug 28, 2022
0dc6ab6
Rollup merge of #101091 - TaKO8Ki:fix-101076, r=notriddle
matthiaskrgr Aug 28, 2022
58ed74c
Rollup merge of #101098 - petrochenkov:noinvis, r=TaKO8Ki
matthiaskrgr Aug 28, 2022
a5fd83a
Rollup merge of #101102 - est31:unstable_book_gen_write, r=Mark-Simul…
matthiaskrgr Aug 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some docs for BorrowBuf
Signed-off-by: Nick Cameron <nrc@ncameron.org>
nrc committed Aug 4, 2022
commit b56cf67ce14580111ffb07a08a293e217566e116
84 changes: 60 additions & 24 deletions library/std/src/io/readbuf.rs
Original file line number Diff line number Diff line change
@@ -7,8 +7,7 @@ use crate::cmp;
use crate::fmt::{self, Debug, Formatter};
use crate::mem::MaybeUninit;

// TODO docs
/// A wrapper around a byte buffer that is incrementally filled and initialized.
/// A borrowed byte buffer which is incrementally filled and initialized.
///
/// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the
/// 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;
/// [ filled | unfilled ]
/// [ initialized | uninitialized ]
/// ```
///
/// A `BorrowBuf` is created around some existing data (or capacity for data) via a unique reference
/// (`&mut`). The `BorrowBuf` can be configured (e.g., using `clear` or `set_init`), but otherwise
/// is read-only. To write into the buffer, use `unfilled` to create a `BorrowCursor`. The cursor
/// has write-only access to the unfilled portion of the buffer (you can think of it like a
/// write-only iterator).
///
/// The lifetime `'a` is a bound on the lifetime of the underlying data.
pub struct BorrowBuf<'a> {
/// The buffer's underlying data.
buf: &'a mut [MaybeUninit<u8>],
/// The length of `self.buf` which is known to be filled.
filled: usize,
/// The length of `self.buf` which is known to be initialized.
initialized: usize,
}

@@ -37,7 +47,7 @@ impl Debug for BorrowBuf<'_> {
}
}

/// Creates a new `BorrowBuf` from a fully initialized slice.
/// Create a new `BorrowBuf` from a fully initialized slice.
impl<'a> From<&'a mut [u8]> for BorrowBuf<'a> {
#[inline]
fn from(slice: &'a mut [u8]) -> BorrowBuf<'a> {
@@ -52,7 +62,7 @@ impl<'a> From<&'a mut [u8]> for BorrowBuf<'a> {
}
}

/// Creates a new `BorrowBuf` from a fully uninitialized buffer.
/// Create a new `BorrowBuf` from an uninitialized buffer.
///
/// Use `set_init` if part of the buffer is known to be already initialized.
impl<'a> From<&'a mut [MaybeUninit<u8>]> for BorrowBuf<'a> {
@@ -90,7 +100,7 @@ impl<'a> BorrowBuf<'a> {

/// Returns a cursor over the unfilled part of the buffer.
#[inline]
pub fn unfilled<'b>(&'b mut self) -> BorrowCursor<'a, 'b> {
pub fn unfilled<'this>(&'this mut self) -> BorrowCursor<'this, 'a> {
BorrowCursor { start: self.filled, buf: self }
}

@@ -118,20 +128,36 @@ impl<'a> BorrowBuf<'a> {
}
}

/// A cursor view of a [`BorrowBuf`](BorrowBuf).
/// A writeable view of the unfilled portion of a [`BorrowBuf`](BorrowBuf).
///
/// Provides access to the initialized and uninitialized parts of the underlying `BorrowBuf`.
/// Data can be written directly to the cursor by using [`append`](BorrowCursor::append) or
/// indirectly by getting a slice of part or all of the cursor and writing into the slice. In the
/// indirect case, the caller must call [`advance`](BorrowCursor::advance) after writing to inform
/// the cursor how many bytes have been written.
///
/// Provides mutable access to the unfilled portion (both initialised and uninitialised data) from
/// the buffer.
/// Once data is written to the cursor, it becomes part of the filled portion of the underlying
/// `BorrowBuf` and can no longer be accessed or re-written by the cursor. I.e., the cursor tracks
/// the unfilled part of the underlying `BorrowBuf`.
///
/// The `'buf` lifetime is a bound on the lifetime of the underlying buffer. `'data` is a bound on
/// that buffer's underlying data.
#[derive(Debug)]
pub struct BorrowCursor<'a, 'b> {
buf: &'b mut BorrowBuf<'a>,
pub struct BorrowCursor<'buf, 'data> {
/// The underlying buffer.
buf: &'buf mut BorrowBuf<'data>,
/// The length of the filled portion of the underlying buffer at the time of the cursor's
/// creation.
start: usize,
}

impl<'a, 'b> BorrowCursor<'a, 'b> {
impl<'buf, 'data> BorrowCursor<'buf, 'data> {
/// Clone this cursor.
///
/// Since a cursor maintains unique access to its underlying buffer, the cloned cursor is not
/// accessible while the clone is alive.
#[inline]
pub fn clone<'c>(&'c mut self) -> BorrowCursor<'a, 'c> {
pub fn clone<'this>(&'this mut self) -> BorrowCursor<'this, 'data> {
BorrowCursor { buf: self.buf, start: self.start }
}

@@ -141,14 +167,16 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
self.buf.capacity() - self.buf.filled
}

/// Returns the number of bytes written to this cursor.
// TODO check for reuse uses
/// Returns the number of bytes written to this cursor since it was created from a `BorrowBuf`.
///
/// Note that if this cursor is a clone of another, then the count returned is the count written
/// via either cursor, not the count since the cursor was cloned.
#[inline]
pub fn written(&self) -> usize {
self.buf.filled - self.start
}

/// Returns a shared reference to the initialized portion of the buffer.
/// Returns a shared reference to the initialized portion of the cursor.
#[inline]
pub fn init_ref(&self) -> &[u8] {
//SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -157,7 +185,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
}
}

/// Returns a mutable reference to the initialized portion of the buffer.
/// Returns a mutable reference to the initialized portion of the cursor.
#[inline]
pub fn init_mut(&mut self) -> &mut [u8] {
//SAFETY: We only slice the initialized part of the buffer, which is always valid
@@ -168,25 +196,33 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
}
}

/// Returns a mutable reference to the uninitialized part of the buffer.
/// Returns a mutable reference to the uninitialized part of the cursor.
///
/// It is safe to uninitialize any of these bytes.
#[inline]
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
&mut self.buf.buf[self.buf.initialized..]
}

/// A view of the cursor as a mutable slice of `MaybeUninit<u8>`.
/// Returns a mutable reference to the whole cursor.
///
/// # Safety
///
/// The caller must not uninitialize any bytes in the initialized portion of the cursor.
#[inline]
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
&mut self.buf.buf[self.buf.filled..]
}

/// Increases the size of the filled region of the buffer.
/// Advance the cursor by asserting that `n` bytes have been filled.
///
/// After advancing, the `n` bytes are no longer accessible via the cursor and can only be
/// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements
/// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements.
///
/// # Safety
///
/// The caller must ensure that the first `n` elements of the cursor have been properly
/// The caller must ensure that the first `n` bytes of the cursor have been properly
/// initialised.
#[inline]
pub unsafe fn advance(&mut self, n: usize) -> &mut Self {
@@ -195,7 +231,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
self
}

/// Initialised all bytes in the cursor.
/// Initializes all bytes in the cursor.
#[inline]
pub fn ensure_init(&mut self) -> &mut Self {
for byte in self.uninit_mut() {
@@ -208,8 +244,8 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {

/// Asserts that the first `n` unfilled bytes of the cursor are initialized.
///
/// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
/// bytes than are already known to be initialized.
/// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when
/// called with fewer bytes than are already known to be initialized.
///
/// # Safety
///
@@ -220,7 +256,7 @@ impl<'a, 'b> BorrowCursor<'a, 'b> {
self
}

/// Appends data to the cursor, advancing the position within its buffer.
/// Appends data to the cursor, advancing position within its buffer.
///
/// # Panics
///