1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::{Permit, Reserve};
use core::{
    future::Future,
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};
use futures_core::{ready, FusedFuture};
use futures_sink::Sink;

/// Future for the [`flush_reserve`](super::SinkExt::flush_reserve) method.
#[derive(Debug)]
#[must_use]
pub struct FlushReserve<'a, Si: ?Sized, Item> {
    reserve: Reserve<'a, Si, Item>,
    state: FlushReserveState,
    _phantom: PhantomData<fn(Item)>,
}

// By default, Unpin would be implemented for FlushReserve even if Si isn't Unpin. But we explicitly
// only support Unpin sinks.
impl<Si: Unpin + ?Sized, Item> Unpin for FlushReserve<'_, Si, Item> {}

impl<'a, Item, Si: Sink<Item> + Unpin + ?Sized> FlushReserve<'a, Si, Item> {
    pub(super) fn new(sink: &'a mut Si) -> Self {
        Self {
            reserve: Reserve::new(sink),
            state: FlushReserveState::PollFlush,
            _phantom: PhantomData,
        }
    }
}

impl<'a, Si: Sink<Item> + Unpin + ?Sized, Item> Future for FlushReserve<'a, Si, Item> {
    type Output = Result<Permit<'a, Si, Item>, Si::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = &mut *self;
        if this.state == FlushReserveState::PollFlush {
            ready!(this
                .reserve
                .sink_pin_mut()
                .expect("PollFlush => sink must be Some")
                .poll_flush(cx))?;
            // Move to the reserve state.
            this.state = FlushReserveState::Reserve;
        }

        debug_assert_eq!(this.state, FlushReserveState::Reserve);

        Pin::new(&mut this.reserve).poll(cx)
    }
}

impl<Si: Sink<Item> + Unpin + ?Sized, Item> FusedFuture for FlushReserve<'_, Si, Item> {
    fn is_terminated(&self) -> bool {
        // Once a permit has been issued, the sink becomes None.
        self.reserve.is_terminated()
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum FlushReserveState {
    PollFlush,
    Reserve,
}