/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.28/src/sink/feed.rs
Line | Count | Source |
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> { |
19 | 0 | pub(super) fn new(sink: &'a mut Si, item: Item) -> Self { |
20 | 0 | Feed { sink, item: Some(item) } |
21 | 0 | } |
22 | | |
23 | 0 | pub(super) fn sink_pin_mut(&mut self) -> Pin<&mut Si> { |
24 | 0 | Pin::new(self.sink) |
25 | 0 | } |
26 | | |
27 | 0 | pub(super) fn is_item_pending(&self) -> bool { |
28 | 0 | self.item.is_some() |
29 | 0 | } |
30 | | } |
31 | | |
32 | | impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Feed<'_, Si, Item> { |
33 | | type Output = Result<(), Si::Error>; |
34 | | |
35 | 0 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
36 | 0 | let this = self.get_mut(); |
37 | 0 | let mut sink = Pin::new(&mut this.sink); |
38 | 0 | ready!(sink.as_mut().poll_ready(cx))?; |
39 | 0 | let item = this.item.take().expect("polled Feed after completion"); |
40 | 0 | sink.as_mut().start_send(item)?; |
41 | 0 | Poll::Ready(Ok(())) |
42 | 0 | } |
43 | | } |