Skip to content

Commit

Permalink
Reduce diff
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 6, 2023
1 parent 8d32d16 commit 851341e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions yamux/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ impl<A: header::private::Sealed> From<Frame<A>> for Frame<()> {

impl Frame<()> {
pub(crate) fn try_from_header_buffer(
buffer: [u8; HEADER_SIZE],
buffer: &[u8; HEADER_SIZE],
) -> Result<Either<Frame<()>, Frame<Data>>, 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)),
Expand Down
23 changes: 9 additions & 14 deletions yamux/src/frame/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl fmt::Debug for WriteState {
f,
"(WriteState::Writing (offset {}) (buffer-len {}))",
offset,
frame.len()
frame.buffer().len()
)
}
}
Expand Down Expand Up @@ -141,7 +141,10 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Stream for Io<T> {
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)?;

Expand All @@ -167,20 +170,16 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Stream for Io<T> {
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);
}
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 {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 851341e

Please sign in to comment.