Skip to content

Commit 69ad06f

Browse files
committed
Add Bytes::byte field.
We can reuse this in `next`, avoiding the need to zero-initialize a local variable every time, for a small speed win.
1 parent ca7eef7 commit 69ad06f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

library/std/src/io/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ pub trait Read {
10001000
where
10011001
Self: Sized,
10021002
{
1003-
Bytes { inner: self }
1003+
Bytes { inner: self, byte: 0 }
10041004
}
10051005

10061006
/// Creates an adapter which will chain this stream with another.
@@ -2772,6 +2772,7 @@ impl<T> SizeHint for Take<T> {
27722772
#[derive(Debug)]
27732773
pub struct Bytes<R> {
27742774
inner: R,
2775+
byte: u8,
27752776
}
27762777

27772778
#[stable(feature = "rust1", since = "1.0.0")]
@@ -2780,11 +2781,10 @@ impl<R: Read> Iterator for Bytes<R> {
27802781

27812782
#[inline]
27822783
fn next(&mut self) -> Option<Result<u8>> {
2783-
let mut byte = 0;
27842784
loop {
2785-
return match self.inner.read(slice::from_mut(&mut byte)) {
2785+
return match self.inner.read(slice::from_mut(&mut self.byte)) {
27862786
Ok(0) => None,
2787-
Ok(..) => Some(Ok(byte)),
2787+
Ok(..) => Some(Ok(self.byte)),
27882788
Err(ref e) if e.is_interrupted() => continue,
27892789
Err(e) => Some(Err(e)),
27902790
};

0 commit comments

Comments
 (0)