@@ -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
0 commit comments