In 2930-read-buf, correct the section on using the `Read` trait with an uninitialized buffer
In the current [2930-read-buf](../blob/c143e315774f746d667c5eecd95a8ed999e8a729/text/2930-read-buf.md), it says:
> ```rust
> let mut buf: [u8; 1024] = unsafe { MaybeUninit::uninit().assume_init() };
> let nread = reader.read(&mut buf)?;
> process_data(&buf[..nread]);
> ```
>
> However, whether it is allowed to call `assume_init()` on an array of uninitialized integers is
[still subject of discussion](https://github.com/rust-lang/unsafe-code-guidelines/issues/71).
That “still subject of discussion” issue—about the validity of integers and floating point—has been closed, with the conclusion that uninitialized integers are not valid.
After further research I found [a discussion about whether `&mut uninit` should be UB](https://github.com/rust-lang/unsafe-code-guidelines/issues/346). That issue is still open and unresolved. If I understand the rationale correctly, and if `&mut uninit` is not immediately UB as the discussion suggests, then the following code would not be UB provided the reader is well-behaved:
```rust
let mut buf: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit();
let but_mut = unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, 1024) };
let nread = reader.read(&mut buf)?;
process_data(&buf[..nread]);
```
I think the code sample and accompanying description should be updated to reflect this change and to clarify which aspects are actually still under discussion.
0 条评论