multipart-write 0.1.0

Sink-like interface for writing an object in parts
Documentation
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::{FusedMultipartWrite, MultipartWrite};

pin_project_lite::pin_project! {
    /// `MultipartWrite` for [`map_err`](super::MultipartWriteExt::map_err).
    #[must_use = "futures do nothing unless polled"]
    pub struct MapErr<Wr, Part, E, F> {
        #[pin]
        writer: Wr,
        f: F,
        _p: PhantomData<fn(Part) -> E>
    }
}

impl<Wr, Part, E, F> MapErr<Wr, Part, E, F> {
    pub(super) fn new(writer: Wr, f: F) -> Self {
        Self { writer, f, _p: PhantomData }
    }

    /// Consumes `MapErr`, returning the underlying writer.
    pub fn into_inner(self) -> Wr {
        self.writer
    }

    /// Acquires a reference to the underlying writer.
    pub fn get_ref(&self) -> &Wr {
        &self.writer
    }

    /// Acquires a mutable reference to the underlying writer.
    ///
    /// It is inadvisable to directly write to the underlying writer.
    pub fn get_mut(&mut self) -> &mut Wr {
        &mut self.writer
    }

    /// Acquires a pinned mutable reference to the underlying writer.
    ///
    /// It is inadvisable to directly write to the underlying writer.
    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
        self.project().writer
    }
}

impl<Wr, Part, E, F> FusedMultipartWrite<Part> for MapErr<Wr, Part, E, F>
where
    Wr: FusedMultipartWrite<Part>,
    F: FnMut(Wr::Error) -> E,
{
    fn is_terminated(&self) -> bool {
        self.writer.is_terminated()
    }
}

impl<Wr, Part, E, F> MultipartWrite<Part> for MapErr<Wr, Part, E, F>
where
    Wr: MultipartWrite<Part>,
    F: FnMut(Wr::Error) -> E,
{
    type Error = E;
    type Output = Wr::Output;
    type Recv = Wr::Recv;

    fn poll_ready(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        self.as_mut()
            .project()
            .writer
            .poll_ready(cx)
            .map_err(self.as_mut().project().f)
    }

    fn start_send(
        mut self: Pin<&mut Self>,
        part: Part,
    ) -> Result<Self::Recv, Self::Error> {
        self.as_mut()
            .project()
            .writer
            .start_send(part)
            .map_err(self.as_mut().project().f)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        self.as_mut()
            .project()
            .writer
            .poll_flush(cx)
            .map_err(self.as_mut().project().f)
    }

    fn poll_complete(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<Self::Output, Self::Error>> {
        self.as_mut()
            .project()
            .writer
            .poll_complete(cx)
            .map_err(self.as_mut().project().f)
    }
}

impl<Wr: Debug, Part, E, F> Debug for MapErr<Wr, Part, E, F> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("MapErr").field("writer", &self.writer).finish()
    }
}