pub enum CollectExactResult<B: HttpBody> {
AtLeast {
buffered: Bytes,
remaining: Option<Remaining<B>>,
},
Incomplete {
buffered: Option<Bytes>,
error: Option<B::Error>,
},
}Expand description
Result of attempting to collect at least N bytes from a body.
Returned by BufferedBody::collect_exact to indicate whether the
requested number of bytes was successfully read from the stream.
§When to Use
Use this when you need to inspect a fixed-size prefix of a body without consuming the entire stream:
- Checking magic bytes for file type detection
- Reading protocol headers
- Validating body format before full processing
§Invariants
AtLeast:buffered.len() >= requested_bytesIncomplete:buffered.len() < requested_bytes(stream ended or error)- The buffered data may exceed the requested size if a frame boundary didn’t align exactly
§Examples
use hitbox_http::{BufferedBody, CollectExactResult};
async fn example<B: hyper::body::Body + Unpin>(body: BufferedBody<B>) {
// Check if body starts with JSON array
let result = body.collect_exact(1).await;
match result {
CollectExactResult::AtLeast { ref buffered, .. } => {
if buffered.starts_with(b"[") {
// It's a JSON array, reconstruct body for further processing
let body = result.into_buffered_body();
}
}
CollectExactResult::Incomplete { buffered, error } => {
// Body was empty or error occurred
}
}
}Variants§
AtLeast
Successfully collected at least the requested number of bytes.
The buffered bytes contains at least the requested amount (possibly more if a frame was consumed). The remaining field contains either:
Some(Remaining::Body(stream))- more data to streamSome(Remaining::Error(err))- error occurred after collecting enough bytesNone- stream ended cleanly
Fields
Incomplete
Failed to collect the requested bytes.
This occurs when either:
- The body stream ended before reaching the requested number of bytes (error is None)
- An error occurred while reading the stream (error is Some)
The buffered field contains any bytes successfully read before the failure.
Implementations§
Source§impl<B: HttpBody> CollectExactResult<B>
impl<B: HttpBody> CollectExactResult<B>
Sourcepub fn into_buffered_body(self) -> BufferedBody<B>
pub fn into_buffered_body(self) -> BufferedBody<B>
Converts the result into a BufferedBody, using the buffered data as prefix.
This reconstructs the body:
AtLeast { buffered, remaining }→BufferedBody::Partialwith buffered as prefix and remaining, orBufferedBody::Completeif no remainingIncomplete { buffered, error }→BufferedBody::Partialwith error, orBufferedBody::Completeif no error
Trait Implementations§
Auto Trait Implementations§
impl<B> !Freeze for CollectExactResult<B>
impl<B> RefUnwindSafe for CollectExactResult<B>
impl<B> Send for CollectExactResult<B>
impl<B> Sync for CollectExactResult<B>
impl<B> Unpin for CollectExactResult<B>
impl<B> UnwindSafe for CollectExactResult<B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more