core/muxing: Drop Unpin requirement from SubstreamBox (#2762)

We are already boxing the given object so we might as well pin to
to avoid the `Unpin` trait bound.
This commit is contained in:
Thomas Eizinger
2022-07-22 10:40:27 +01:00
committed by GitHub
parent 51a847128c
commit f15a3dc4e0
2 changed files with 19 additions and 17 deletions

View File

@ -17,7 +17,7 @@ pub struct StreamMuxerBox {
///
/// A [`SubstreamBox`] erases the concrete type it is given and only retains its `AsyncRead`
/// and `AsyncWrite` capabilities.
pub struct SubstreamBox(Box<dyn AsyncReadWrite + Send + Unpin>);
pub struct SubstreamBox(Pin<Box<dyn AsyncReadWrite + Send>>);
struct Wrap<T>
where
@ -106,8 +106,8 @@ impl StreamMuxer for StreamMuxerBox {
impl SubstreamBox {
/// Construct a new [`SubstreamBox`] from something that implements [`AsyncRead`] and [`AsyncWrite`].
pub fn new<S: AsyncRead + AsyncWrite + Send + Unpin + 'static>(stream: S) -> Self {
Self(Box::new(stream))
pub fn new<S: AsyncRead + AsyncWrite + Send + 'static>(stream: S) -> Self {
Self(Box::pin(stream))
}
}
@ -118,7 +118,7 @@ impl fmt::Debug for SubstreamBox {
}
/// Workaround because Rust does not allow `Box<dyn AsyncRead + AsyncWrite>`.
trait AsyncReadWrite: AsyncRead + AsyncWrite + Unpin {
trait AsyncReadWrite: AsyncRead + AsyncWrite {
/// Helper function to capture the erased inner type.
///
/// Used to make the [`Debug`] implementation of [`SubstreamBox`] more useful.
@ -127,7 +127,7 @@ trait AsyncReadWrite: AsyncRead + AsyncWrite + Unpin {
impl<S> AsyncReadWrite for S
where
S: AsyncRead + AsyncWrite + Unpin,
S: AsyncRead + AsyncWrite,
{
fn type_name(&self) -> &'static str {
std::any::type_name::<S>()
@ -136,44 +136,44 @@ where
impl AsyncRead for SubstreamBox {
fn poll_read(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_read(cx, buf)
self.0.as_mut().poll_read(cx, buf)
}
fn poll_read_vectored(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_read_vectored(cx, bufs)
self.0.as_mut().poll_read_vectored(cx, bufs)
}
}
impl AsyncWrite for SubstreamBox {
fn poll_write(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
self.0.as_mut().poll_write(cx, buf)
}
fn poll_write_vectored(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_write_vectored(cx, bufs)
self.0.as_mut().poll_write_vectored(cx, bufs)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().0).poll_flush(cx)
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
self.0.as_mut().poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().0).poll_close(cx)
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
self.0.as_mut().poll_close(cx)
}
}