1use core::pin::Pin;
2use futures_core::future::Future;
3use futures_core::ready;
4use futures_core::task::{Context, Poll};
5use futures_sink::Sink;
6
7/// Future for the [`feed`](super::SinkExt::feed) method.
8#[derive(Debug)]
9#[must_use = "futures do nothing unless you `.await` or poll them"]
10pub struct Feed<'a, Si: ?Sized, Item> {
11 sink: &'a mut Si,
12 item: Option<Item>,
13}
14
15// Pinning is never projected to children
16impl<Si: Unpin + ?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 { sink, item: Some(item) }
21 }
22
23 pub(super) fn sink_pin_mut(&mut self) -> Pin<&mut Si> {
24 Pin::new(self.sink)
25 }
26
27 pub(super) fn is_item_pending(&self) -> bool {
28 self.item.is_some()
29 }
30}
31
32impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Feed<'_, Si, Item> {
33 type Output = Result<(), Si::Error>;
34
35 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36 let this: &mut Feed<'_, Si, Item> = self.get_mut();
37 let mut sink: Pin<&mut &mut Si> = Pin::new(&mut this.sink);
38 ready!(sink.as_mut().poll_ready(cx))?;
39 let item: Item = this.item.take().expect(msg:"polled Feed after completion");
40 sink.as_mut().start_send(item)?;
41 Poll::Ready(Ok(()))
42 }
43}
44