|
|
|
@ -89,24 +89,7 @@ where |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn read_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> { |
|
|
|
|
struct Reader<'a, 'b, T> { |
|
|
|
|
io: &'a mut T, |
|
|
|
|
cx: &'a mut Context<'b>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a, 'b, T: AsyncRead + Unpin> Read for Reader<'a, 'b, T> { |
|
|
|
|
#[inline] |
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
|
|
|
|
let mut buf = ReadBuf::new(buf); |
|
|
|
|
match Pin::new(&mut self.io).poll_read(self.cx, &mut buf) { |
|
|
|
|
Poll::Ready(Ok(())) => Ok(buf.filled().len()), |
|
|
|
|
Poll::Ready(Err(err)) => Err(err), |
|
|
|
|
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut reader = Reader { io: self.io, cx }; |
|
|
|
|
let mut reader = SyncReadAdapter { io: self.io, cx }; |
|
|
|
|
|
|
|
|
|
let n = match self.session.read_tls(&mut reader) { |
|
|
|
|
Ok(n) => n, |
|
|
|
@ -145,7 +128,7 @@ where |
|
|
|
|
&mut self, |
|
|
|
|
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<io::Result<U>>, |
|
|
|
|
) -> io::Result<U> { |
|
|
|
|
match f(Pin::new(&mut self.io), self.cx) { |
|
|
|
|
match f(Pin::new(self.io), self.cx) { |
|
|
|
|
Poll::Ready(result) => result, |
|
|
|
|
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()), |
|
|
|
|
} |
|
|
|
@ -343,5 +326,26 @@ where |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// An adapter that implements a [`Read`] interface for [`AsyncRead`] types and an
|
|
|
|
|
/// associated [`Context`].
|
|
|
|
|
///
|
|
|
|
|
/// Turns `Poll::Pending` into `WouldBlock`.
|
|
|
|
|
pub struct SyncReadAdapter<'a, 'b, T> { |
|
|
|
|
pub io: &'a mut T, |
|
|
|
|
pub cx: &'a mut Context<'b>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a, 'b, T: AsyncRead + Unpin> Read for SyncReadAdapter<'a, 'b, T> { |
|
|
|
|
#[inline] |
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
|
|
|
|
let mut buf = ReadBuf::new(buf); |
|
|
|
|
match Pin::new(&mut self.io).poll_read(self.cx, &mut buf) { |
|
|
|
|
Poll::Ready(Ok(())) => Ok(buf.filled().len()), |
|
|
|
|
Poll::Ready(Err(err)) => Err(err), |
|
|
|
|
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
mod test_stream; |
|
|
|
|