|
| 1 | +use crate::stream::inspect; |
| 2 | +use core::pin::Pin; |
| 3 | +use futures_core::stream::{FusedStream, Stream, TryStream}; |
| 4 | +use futures_core::task::{Context, Poll}; |
| 5 | +use futures_sink::Sink; |
| 6 | +use pin_utils::{unsafe_pinned, unsafe_unpinned}; |
| 7 | + |
| 8 | +/// Stream for the [`inspect_err`](super::TryStreamExt::inspect_err) method. |
| 9 | +#[derive(Debug)] |
| 10 | +#[must_use = "streams do nothing unless polled"] |
| 11 | +pub struct InspectErr<St, F> { |
| 12 | + stream: St, |
| 13 | + f: F, |
| 14 | +} |
| 15 | + |
| 16 | +impl<St: TryStream + Unpin, F> Unpin for InspectErr<St, F> {} |
| 17 | + |
| 18 | +impl<St, F> InspectErr<St, F> |
| 19 | +where |
| 20 | + St: TryStream, |
| 21 | + F: FnMut(&St::Error), |
| 22 | +{ |
| 23 | + unsafe_pinned!(stream: St); |
| 24 | + unsafe_unpinned!(f: F); |
| 25 | + |
| 26 | + pub(super) fn new(stream: St, f: F) -> Self { |
| 27 | + Self { stream, f } |
| 28 | + } |
| 29 | + |
| 30 | + /// Acquires a reference to the underlying stream that this combinator is |
| 31 | + /// pulling from. |
| 32 | + pub fn get_ref(&self) -> &St { |
| 33 | + &self.stream |
| 34 | + } |
| 35 | + |
| 36 | + /// Acquires a mutable reference to the underlying stream that this |
| 37 | + /// combinator is pulling from. |
| 38 | + /// |
| 39 | + /// Note that care must be taken to avoid tampering with the state of the |
| 40 | + /// stream which may otherwise confuse this combinator. |
| 41 | + pub fn get_mut(&mut self) -> &mut St { |
| 42 | + &mut self.stream |
| 43 | + } |
| 44 | + |
| 45 | + /// Acquires a pinned mutable reference to the underlying stream that this |
| 46 | + /// combinator is pulling from. |
| 47 | + /// |
| 48 | + /// Note that care must be taken to avoid tampering with the state of the |
| 49 | + /// stream which may otherwise confuse this combinator. |
| 50 | + pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut St> { |
| 51 | + self.stream() |
| 52 | + } |
| 53 | + |
| 54 | + /// Consumes this combinator, returning the underlying stream. |
| 55 | + /// |
| 56 | + /// Note that this may discard intermediate state of this combinator, so |
| 57 | + /// care should be taken to avoid losing resources when this is called. |
| 58 | + pub fn into_inner(self) -> St { |
| 59 | + self.stream |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<St: TryStream + FusedStream, F> FusedStream for InspectErr<St, F> { |
| 64 | + fn is_terminated(&self) -> bool { |
| 65 | + self.stream.is_terminated() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<St, F> Stream for InspectErr<St, F> |
| 70 | +where |
| 71 | + St: TryStream, |
| 72 | + F: FnMut(&St::Error), |
| 73 | +{ |
| 74 | + type Item = Result<St::Ok, St::Error>; |
| 75 | + |
| 76 | + fn poll_next( |
| 77 | + mut self: Pin<&mut Self>, |
| 78 | + cx: &mut Context<'_>, |
| 79 | + ) -> Poll<Option<Self::Item>> { |
| 80 | + self.as_mut() |
| 81 | + .stream() |
| 82 | + .try_poll_next(cx) |
| 83 | + .map(|opt| opt.map(|res| res.map_err(|e| inspect(e, self.as_mut().f())))) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Forwarding impl of Sink from the underlying stream |
| 88 | +impl<S, F, Item> Sink<Item> for InspectErr<S, F> |
| 89 | +where |
| 90 | + S: TryStream + Sink<Item>, |
| 91 | + F: FnMut(&S::Error), |
| 92 | +{ |
| 93 | + type SinkError = S::SinkError; |
| 94 | + |
| 95 | + delegate_sink!(stream, Item); |
| 96 | +} |
0 commit comments