use std::task::Poll;
use futures::future::{BoxFuture, FutureExt};
use tower::{Service, ServiceExt};
pub trait IsReady<Request>: Service<Request> {
#[allow(clippy::wrong_self_convention)]
fn is_ready(&mut self) -> BoxFuture<bool>;
#[allow(clippy::wrong_self_convention)]
fn is_pending(&mut self) -> BoxFuture<bool>;
#[allow(clippy::wrong_self_convention)]
fn is_failed(&mut self) -> BoxFuture<bool>;
}
impl<S, Request> IsReady<Request> for S
where
S: Service<Request> + Send,
Request: 'static,
{
fn is_ready(&mut self) -> BoxFuture<bool> {
async move {
let ready_result = futures::poll!(self.ready());
matches!(ready_result, Poll::Ready(Ok(_)))
}
.boxed()
}
fn is_pending(&mut self) -> BoxFuture<bool> {
async move {
let ready_result = futures::poll!(self.ready());
ready_result.is_pending()
}
.boxed()
}
fn is_failed(&mut self) -> BoxFuture<bool> {
async move {
let ready_result = futures::poll!(self.ready());
matches!(ready_result, Poll::Ready(Err(_)))
}
.boxed()
}
}