use std::{future::Future, pin::Pin, task::Poll};
use http::Response;
use hyper::body::Incoming;
use tokio::{
io::{AsyncRead, AsyncWrite},
sync::oneshot,
};
pub type ResponseFuture = Pin<Box<dyn Future<Output = hyper::Result<Response<Incoming>>> + Send>>;
pub fn oneshot_result<T>() -> (oneshot::Sender<crate::nq_core::Result<T>>, OneshotResult<T>) {
let (tx, rx) = oneshot::channel();
(tx, OneshotResult { inner: rx })
}
pub struct OneshotResult<T> {
inner: oneshot::Receiver<crate::nq_core::Result<T>>,
}
impl<T> Future for OneshotResult<T> {
type Output = crate::nq_core::Result<T>;
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
match Pin::new(&mut self.get_mut().inner).poll(cx) {
Poll::Ready(Ok(t)) => Poll::Ready(t),
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
Poll::Pending => Poll::Pending,
}
}
}
pub trait ByteStream: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static {}
impl<T> ByteStream for T where T: AsyncRead + AsyncWrite + Sync + Send + Unpin + 'static {}