1 use Poll;
2 use stream::Stream;
3
4 /// A stream combinator which will change the error type of a stream from one
5 /// type to another.
6 ///
7 /// This is produced by the `Stream::map_err` method.
8 #[derive(Debug)]
9 #[must_use = "streams do nothing unless polled"]
10 pub struct MapErr<S, F> {
11 stream: S,
12 f: F,
13 }
14
new<S, F, U>(s: S, f: F) -> MapErr<S, F> where S: Stream, F: FnMut(S::Error) -> U,15 pub fn new<S, F, U>(s: S, f: F) -> MapErr<S, F>
16 where S: Stream,
17 F: FnMut(S::Error) -> U,
18 {
19 MapErr {
20 stream: s,
21 f: f,
22 }
23 }
24
25 impl<S, F> MapErr<S, F> {
26 /// Acquires a reference to the underlying stream that this combinator is
27 /// pulling from.
get_ref(&self) -> &S28 pub fn get_ref(&self) -> &S {
29 &self.stream
30 }
31
32 /// Acquires a mutable reference to the underlying stream that this
33 /// combinator is pulling from.
34 ///
35 /// Note that care must be taken to avoid tampering with the state of the
36 /// stream which may otherwise confuse this combinator.
get_mut(&mut self) -> &mut S37 pub fn get_mut(&mut self) -> &mut S {
38 &mut self.stream
39 }
40
41 /// Consumes this combinator, returning the underlying stream.
42 ///
43 /// Note that this may discard intermediate state of this combinator, so
44 /// care should be taken to avoid losing resources when this is called.
into_inner(self) -> S45 pub fn into_inner(self) -> S {
46 self.stream
47 }
48 }
49
50 // Forwarding impl of Sink from the underlying stream
51 impl<S, F> ::sink::Sink for MapErr<S, F>
52 where S: ::sink::Sink
53 {
54 type SinkItem = S::SinkItem;
55 type SinkError = S::SinkError;
56
start_send(&mut self, item: S::SinkItem) -> ::StartSend<S::SinkItem, S::SinkError>57 fn start_send(&mut self, item: S::SinkItem) -> ::StartSend<S::SinkItem, S::SinkError> {
58 self.stream.start_send(item)
59 }
60
poll_complete(&mut self) -> Poll<(), S::SinkError>61 fn poll_complete(&mut self) -> Poll<(), S::SinkError> {
62 self.stream.poll_complete()
63 }
64
close(&mut self) -> Poll<(), S::SinkError>65 fn close(&mut self) -> Poll<(), S::SinkError> {
66 self.stream.close()
67 }
68 }
69
70 impl<S, F, U> Stream for MapErr<S, F>
71 where S: Stream,
72 F: FnMut(S::Error) -> U,
73 {
74 type Item = S::Item;
75 type Error = U;
76
poll(&mut self) -> Poll<Option<S::Item>, U>77 fn poll(&mut self) -> Poll<Option<S::Item>, U> {
78 self.stream.poll().map_err(&mut self.f)
79 }
80 }
81