Skip to content

Commit 10cff15

Browse files
committed
BorrowedBuf: Update outdated safety comments in set_init users.
These comments appear to have been written before `BorrowedBuf`'s init tracking was simplified in #150129. The `BufWriter` comment of the usage within `BufWriter` will be handled separately.
1 parent 12f35ad commit 10cff15

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

library/std/src/io/buffered/bufreader/buffer.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ pub struct Buffer {
2121
// Each call to `fill_buf` sets `filled` to indicate how many bytes at the start of `buf` are
2222
// initialized with bytes from a read.
2323
filled: usize,
24-
// This is the max number of bytes returned across all `fill_buf` calls. We track this so that we
25-
// can accurately tell `read_buf` how many bytes of buf are initialized, to bypass as much of its
26-
// defensive initialization as possible. Note that while this often the same as `filled`, it
27-
// doesn't need to be. Calls to `fill_buf` are not required to actually fill the buffer, and
28-
// omitting this is a huge perf regression for `Read` impls that do not.
24+
// Whether `buf` has been fully initialized. We track this so that we can accurately tell
25+
// `read_buf` how many bytes of buf are initialized, to bypass as much of its defensive
26+
// initialization as possible. Note that while this often the same as `filled`, it doesn't need
27+
// to be. Calls to `fill_buf` are not required to actually fill the buffer, and omitting this
28+
// is a huge perf regression for `Read` impls that do not.
2929
initialized: bool,
3030
}
3131

@@ -112,6 +112,9 @@ impl Buffer {
112112
let mut buf = BorrowedBuf::from(&mut self.buf[self.filled..]);
113113

114114
if self.initialized {
115+
// SAFETY: `self.initialized` is only set after `self.buf` was
116+
// fully initialized, and once `self.buf` is fully initialized
117+
// no part will become uninitialized.
115118
unsafe { buf.set_init() };
116119
}
117120

@@ -138,9 +141,11 @@ impl Buffer {
138141
debug_assert!(self.pos == self.filled);
139142

140143
let mut buf = BorrowedBuf::from(&mut *self.buf);
141-
// SAFETY: `self.filled` bytes will always have been initialized.
142144

143145
if self.initialized {
146+
// SAFETY: `self.initialized` is only set after `self.buf` was
147+
// fully initialized, and once `self.buf` is fully initialized
148+
// no part will become uninitialized.
144149
unsafe { buf.set_init() };
145150
}
146151

library/std/src/io/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3087,8 +3087,9 @@ impl<T: Read> Read for Take<T> {
30873087

30883088
let mut sliced_buf: BorrowedBuf<'_> = ibuf.into();
30893089

3090-
// SAFETY: extra_init bytes of ibuf are known to be initialized
30913090
if is_init {
3091+
// SAFETY: `sliced_buf` is a subslice of `buf`, so if `buf` was initialized then
3092+
// `sliced_buf` is.
30923093
unsafe { sliced_buf.set_init() };
30933094
}
30943095

0 commit comments

Comments
 (0)