Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ features = ["codec"]
default = []
libudev = ["mio-serial/libudev"]
rt = ["tokio/rt-multi-thread"]
codec = ["tokio-util/codec", "bytes"]
codec = ["tokio-util/codec", "tokio-util/io", "bytes"]

[dependencies.futures]
version = "0.3"
Expand Down
23 changes: 8 additions & 15 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use super::SerialStream;
use tokio_util::codec::{Decoder, Encoder};

use futures::{Sink, Stream};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::io::AsyncWrite;

use bytes::{BufMut, BytesMut};
use bytes::BytesMut;
use futures::ready;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{io, mem::MaybeUninit};

/// A unified [`Stream`] and [`Sink`] interface to an underlying `SerialStream`, using
/// the `Encoder` and `Decoder` traits to encode and decode frames.
Expand Down Expand Up @@ -68,18 +68,11 @@ impl<C: Decoder + Unpin> Stream for SerialFramed<C> {
}

// We're out of data. Try and fetch more data to decode
unsafe {
// Convert `&mut [MaybeUnit<u8>]` to `&mut [u8]` because we will be
// writing to it via `poll_recv_from` and therefore initializing the memory.
let buf = &mut *(pin.rd.chunk_mut() as *mut _ as *mut [MaybeUninit<u8>]);
let mut read = ReadBuf::uninit(buf);
let ptr = read.filled().as_ptr();
ready!(Pin::new(&mut pin.port).poll_read(cx, &mut read))?;

assert_eq!(ptr, read.filled().as_ptr());
pin.rd.advance_mut(read.filled().len());
};

ready!(tokio_util::io::poll_read_buf(
Pin::new(&mut pin.port),
cx,
&mut pin.rd
))?;
pin.is_readable = true;
}
}
Expand Down