nyquest_interface/blocking/
body.rs

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