Skip to main content

Multipart

Struct Multipart 

Source
pub struct Multipart<S>
where S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,
{ /* private fields */ }
Expand description

A streaming parser for multipart/form-data.

The parser owns the underlying stream and yields one Part at a time. At most one Part is active at any moment; the borrow checker enforces this property at compile time.

The stream has to be Send + Unpin, but not Sync: the parser only ever polls it through &mut, so it never needs shared access, and requiring Sync would turn away streams that can be moved between threads but not shared between them.

Implementations§

Source§

impl<S> Multipart<S>
where S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,

Source

pub fn new(stream: S, boundary: &Boundary, max_buffer_size: usize) -> Self

Constructs a parser.

max_buffer_size bounds the part header block: a block that ends within the limit is accepted, and one that grows past it without a terminator is rejected with Error::HeaderSizeExceeded. The limit is a property of the body, not of the delivery — the same bytes parse the same way however the transport splits them — so a chunk larger than max_buffer_size carrying a block that ends within the limit is accepted.

Data chunks yielded by stream while part data is being streamed are passed through without being retained, so they are not charged to this limit. The buffer can still hold one arriving chunk beyond the limit: the bytes that follow the header terminator are retained until the header block is consumed, so the limit bounds the block, not the peak allocation.

Source

pub async fn next_part(&mut self) -> Result<Option<Part<'_, S>>, Error>

Returns the next part, or None after the closing delimiter.

Dropping a Part without entering its data phase (through Part::next_data or Part::take_data_stream) skips the rest of that part: unread headers are discarded and the part’s data is skipped lazily, so a caller can leave a part behind and keep going.

§Errors

Returns an error for malformed input, stream errors, or when the multipart stream has already been taken by a part data stream.

Source

pub fn poll_next_part( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<Option<Part<'_, S>>, Error>>

Poll-based core of the state machine, driven by Multipart::next_part.

Auto Trait Implementations§

§

impl<S> Freeze for Multipart<S>
where Option<StreamBuffer<S>>: Freeze,

§

impl<S> RefUnwindSafe for Multipart<S>
where Option<StreamBuffer<S>>: RefUnwindSafe,

§

impl<S> Send for Multipart<S>
where Option<StreamBuffer<S>>: Send,

§

impl<S> Sync for Multipart<S>
where Option<StreamBuffer<S>>: Sync,

§

impl<S> Unpin for Multipart<S>
where Option<StreamBuffer<S>>: Unpin,

§

impl<S> UnsafeUnpin for Multipart<S>
where Option<StreamBuffer<S>>: UnsafeUnpin,

§

impl<S> UnwindSafe for Multipart<S>
where Option<StreamBuffer<S>>: 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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.