use super::Permit;
use core::{
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use futures_core::{ready, FusedFuture};
use futures_sink::Sink;
#[derive(Debug)]
#[must_use]
pub struct Reserve<'a, Si: ?Sized, Item> {
sink: Option<&'a mut Si>,
_phantom: PhantomData<fn(Item)>,
}
impl<Si: Unpin + ?Sized, Item> Unpin for Reserve<'_, Si, Item> {}
impl<'a, Item, Si: Sink<Item> + Unpin + ?Sized> Reserve<'a, Si, Item> {
pub(super) fn new(sink: &'a mut Si) -> Self {
Self {
sink: Some(sink),
_phantom: PhantomData,
}
}
pub(super) fn sink_pin_mut(&mut self) -> Option<Pin<&mut Si>> {
#[allow(clippy::manual_map)]
match &mut self.sink {
Some(sink) => Some(Pin::new(sink)),
None => None,
}
}
}
impl<'a, Si: Sink<Item> + Unpin + ?Sized, Item> Future for Reserve<'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> {
if let Some(sink) = self.sink.as_mut() {
ready!(Pin::new(sink).poll_ready(cx))?;
let sink = self.sink.take().unwrap();
Poll::Ready(Ok(Permit::new(sink)))
} else {
Poll::Pending
}
}
}
impl<Si: Sink<Item> + Unpin + ?Sized, Item> FusedFuture for Reserve<'_, Si, Item> {
fn is_terminated(&self) -> bool {
self.sink.is_none()
}
}