use crate::waker::noop_waker;
use ::core::future::Future;
use ::core::pin::Pin;
use ::core::task::{Context, Poll, Waker};
pub struct TestingFuturePoller<OutType> {
future: Pin<Box<dyn Future<Output = OutType> + 'static + Send>>,
}
impl<OutType> TestingFuturePoller<OutType> {
pub fn new(future: impl Future<Output = OutType> + 'static + Send) -> TestingFuturePoller<OutType> {
Self { future: Box::pin(future) }
}
pub fn from_boxed(boxed_future: Pin<Box<dyn Future<Output = OutType> + 'static + Send>>) -> TestingFuturePoller<OutType> {
Self { future: boxed_future }
}
pub fn poll_with_waker(&mut self, waker: &Waker) -> Poll<OutType> {
let mut cx = Context::from_waker(waker);
self.future.as_mut().poll(&mut cx)
}
pub fn poll_n_with_waker(&mut self, n: usize, waker: &Waker) -> Poll<OutType> {
let mut cx = Context::from_waker(waker);
let mut result: Poll<OutType> = Poll::Pending;
for _ in 0..n {
result = self.future.as_mut().poll(&mut cx)
}
result
}
pub fn poll(&mut self) -> Poll<OutType> {
self.poll_with_waker(&noop_waker())
}
pub fn poll_n(&mut self, n: usize) -> Poll<OutType> {
self.poll_n_with_waker(n, &noop_waker())
}
}
pub struct AlwaysPending {}
impl Future for AlwaysPending {
type Output = ();
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Pending
}
}
pub fn assert_poll_ready<T: PartialEq + ::core::fmt::Debug>(res: ::core::task::Poll<T>, val: T) {
match res {
::core::task::Poll::Ready(v) => assert_eq!(val, v),
::core::task::Poll::Pending => panic!("Expected Poll::Ready but got Poll::Pending"),
}
}