Skip to main content

RequestBodyStream

Struct RequestBodyStream 

Source
pub struct RequestBodyStream { /* private fields */ }
Expand description

A streaming request body.

This provides an async interface for reading request body chunks without buffering the entire body in memory.

§Example

use fastapi_core::{Body, RequestBodyStream};

async fn handle_upload(body: Body) -> Vec<u8> {
    match body {
        Body::Stream(mut stream) => {
            let mut buffer = Vec::new();
            while let Some(chunk) = stream.next().await {
                buffer.extend_from_slice(&chunk?);
            }
            buffer
        }
        Body::Bytes(bytes) => bytes,
        Body::Empty => Vec::new(),
    }
}

Implementations§

Source§

impl RequestBodyStream

Source

pub fn new<S>(stream: S) -> Self
where S: Stream<Item = Result<Vec<u8>, RequestBodyStreamError>> + Send + Sync + 'static,

Create a new body stream from an async stream of chunks.

Source

pub fn with_expected_size<S>(stream: S, expected_size: usize) -> Self
where S: Stream<Item = Result<Vec<u8>, RequestBodyStreamError>> + Send + Sync + 'static,

Create a body stream with a known expected size.

Source

pub fn bytes_received(&self) -> usize

Returns the number of bytes received so far.

Source

pub fn expected_size(&self) -> Option<usize>

Returns the expected total size, if known.

Source

pub fn is_complete(&self) -> bool

Returns true if the stream is complete.

Source

pub async fn collect(self) -> Result<Vec<u8>, RequestBodyStreamError>

Collect all chunks into a single buffer.

This consumes the stream and buffers the entire body in memory. Use this for small bodies or when the full content is needed.

For large bodies, prefer processing chunks individually via next().

Trait Implementations§

Source§

impl Debug for RequestBodyStream

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Stream for RequestBodyStream

Source§

type Item = Result<Vec<u8>, RequestBodyStreamError>

The type of values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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<S> StreamExt for S
where S: Stream + ?Sized,

Source§

fn next(&mut self) -> Next<'_, Self>
where Self: Unpin,

Returns the next item from the stream.
Source§

fn map<T, F>(self, f: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> T,

Transforms each item using a closure.
Source§

fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
where Self: Sized, F: FnMut(Self::Item) -> Fut, Fut: Future,

Transforms each item using an async closure.
Source§

fn chain<S2>(self, other: S2) -> Chain<Self, S2>
where Self: Sized, S2: Stream<Item = Self::Item>,

Chains this stream with another stream.
Source§

fn zip<S2>(self, other: S2) -> Zip<Self, S2>
where Self: Sized, S2: Stream,

Zips this stream with another stream, yielding pairs.
Source§

fn filter<P>(self, predicate: P) -> Filter<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Yields only items that match the predicate.
Source§

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> Option<T>,

Filters and transforms items in one step.
Source§

fn take(self, n: usize) -> Take<Self>
where Self: Sized,

Takes the first n items.
Source§

fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Takes items while the predicate is true.
Source§

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Skips the first n items.
Source§

fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Skips items while the predicate is true.
Source§

fn enumerate(self) -> Enumerate<Self>
where Self: Sized,

Enumerates items with their index.
Source§

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Fuses the stream to handle None gracefully.
Source§

fn inspect<F>(self, f: F) -> Inspect<Self, F>
where Self: Sized, F: FnMut(&Self::Item),

Inspects items without modifying the stream.
Source§

fn buffered(self, n: usize) -> Buffered<Self>
where Self: Sized, Self::Item: Future,

Buffers up to n futures, preserving output order.
Source§

fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
where Self: Sized, Self::Item: Future,

Buffers up to n futures, yielding results as they complete.
Source§

fn collect<C>(self) -> Collect<Self, C>
where Self: Sized, C: Default + Extend<Self::Item>,

Collects all items into a collection.
Source§

fn chunks(self, size: usize) -> Chunks<Self>
where Self: Sized,

Collects items into fixed-size chunks.
Source§

fn ready_chunks(self, size: usize) -> ReadyChunks<Self>
where Self: Sized,

Yields immediately available items up to a maximum chunk size.
Source§

fn fold<Acc, F>(self, init: Acc, f: F) -> Fold<Self, F, Acc>
where Self: Sized, F: FnMut(Acc, Self::Item) -> Acc,

Folds all items into a single value.
Source§

fn for_each<F>(self, f: F) -> ForEach<Self, F>
where Self: Sized, F: FnMut(Self::Item),

Executes a closure for each item.
Source§

fn for_each_async<F, Fut>(self, f: F) -> ForEachAsync<Self, F, Fut>
where Self: Sized, F: FnMut(Self::Item) -> Fut, Fut: Future<Output = ()>,

Executes an async closure for each item.
Source§

fn count(self) -> Count<Self>
where Self: Sized,

Counts the number of items in the stream.
Source§

fn any<P>(self, predicate: P) -> Any<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Checks if any item matches the predicate.
Source§

fn all<P>(self, predicate: P) -> All<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Checks if all items match the predicate.
Source§

fn try_collect<T, E, C>(self) -> TryCollect<Self, C>
where Self: Sized + Stream<Item = Result<T, E>>, C: Default + Extend<T>,

Collects items from a stream of Results, short-circuiting on error.
Source§

fn try_fold<T, E, Acc, F>(self, init: Acc, f: F) -> TryFold<Self, F, Acc>
where Self: Sized + Stream<Item = Result<T, E>>, F: FnMut(Acc, T) -> Result<Acc, E>,

Folds a stream of Results, short-circuiting on error.
Source§

fn try_for_each<F, E>(self, f: F) -> TryForEach<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> Result<(), E>,

Executes a fallible closure for each item, short-circuiting on error.
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
Source§

impl<T> ResponseProduces<T> for T