Skip to content

Commit

Permalink
Allow unsized writer for copy_{buf_}into
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Jun 25, 2019
1 parent b30dfbb commit 8846080
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
10 changes: 5 additions & 5 deletions futures-util/src/io/copy_buf_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use std::pin::Pin;
/// Future for the [`copy_buf_into`](super::AsyncBufReadExt::copy_buf_into) method.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct CopyBufInto<'a, R, W> {
pub struct CopyBufInto<'a, R, W: ?Sized> {
reader: R,
writer: &'a mut W,
amt: u64,
}

impl<R: Unpin, W> Unpin for CopyBufInto<'_, R, W> {}
impl<R: Unpin, W: ?Sized> Unpin for CopyBufInto<'_, R, W> {}

impl<R, W> CopyBufInto<'_, R, W> {
impl<R, W: ?Sized> CopyBufInto<'_, R, W> {
pub(super) fn new(reader: R, writer: &mut W) -> CopyBufInto<'_, R, W> {
CopyBufInto {
reader,
Expand All @@ -25,7 +25,7 @@ impl<R, W> CopyBufInto<'_, R, W> {
}
}

impl<R, W: Unpin> CopyBufInto<'_, R, W> {
impl<R, W: Unpin + ?Sized> CopyBufInto<'_, R, W> {
fn project<'b>(self: Pin<&'b mut Self>) -> (Pin<&'b mut R>, Pin<&'b mut W>, &'b mut u64) {
unsafe {
let this = self.get_unchecked_mut();
Expand All @@ -36,7 +36,7 @@ impl<R, W: Unpin> CopyBufInto<'_, R, W> {

impl<R, W> Future for CopyBufInto<'_, R, W>
where R: AsyncBufRead,
W: AsyncWrite + Unpin,
W: AsyncWrite + Unpin + ?Sized,
{
type Output = io::Result<u64>;

Expand Down
8 changes: 4 additions & 4 deletions futures-util/src/io/copy_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use pin_utils::unsafe_pinned;
/// Future for the [`copy_into`](super::AsyncReadExt::copy_into) method.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct CopyInto<'a, R: AsyncRead, W> {
pub struct CopyInto<'a, R: AsyncRead, W: ?Sized> {
inner: CopyBufInto<'a, BufReader<R>, W>,
}

impl<'a, R: AsyncRead, W> Unpin for CopyInto<'a, R, W> where CopyBufInto<'a, BufReader<R>, W>: Unpin {}
impl<'a, R: AsyncRead, W: ?Sized> Unpin for CopyInto<'a, R, W> where CopyBufInto<'a, BufReader<R>, W>: Unpin {}

impl<'a, R: AsyncRead, W> CopyInto<'a, R, W> {
impl<'a, R: AsyncRead, W: ?Sized> CopyInto<'a, R, W> {
unsafe_pinned!(inner: CopyBufInto<'a, BufReader<R>, W>);

pub(super) fn new(reader: R, writer: &mut W) -> CopyInto<'_, R, W> {
Expand All @@ -25,7 +25,7 @@ impl<'a, R: AsyncRead, W> CopyInto<'a, R, W> {
}
}

impl<R: AsyncRead, W: AsyncWrite + Unpin> Future for CopyInto<'_, R, W> {
impl<R: AsyncRead, W: AsyncWrite + Unpin + ?Sized> Future for CopyInto<'_, R, W> {
type Output = io::Result<u64>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down
12 changes: 10 additions & 2 deletions futures-util/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ pub trait AsyncReadExt: AsyncRead {
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
/// ```
fn copy_into<W: AsyncWrite + Unpin>(self, writer: &mut W) -> CopyInto<'_, Self, W> where Self: Sized {
fn copy_into<W>(self, writer: &mut W) -> CopyInto<'_, Self, W>
where
Self: Sized,
W: AsyncWrite + Unpin + ?Sized,
{
CopyInto::new(self, writer)
}

Expand Down Expand Up @@ -470,7 +474,11 @@ pub trait AsyncBufReadExt: AsyncBufRead {
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
/// ```
fn copy_buf_into<W: AsyncWrite + Unpin>(self, writer: &mut W) -> CopyBufInto<'_, Self, W> where Self: Sized {
fn copy_buf_into<W>(self, writer: &mut W) -> CopyBufInto<'_, Self, W>
where
Self: Sized,
W: AsyncWrite + Unpin + ?Sized,
{
CopyBufInto::new(self, writer)
}

Expand Down

0 comments on commit 8846080

Please sign in to comment.