1use super::Sink;
2use core::future::Future;
3use core::pin::Pin;
4use core::task::{Context, Poll};
5
6#[derive(Debug)]
8#[must_use = "futures do nothing unless you `.await` or poll them"]
9pub struct Feed<'a, Si: ?Sized, Item> {
10 sink: &'a mut Si,
11 item: Option<Item>,
12}
13
14impl<Si: ?Sized, Item> Unpin for Feed<'_, Si, Item> {}
17
18impl<'a, Si: Sink<Item> + Unpin + ?Sized, Item> Feed<'a, Si, Item> {
19 pub(super) fn new(sink: &'a mut Si, item: Item) -> Self {
20 Feed {
21 sink,
22 item: Some(item),
23 }
24 }
25
26 pub(super) fn sink_pin_mut(&mut self) -> Pin<&mut Si> {
27 Pin::new(self.sink)
28 }
29
30 pub(super) fn is_item_pending(&self) -> bool {
31 self.item.is_some()
32 }
33}
34
35impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Feed<'_, Si, Item> {
36 type Output = Result<(), Si::Error>;
37
38 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
39 let this = self.get_mut();
40 let mut sink = Pin::new(&mut *this.sink);
41 match sink.as_mut().poll_ready(cx) {
42 Poll::Ready(Ok(())) => {
43 let item = this.item.take().expect("polled Feed after completion");
44 Poll::Ready(sink.as_mut().start_send(item))
45 }
46 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
47 Poll::Pending => Poll::Pending,
48 }
49 }
50}