nyquest_interface/async/
body.rs

1//! Async body types for HTTP requests.
2//!
3//! This module defines types for handling asynchronous request bodies.
4
5use std::io;
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9use futures_io::{AsyncRead, AsyncSeek};
10
11/// Trait for seekable async body streams with a known size.
12pub trait SizedBodyStream: AsyncRead + AsyncSeek + Send + 'static {}
13
14/// Trait for unsized async body streams that do not support seeking.
15pub trait UnsizedBodyStream: AsyncRead + Send + 'static {}
16
17/// A boxed async stream type that can either be sized or unsized.
18pub enum BoxedStream {
19    /// Sized stream with a known content length.
20    Sized {
21        /// The underlying stream that provides the body data.
22        stream: Pin<Box<dyn SizedBodyStream>>,
23        /// Content length of the stream.
24        content_length: u64,
25    },
26    /// Unsized stream without a known content length.
27    Unsized {
28        /// The underlying stream that provides the body data.
29        stream: Pin<Box<dyn UnsizedBodyStream>>,
30    },
31}
32
33/// Type alias for asynchronous HTTP request bodies.
34pub type Body = crate::body::Body<BoxedStream>;
35
36impl<S: AsyncRead + AsyncSeek + Send + 'static + ?Sized> SizedBodyStream for S {}
37impl<S: AsyncRead + Send + 'static + ?Sized> UnsizedBodyStream for S {}
38
39impl AsyncRead for BoxedStream {
40    fn poll_read(
41        self: Pin<&mut Self>,
42        cx: &mut Context<'_>,
43        buf: &mut [u8],
44    ) -> Poll<io::Result<usize>> {
45        match self.get_mut() {
46            BoxedStream::Sized { stream, .. } => Pin::new(stream).poll_read(cx, buf),
47            BoxedStream::Unsized { stream } => Pin::new(stream).poll_read(cx, buf),
48        }
49    }
50}