futures_util/sink/
err_into.rs

1use futures_core::{Stream, Poll};
2use futures_core::task;
3use futures_sink::{Sink};
4use sink::{SinkExt, SinkMapErr};
5
6/// A sink combinator to change the error type of a sink.
7///
8/// This is created by the `Sink::err_into` method.
9#[derive(Clone, Debug)]
10#[must_use = "futures do nothing unless polled"]
11pub struct SinkErrInto<S: Sink, E> {
12    sink: SinkMapErr<S, fn(S::SinkError) -> E>,
13}
14
15pub fn new<S, E>(sink: S) -> SinkErrInto<S, E>
16    where S: Sink,
17          S::SinkError: Into<E>
18{
19    SinkErrInto {
20        sink: SinkExt::sink_map_err(sink, Into::into),
21    }
22}
23
24impl<S: Sink, E> SinkErrInto<S, E> {
25    /// Get a shared reference to the inner sink.
26    pub fn get_ref(&self) -> &S {
27        self.sink.get_ref()
28    }
29
30    /// Get a mutable reference to the inner sink.
31    pub fn get_mut(&mut self) -> &mut S {
32        self.sink.get_mut()
33    }
34
35    /// Consumes this combinator, returning the underlying sink.
36    ///
37    /// Note that this may discard intermediate state of this combinator, so
38    /// care should be taken to avoid losing resources when this is called.
39    pub fn into_inner(self) -> S {
40        self.sink.into_inner()
41    }
42}
43
44impl<S, E> Sink for SinkErrInto<S, E>
45    where S: Sink,
46          S::SinkError: Into<E>,
47{
48    type SinkItem = S::SinkItem;
49    type SinkError = E;
50
51    delegate_sink!(sink);
52}
53
54impl<S: Sink + Stream, E> Stream for SinkErrInto<S, E> {
55    type Item = S::Item;
56    type Error = S::Error;
57
58    fn poll_next(&mut self, cx: &mut task::Context) -> Poll<Option<S::Item>, S::Error> {
59        self.sink.poll_next(cx)
60    }
61}