• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::pin::Pin;
2 use futures_core::future::Future;
3 use futures_core::ready;
4 use futures_core::task::{Context, Poll};
5 use 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"]
10 pub struct Feed<'a, Si: ?Sized, Item> {
11     sink: &'a mut Si,
12     item: Option<Item>,
13 }
14 
15 // Pinning is never projected to children
16 impl<Si: Unpin + ?Sized, Item> Unpin for Feed<'_, Si, Item> {}
17 
18 impl<'a, Si: Sink<Item> + Unpin + ?Sized, Item> Feed<'a, Si, Item> {
new(sink: &'a mut Si, item: Item) -> Self19     pub(super) fn new(sink: &'a mut Si, item: Item) -> Self {
20         Feed {
21             sink,
22             item: Some(item),
23         }
24     }
25 
sink_pin_mut(&mut self) -> Pin<&mut Si>26     pub(super) fn sink_pin_mut(&mut self) -> Pin<&mut Si> {
27         Pin::new(self.sink)
28     }
29 
is_item_pending(&self) -> bool30     pub(super) fn is_item_pending(&self) -> bool {
31         self.item.is_some()
32     }
33 }
34 
35 impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Feed<'_, Si, Item> {
36     type Output = Result<(), Si::Error>;
37 
poll( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Self::Output>38     fn poll(
39         self: Pin<&mut Self>,
40         cx: &mut Context<'_>,
41     ) -> Poll<Self::Output> {
42         let this = self.get_mut();
43         let mut sink = Pin::new(&mut this.sink);
44         ready!(sink.as_mut().poll_ready(cx))?;
45         let item = this.item.take().expect("polled Feed after completion");
46         sink.as_mut().start_send(item)?;
47         Poll::Ready(Ok(()))
48     }
49 }
50