• 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 { sink, item: Some(item) }
21     }
22 
sink_pin_mut(&mut self) -> Pin<&mut Si>23     pub(super) fn sink_pin_mut(&mut self) -> Pin<&mut Si> {
24         Pin::new(self.sink)
25     }
26 
is_item_pending(&self) -> bool27     pub(super) fn is_item_pending(&self) -> bool {
28         self.item.is_some()
29     }
30 }
31 
32 impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Feed<'_, Si, Item> {
33     type Output = Result<(), Si::Error>;
34 
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>35     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36         let this = self.get_mut();
37         let mut sink = Pin::new(&mut this.sink);
38         ready!(sink.as_mut().poll_ready(cx))?;
39         let item = this.item.take().expect("polled Feed after completion");
40         sink.as_mut().start_send(item)?;
41         Poll::Ready(Ok(()))
42     }
43 }
44