From 851341e96d642f6848ae285d576c40a4495aa62f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 6 Oct 2023 12:08:37 +1100 Subject: [PATCH] Reduce diff --- yamux/src/frame.rs | 4 ++-- yamux/src/frame/io.rs | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/yamux/src/frame.rs b/yamux/src/frame.rs index a6165241..50f6e464 100644 --- a/yamux/src/frame.rs +++ b/yamux/src/frame.rs @@ -95,9 +95,9 @@ impl From> for Frame<()> { impl Frame<()> { pub(crate) fn try_from_header_buffer( - buffer: [u8; HEADER_SIZE], + buffer: &[u8; HEADER_SIZE], ) -> Result, Frame>, FrameDecodeError> { - let header = header::decode(&buffer)?; + let header = header::decode(buffer)?; let either = match header.try_into_data() { Ok(data) => Either::Right(Frame::new(data)), diff --git a/yamux/src/frame/io.rs b/yamux/src/frame/io.rs index 47a359ed..fd2809b7 100644 --- a/yamux/src/frame/io.rs +++ b/yamux/src/frame/io.rs @@ -59,7 +59,7 @@ impl fmt::Debug for WriteState { f, "(WriteState::Writing (offset {}) (buffer-len {}))", offset, - frame.len() + frame.buffer().len() ) } } @@ -141,7 +141,10 @@ impl Stream for Io { log::trace!("{}: read: {:?}", this.id, this.read_state); match &mut this.read_state { - ReadState::Header { offset, mut buffer } => { + ReadState::Header { + offset, + ref mut buffer, + } => { if *offset == header::HEADER_SIZE { let frame = Frame::try_from_header_buffer(buffer)?; @@ -167,7 +170,8 @@ impl Stream for Io { continue; } - match ready!(Pin::new(&mut this.io).poll_read(cx, &mut buffer[*offset..]))? { + let buf = &mut buffer[*offset..header::HEADER_SIZE]; + match ready!(Pin::new(&mut this.io).poll_read(cx, buf))? { 0 => { if *offset == 0 { return Poll::Ready(None); @@ -175,12 +179,7 @@ impl Stream for Io { let e = FrameDecodeError::Io(io::ErrorKind::UnexpectedEof.into()); return Poll::Ready(Some(Err(e))); } - n => { - this.read_state = ReadState::Header { - buffer, - offset: *offset + n, - }; - } + n => *offset += n, } } ReadState::Body { @@ -299,11 +298,7 @@ mod tests { fn property(f: Frame<()>) -> bool { futures::executor::block_on(async move { let id = crate::connection::Id::random(); - let mut io = Io::new( - id, - futures::io::Cursor::new(Vec::new()), - f.buffer.len(), - ); + let mut io = Io::new(id, futures::io::Cursor::new(Vec::new()), f.buffer.len()); if io.send(f.clone()).await.is_err() { return false; }