| 1 |
use core::fmt; |
| 2 |
use core::marker::PhantomData; |
| 3 |
use core::pin::Pin; |
| 4 |
use futures_core::future::Future; |
| 5 |
use futures_core::ready; |
| 6 |
use futures_core::stream::{FusedStream, Stream}; |
| 7 |
use futures_core::task::{Context, Poll}; |
| 8 |
use futures_sink::Sink; |
| 9 |
use pin_project_lite::pin_project; |
| 10 |
|
| 11 |
pin_project! { |
| 12 |
/// Sink for the [`with`](super::SinkExt::with) method. |
| 13 |
#[must_use = "sinks do nothing unless polled"] |
| 14 |
pub struct With<Si, Item, U, Fut, F> { |
| 15 |
#[pin] |
| 16 |
sink: Si, |
| 17 |
f: F, |
| 18 |
#[pin] |
| 19 |
state: Option<Fut>, |
| 20 |
_phantom: PhantomData<fn(U) -> Item>, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl<Si, Item, U, Fut, F> fmt::Debug for With<Si, Item, U, Fut, F> |
| 25 |
where |
| 26 |
Si: fmt::Debug, |
| 27 |
Fut: fmt::Debug, |
| 28 |
{ |
| 29 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 |
f.debug_struct("With").field("sink", &self.sink).field("state", &self.state).finish() |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl<Si, Item, U, Fut, F> With<Si, Item, U, Fut, F> |
| 35 |
where |
| 36 |
Si: Sink<Item>, |
| 37 |
F: FnMut(U) -> Fut, |
| 38 |
Fut: Future, |
| 39 |
{ |
| 40 |
pub(super) fn new<E>(sink: Si, f: F) -> Self |
| 41 |
where |
| 42 |
Fut: Future<Output = Result<Item, E>>, |
| 43 |
E: From<Si::Error>, |
| 44 |
{ |
| 45 |
Self { state: None, sink, f, _phantom: PhantomData } |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl<Si, Item, U, Fut, F> Clone for With<Si, Item, U, Fut, F> |
| 50 |
where |
| 51 |
Si: Clone, |
| 52 |
F: Clone, |
| 53 |
Fut: Clone, |
| 54 |
{ |
| 55 |
fn clone(&self) -> Self { |
| 56 |
Self { |
| 57 |
state: self.state.clone(), |
| 58 |
sink: self.sink.clone(), |
| 59 |
f: self.f.clone(), |
| 60 |
_phantom: PhantomData, |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Forwarding impl of Stream from the underlying sink |
| 66 |
impl<S, Item, U, Fut, F> Stream for With<S, Item, U, Fut, F> |
| 67 |
where |
| 68 |
S: Stream + Sink<Item>, |
| 69 |
F: FnMut(U) -> Fut, |
| 70 |
Fut: Future, |
| 71 |
{ |
| 72 |
type Item = S::Item; |
| 73 |
|
| 74 |
delegate_stream!(sink); |
| 75 |
} |
| 76 |
|
| 77 |
impl<S, Item, U, Fut, F> FusedStream for With<S, Item, U, Fut, F> |
| 78 |
where |
| 79 |
S: FusedStream + Sink<Item>, |
| 80 |
F: FnMut(U) -> Fut, |
| 81 |
Fut: Future, |
| 82 |
{ |
| 83 |
fn is_terminated(&self) -> bool { |
| 84 |
self.sink.is_terminated() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl<Si, Item, U, Fut, F, E> With<Si, Item, U, Fut, F> |
| 89 |
where |
| 90 |
Si: Sink<Item>, |
| 91 |
F: FnMut(U) -> Fut, |
| 92 |
Fut: Future<Output = Result<Item, E>>, |
| 93 |
E: From<Si::Error>, |
| 94 |
{ |
| 95 |
delegate_access_inner!(sink, Si, ()); |
| 96 |
|
| 97 |
/// Completes the processing of previous item if any. |
| 98 |
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), E>> { |
| 99 |
let mut this = self.project(); |
| 100 |
|
| 101 |
let item = match this.state.as_mut().as_pin_mut() { |
| 102 |
None => return Poll::Ready(Ok(())), |
| 103 |
Some(fut) => ready!(fut.poll(cx))?, |
| 104 |
}; |
| 105 |
this.state.set(None); |
| 106 |
this.sink.start_send(item)?; |
| 107 |
Poll::Ready(Ok(())) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl<Si, Item, U, Fut, F, E> Sink<U> for With<Si, Item, U, Fut, F> |
| 112 |
where |
| 113 |
Si: Sink<Item>, |
| 114 |
F: FnMut(U) -> Fut, |
| 115 |
Fut: Future<Output = Result<Item, E>>, |
| 116 |
E: From<Si::Error>, |
| 117 |
{ |
| 118 |
type Error = E; |
| 119 |
|
| 120 |
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 121 |
ready!(self.as_mut().poll(cx))?; |
| 122 |
ready!(self.project().sink.poll_ready(cx)?); |
| 123 |
Poll::Ready(Ok(())) |
| 124 |
} |
| 125 |
|
| 126 |
fn start_send(self: Pin<&mut Self>, item: U) -> Result<(), Self::Error> { |
| 127 |
let mut this = self.project(); |
| 128 |
|
| 129 |
assert!(this.state.is_none()); |
| 130 |
this.state.set(Some((this.f)(item))); |
| 131 |
Ok(()) |
| 132 |
} |
| 133 |
|
| 134 |
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 135 |
ready!(self.as_mut().poll(cx))?; |
| 136 |
ready!(self.project().sink.poll_flush(cx)?); |
| 137 |
Poll::Ready(Ok(())) |
| 138 |
} |
| 139 |
|
| 140 |
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 141 |
ready!(self.as_mut().poll(cx))?; |
| 142 |
ready!(self.project().sink.poll_close(cx)?); |
| 143 |
Poll::Ready(Ok(())) |
| 144 |
} |
| 145 |
} |