Skip to main content

wasi_hyperium/
outgoing.rs

1use std::{
2    future::Future,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use crate::Error;
8
9pub enum Copied {
10    Body(usize),
11    Trailers,
12}
13
14pub trait OutgoingBodyCopier {
15    fn poll_copy(&mut self, cx: &mut Context) -> Poll<Option<Result<Copied, Error>>>;
16
17    fn copy_all(self) -> CopyAllFuture<Self>
18    where
19        Self: Sized + Unpin,
20    {
21        CopyAllFuture(self)
22    }
23}
24
25pub struct CopyAllFuture<T>(T);
26
27impl<T: OutgoingBodyCopier + Unpin> Future for CopyAllFuture<T> {
28    type Output = Result<(), Error>;
29
30    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
31        loop {
32            match self.0.poll_copy(cx) {
33                Poll::Ready(Some(Ok(_))) => (),
34                Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err)),
35                Poll::Ready(None) => return Poll::Ready(Ok(())),
36                Poll::Pending => return Poll::Pending,
37            }
38        }
39    }
40}