rust/library/std/src/sys/windows
Matthias Krüger b9306c231a
Rollup merge of #97015 - nrc:read-buf-cursor, r=Mark-Simulacrum
std::io: migrate ReadBuf to BorrowBuf/BorrowCursor

This PR replaces `ReadBuf` (used by the `Read::read_buf` family of methods) with `BorrowBuf` and `BorrowCursor`.

The general idea is to split `ReadBuf` because its API is large and confusing. `BorrowBuf` represents a borrowed buffer which is mostly read-only and (other than for construction) deals only with filled vs unfilled segments. a `BorrowCursor` is a mostly write-only view of the unfilled part of a `BorrowBuf` which distinguishes between initialized and uninitialized segments. For `Read::read_buf`, the caller would create a `BorrowBuf`, then pass a `BorrowCursor` to `read_buf`.

In addition to the major API split, I've made the following smaller changes:

* Removed some methods entirely from the API (mostly the functionality can be replicated with two calls rather than a single one)
* Unified naming, e.g., by replacing initialized with init and assume_init with set_init
* Added an easy way to get the number of bytes written to a cursor (`written` method)

As well as simplifying the API (IMO), this approach has the following advantages:

* Since we pass the cursor by value, we remove the 'unsoundness footgun' where a malicious `read_buf` could swap out the `ReadBuf`.
* Since `read_buf` cannot write into the filled part of the buffer, we prevent the filled part shrinking or changing which could cause underflow for the caller or unexpected behaviour.

## Outline

```rust
pub struct BorrowBuf<'a>

impl Debug for BorrowBuf<'_>

impl<'a> From<&'a mut [u8]> for BorrowBuf<'a>
impl<'a> From<&'a mut [MaybeUninit<u8>]> for BorrowBuf<'a>

impl<'a> BorrowBuf<'a> {
    pub fn capacity(&self) -> usize
    pub fn len(&self) -> usize
    pub fn init_len(&self) -> usize
    pub fn filled(&self) -> &[u8]
    pub fn unfilled<'this>(&'this mut self) -> BorrowCursor<'this, 'a>
    pub fn clear(&mut self) -> &mut Self
    pub unsafe fn set_init(&mut self, n: usize) -> &mut Self
}

pub struct BorrowCursor<'buf, 'data>

impl<'buf, 'data> BorrowCursor<'buf, 'data> {
    pub fn clone<'this>(&'this mut self) -> BorrowCursor<'this, 'data>
    pub fn capacity(&self) -> usize
    pub fn written(&self) -> usize
    pub fn init_ref(&self) -> &[u8]
    pub fn init_mut(&mut self) -> &mut [u8]
    pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>]
    pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>]
    pub unsafe fn advance(&mut self, n: usize) -> &mut Self
    pub fn ensure_init(&mut self) -> &mut Self
    pub unsafe fn set_init(&mut self, n: usize) -> &mut Self
    pub fn append(&mut self, buf: &[u8])
}
```

## TODO

* ~~Migrate non-unix libs and tests~~
* ~~Naming~~
  * ~~`BorrowBuf` or `BorrowedBuf` or `SliceBuf`? (We might want an owned equivalent for the async IO traits)~~
  * ~~Should we rename the `readbuf` module? We might keep the name indicate it includes both the buf and cursor variations and someday the owned version too. Or we could change it. It is not publicly exposed, so it is not that important~~.
  * ~~`read_buf` method: we read into the cursor now, so the `_buf` suffix is a bit weird.~~
* ~~Documentation~~
* Tests are incomplete (I adjusted existing tests, but did not add new ones).

cc https://github.com/rust-lang/rust/issues/78485, https://github.com/rust-lang/rust/issues/94741
supersedes: https://github.com/rust-lang/rust/pull/95770, https://github.com/rust-lang/rust/pull/93359
fixes #93305
2022-08-28 09:35:11 +02:00
..
alloc Rework std::sys::windows::alloc 2021-03-26 12:38:26 +01:00
args Update Windows arg parsing tests 2021-08-08 22:11:29 +01:00
c Windows error codes: Add two missing ones 2021-06-18 18:51:53 +01:00
handle Tests for unsound Windows file methods 2022-07-06 17:40:21 +01:00
locks Make all {Mutex, Condvar, RwLock}::new #[inline]. 2022-06-06 13:49:23 +02:00
os std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
path fix panic in Path::strip_prefix 2022-05-08 22:15:26 +08:00
process Expose process main_thread_handle on Windows 2022-05-10 02:41:19 -03:00
alloc.rs Replace most uses of pointer::offset with add and sub 2022-08-21 02:21:41 +04:00
args.rs Add a fixme comment 2022-06-28 12:18:16 +01:00
c.rs Windows: Load synch functions together 2022-08-18 07:39:14 +01:00
cmath.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
compat.rs Use const instead of static 2022-08-20 04:15:47 +01:00
env.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
fs.rs Rollup merge of #97015 - nrc:read-buf-cursor, r=Mark-Simulacrum 2022-08-28 09:35:11 +02:00
handle.rs Address reviewer comments 2022-08-18 10:34:40 +01:00
io.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
memchr.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
mod.rs Fix comment typo 2022-08-19 08:45:21 -07:00
net.rs Represent SocketAddrV4 and SocketAddrV6 as Rust native encoding 2022-06-23 21:01:58 +02:00
os.rs Replace most uses of pointer::offset with add and sub 2022-08-21 02:21:41 +04:00
os_str.rs Optimize Wtf8Buf::into_string for the case where it contains UTF-8. 2022-06-23 13:10:47 -07:00
path.rs Improve Windows path prefix parsing 2022-04-17 01:23:46 -04:00
pipe.rs Revert "Auto merge of #96441 - ChrisDenton:sync-pipes, r=m-ou-se" 2022-05-17 18:46:11 -04:00
process.rs Implement ExitCodeExt for Windows 2022-06-09 15:32:01 +02:00
rand.rs Simplify Windows hashmap_random_keys 2022-07-17 11:16:49 +01:00
stack_overflow.rs Apply clippy suggestions 2021-10-09 18:56:01 +02:00
stack_overflow_uwp.rs Move all cleanup to sys::cleanup 2021-04-22 10:44:44 +02:00
stdio.rs Hide Repr details from io::Error, and rework io::Error::new_const. 2022-02-04 18:47:29 -08:00
stdio_uwp.rs Reason safety for unsafe blocks for uwp stdin 2021-09-23 07:29:52 +08:00
thread.rs Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
thread_local_dtor.rs Never inline Windows dtor access 2022-08-01 03:53:16 +01:00
thread_local_key.rs Run destructors from existing tls callback 2021-11-01 15:19:49 +00:00
thread_parker.rs Windows: Load synch functions together 2022-08-18 07:39:14 +01:00
time.rs Support setting file accessed/modified timestamps 2022-07-15 02:54:06 -07:00