|
|
|
@ -104,7 +104,7 @@ pub struct TestProtocolStream {
|
|
|
|
|
impl TestProtocolStream {
|
|
|
|
|
/// Read from the receiver and remaining buffer
|
|
|
|
|
async fn read_from_receiver(
|
|
|
|
|
buf: &mut ReadBuf<'static>,
|
|
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
|
receiver: Arc<Mutex<Receiver<Vec<u8>>>>,
|
|
|
|
|
remaining_buf: Arc<Mutex<Vec<u8>>>,
|
|
|
|
|
) {
|
|
|
|
@ -133,7 +133,7 @@ impl TestProtocolStream {
|
|
|
|
|
/// Read from the remaining buffer returning a boolean if the
|
|
|
|
|
/// read buffer has been filled
|
|
|
|
|
async fn read_from_remaining_buffer(
|
|
|
|
|
buf: &mut ReadBuf<'static>,
|
|
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
|
remaining_buf: &mut Vec<u8>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
if remaining_buf.len() < buf.capacity() {
|
|
|
|
@ -180,27 +180,26 @@ impl AsyncProtocolStream for TestProtocolStream {
|
|
|
|
|
|
|
|
|
|
impl AsyncRead for TestProtocolStream {
|
|
|
|
|
fn poll_read(
|
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
|
) -> Poll<std::io::Result<()>> {
|
|
|
|
|
unsafe {
|
|
|
|
|
// we need a mutable reference to access the inner future
|
|
|
|
|
let stream = self.get_unchecked_mut();
|
|
|
|
|
|
|
|
|
|
if stream.future.is_none() {
|
|
|
|
|
if self.future.is_none() {
|
|
|
|
|
// we need to change the lifetime to be able to use the read buffer in the read future
|
|
|
|
|
let buf: &mut ReadBuf<'static> = mem::transmute(buf);
|
|
|
|
|
let receiver = Arc::clone(&stream.receiver);
|
|
|
|
|
let remaining_buf = Arc::clone(&stream.remaining_buf);
|
|
|
|
|
let buf: &mut ReadBuf<'static> = unsafe {
|
|
|
|
|
// SAFETY: idk tbh
|
|
|
|
|
mem::transmute(buf)
|
|
|
|
|
};
|
|
|
|
|
let receiver = Arc::clone(&self.receiver);
|
|
|
|
|
let remaining_buf = Arc::clone(&self.remaining_buf);
|
|
|
|
|
|
|
|
|
|
let future = TestProtocolStream::read_from_receiver(buf, receiver, remaining_buf);
|
|
|
|
|
stream.future = Some(Box::pin(future));
|
|
|
|
|
self.future = Some(Box::pin(future));
|
|
|
|
|
}
|
|
|
|
|
if let Some(future) = &mut stream.future {
|
|
|
|
|
if let Some(future) = &mut self.future {
|
|
|
|
|
match future.as_mut().poll(cx) {
|
|
|
|
|
Poll::Ready(_) => {
|
|
|
|
|
stream.future = None;
|
|
|
|
|
self.future = None;
|
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
|
}
|
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
@ -209,34 +208,32 @@ impl AsyncRead for TestProtocolStream {
|
|
|
|
|
Poll::Pending
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Unpin for TestProtocolStream {}
|
|
|
|
|
|
|
|
|
|
impl AsyncWrite for TestProtocolStream {
|
|
|
|
|
fn poll_write(
|
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
|
buf: &[u8],
|
|
|
|
|
) -> Poll<std::prelude::rust_2015::Result<usize, Error>> {
|
|
|
|
|
let write_len = buf.len();
|
|
|
|
|
unsafe {
|
|
|
|
|
// we need a mutable reference to access the inner future
|
|
|
|
|
let stream = self.get_unchecked_mut();
|
|
|
|
|
|
|
|
|
|
if stream.future.is_none() {
|
|
|
|
|
if self.future.is_none() {
|
|
|
|
|
// we take ownership here so that we don't need to change lifetimes here
|
|
|
|
|
let buf = buf.to_vec();
|
|
|
|
|
let sender = stream.sender.clone();
|
|
|
|
|
let sender = self.sender.clone();
|
|
|
|
|
|
|
|
|
|
let future = async move {
|
|
|
|
|
sender.send(buf).await.unwrap();
|
|
|
|
|
};
|
|
|
|
|
stream.future = Some(Box::pin(future));
|
|
|
|
|
self.future = Some(Box::pin(future));
|
|
|
|
|
}
|
|
|
|
|
if let Some(future) = &mut stream.future {
|
|
|
|
|
if let Some(future) = &mut self.future {
|
|
|
|
|
match future.as_mut().poll(cx) {
|
|
|
|
|
Poll::Ready(_) => {
|
|
|
|
|
stream.future = None;
|
|
|
|
|
self.future = None;
|
|
|
|
|
Poll::Ready(Ok(write_len))
|
|
|
|
|
}
|
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
@ -245,7 +242,6 @@ impl AsyncWrite for TestProtocolStream {
|
|
|
|
|
Poll::Pending
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn poll_flush(
|
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
|