Skip to main content

CollectExactResult

Enum CollectExactResult 

Source
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_bytes
  • Incomplete: 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 stream
  • Some(Remaining::Error(err)) - error occurred after collecting enough bytes
  • None - stream ended cleanly

Fields

§buffered: Bytes

The bytes successfully read from the stream (at least limit_bytes).

§remaining: Option<Remaining<B>>

The remaining stream data, if any.

§

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.

Fields

§buffered: Option<Bytes>

Bytes read before the stream ended or error occurred.

§error: Option<B::Error>

The error that occurred, if any.

Implementations§

Source§

impl<B: HttpBody> CollectExactResult<B>

Source

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::Partial with buffered as prefix and remaining, or BufferedBody::Complete if no remaining
  • Incomplete { buffered, error }BufferedBody::Partial with error, or BufferedBody::Complete if no error

Trait Implementations§

Source§

impl<B: Debug + HttpBody> Debug for CollectExactResult<B>
where B::Error: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> !Freeze for CollectExactResult<B>

§

impl<B> RefUnwindSafe for CollectExactResult<B>

§

impl<B> Send for CollectExactResult<B>
where <B as Body>::Error: Send, B: Send,

§

impl<B> Sync for CollectExactResult<B>
where <B as Body>::Error: Sync, B: Sync,

§

impl<B> Unpin for CollectExactResult<B>
where <B as Body>::Error: Unpin, B: Unpin,

§

impl<B> UnwindSafe for CollectExactResult<B>
where <B as Body>::Error: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more