rustls: add write_vectored implementation (#42)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
Eliza Weisman 2020-12-07 19:30:55 -08:00 committed by GitHub
parent 0c2d573a4e
commit c2dbab6c5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 10 deletions

View File

@ -12,7 +12,7 @@ categories = ["asynchronous", "cryptography", "network-programming"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
tokio = "0.3" tokio = "0.3.5"
rustls = "0.19" rustls = "0.19"
webpki = "0.21" webpki = "0.21"
@ -21,7 +21,7 @@ early-data = []
dangerous_configuration = ["rustls/dangerous_configuration"] dangerous_configuration = ["rustls/dangerous_configuration"]
[dev-dependencies] [dev-dependencies]
tokio = { version = "0.3", features = ["full"] } tokio = { version = "0.3.5", features = ["full"] }
futures-util = "0.3.1" futures-util = "0.3.1"
lazy_static = "1" lazy_static = "1"
webpki-roots = "0.21" webpki-roots = "0.21"

View File

@ -2,7 +2,7 @@ mod handshake;
pub(crate) use handshake::{IoSession, MidHandshake}; pub(crate) use handshake::{IoSession, MidHandshake};
use rustls::Session; use rustls::Session;
use std::io::{self, Read, Write}; use std::io::{self, IoSlice, Read, Write};
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
@ -127,20 +127,32 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {
cx: &'a mut Context<'b>, cx: &'a mut Context<'b>,
} }
impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> { impl<'a, 'b, T: Unpin> Writer<'a, 'b, T> {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn poll_with<U>(
match Pin::new(&mut self.io).poll_write(self.cx, buf) { &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) {
Poll::Ready(result) => result, Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()), Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
} }
} }
}
impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.poll_with(|io, cx| io.poll_write(cx, buf))
}
#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.poll_with(|io, cx| io.poll_write_vectored(cx, bufs))
}
fn flush(&mut self) -> io::Result<()> { fn flush(&mut self) -> io::Result<()> {
match Pin::new(&mut self.io).poll_flush(self.cx) { self.poll_with(|io, cx| io.poll_flush(cx))
Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
}
} }
} }