Struct rocket::data::DataStream

source ·
pub struct DataStream<'r> { /* private fields */ }
Expand description

Raw data stream of a request body.

This stream can only be obtained by calling Data::open() with a data limit. The stream contains all of the data in the body of the request.

Reading from a DataStream is accomplished via the various methods on the structure. In general, methods exists in two variants: those that check whether the entire stream was read and those that don’t. The former either directly or indirectly (via Capped) return an N which allows checking if the stream was read to completion while the latter do not.

Read IntoMethodNotes
StringDataStream::into_string()Completeness checked. Preferred.
StringAsyncReadExt::read_to_string()Unchecked w/existing String.
Vec<u8>DataStream::into_bytes()Checked. Preferred.
Vec<u8>DataStream::stream_to(&mut vec)Checked w/existing Vec.
Vec<u8>DataStream::stream_precise_to()Unchecked w/existing Vec.
FileDataStream::into_file()Checked. Preferred.
FileDataStream::stream_to(&mut file)Checked w/ existing File.
FileDataStream::stream_precise_to()Unchecked w/ existing File.
TDataStream::stream_to()Checked. Any T: AsyncWrite.
TDataStream::stream_precise_to()Unchecked. Any T: AsyncWrite.

Implementations§

source§

impl<'r> DataStream<'r>

source

pub fn hint(&self) -> usize

Number of bytes a full read from self will definitely read.

Example
use rocket::data::{Data, ToByteUnit};

async fn f(data: Data<'_>) {
    let definitely_have_n_bytes = data.open(1.kibibytes()).hint();
}
source

pub async fn stream_to<W>(self, writer: W) -> Result<N>where W: AsyncWrite + Unpin,

A helper method to write the body of the request to any AsyncWrite type. Returns an N which indicates how many bytes were written and whether the entire stream was read. An additional read from self may be required to check if all of the stream has been read. If that information is not needed, use DataStream::stream_precise_to().

This method is identical to tokio::io::copy(&mut self, &mut writer) except in that it returns an N to check for completeness.

Example
use std::io;
use rocket::data::{Data, ToByteUnit};

async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
    // write all of the data to stdout
    let written = data.open(512.kibibytes())
        .stream_to(tokio::io::stdout()).await?;

    Ok(format!("Wrote {} bytes.", written))
}
source

pub async fn stream_precise_to<W>(self, writer: W) -> Result<u64>where W: AsyncWrite + Unpin,

Like DataStream::stream_to() except that no end-of-stream check is conducted and thus read/write completeness is unknown.

Example
use std::io;
use rocket::data::{Data, ToByteUnit};

async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
    // write all of the data to stdout
    let written = data.open(512.kibibytes())
        .stream_precise_to(tokio::io::stdout()).await?;

    Ok(format!("Wrote {} bytes.", written))
}
source

pub async fn into_bytes(self) -> Result<Capped<Vec<u8>>>

A helper method to write the body of the request to a Vec<u8>.

Example
use std::io;
use rocket::data::{Data, ToByteUnit};

async fn data_guard(data: Data<'_>) -> io::Result<Vec<u8>> {
    let bytes = data.open(4.kibibytes()).into_bytes().await?;
    if !bytes.is_complete() {
        println!("there are bytes remaining in the stream");
    }

    Ok(bytes.into_inner())
}
source

pub async fn into_string(self) -> Result<Capped<String>>

A helper method to write the body of the request to a String.

Example
use std::io;
use rocket::data::{Data, ToByteUnit};

async fn data_guard(data: Data<'_>) -> io::Result<String> {
    let string = data.open(10.bytes()).into_string().await?;
    if !string.is_complete() {
        println!("there are bytes remaining in the stream");
    }

    Ok(string.into_inner())
}
source

pub async fn into_file<P: AsRef<Path>>(self, path: P) -> Result<Capped<File>>

A helper method to write the body of the request to a file at the path determined by path. If a file at the path already exists, it is overwritten. The opened file is returned.

Example
use std::io;
use rocket::data::{Data, ToByteUnit};

async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
    let file = data.open(1.megabytes()).into_file("/static/file").await?;
    if !file.is_complete() {
        println!("there are bytes remaining in the stream");
    }

    Ok(format!("Wrote {} bytes to /static/file", file.n))
}

Trait Implementations§

source§

impl AsyncRead for DataStream<'_>

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_> ) -> Poll<Result<()>>

Attempts to read from the AsyncRead into buf. Read more

Auto Trait Implementations§

§

impl<'r> !RefUnwindSafe for DataStream<'r>

§

impl<'r> Send for DataStream<'r>

§

impl<'r> Sync for DataStream<'r>

§

impl<'r> Unpin for DataStream<'r>

§

impl<'r> !UnwindSafe for DataStream<'r>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>

§

impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self>

source§

impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,

source§

fn chain<R>(self, next: R) -> Chain<Self, R>where Self: Sized, R: AsyncRead,

Creates a new AsyncRead instance that chains this stream with next. Read more
source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where Self: Unpin,

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more
source§

fn read_buf<B, 'a>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>where Self: Sized + Unpin, B: BufMut,

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more
source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
source§

fn read_u8<'a>(&'a mut self) -> ReadU8<&'a mut Self>where Self: Unpin,

Reads an unsigned 8 bit integer from the underlying reader. Read more
source§

fn read_i8<'a>(&'a mut self) -> ReadI8<&'a mut Self>where Self: Unpin,

Reads a signed 8 bit integer from the underlying reader. Read more
source§

fn read_u16<'a>(&'a mut self) -> ReadU16<&'a mut Self>where Self: Unpin,

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_i16<'a>(&'a mut self) -> ReadI16<&'a mut Self>where Self: Unpin,

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_u32<'a>(&'a mut self) -> ReadU32<&'a mut Self>where Self: Unpin,

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_i32<'a>(&'a mut self) -> ReadI32<&'a mut Self>where Self: Unpin,

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_u64<'a>(&'a mut self) -> ReadU64<&'a mut Self>where Self: Unpin,

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_i64<'a>(&'a mut self) -> ReadI64<&'a mut Self>where Self: Unpin,

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_u128<'a>(&'a mut self) -> ReadU128<&'a mut Self>where Self: Unpin,

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_i128<'a>(&'a mut self) -> ReadI128<&'a mut Self>where Self: Unpin,

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more
source§

fn read_f32<'a>(&'a mut self) -> ReadF32<&'a mut Self>where Self: Unpin,

Reads an 32-bit floating point type in big-endian order from the underlying reader. Read more
source§

fn read_f64<'a>(&'a mut self) -> ReadF64<&'a mut Self>where Self: Unpin,

Reads an 64-bit floating point type in big-endian order from the underlying reader. Read more
source§

fn read_u16_le<'a>(&'a mut self) -> ReadU16Le<&'a mut Self>where Self: Unpin,

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_i16_le<'a>(&'a mut self) -> ReadI16Le<&'a mut Self>where Self: Unpin,

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_u32_le<'a>(&'a mut self) -> ReadU32Le<&'a mut Self>where Self: Unpin,

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_i32_le<'a>(&'a mut self) -> ReadI32Le<&'a mut Self>where Self: Unpin,

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_u64_le<'a>(&'a mut self) -> ReadU64Le<&'a mut Self>where Self: Unpin,

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_i64_le<'a>(&'a mut self) -> ReadI64Le<&'a mut Self>where Self: Unpin,

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_u128_le<'a>(&'a mut self) -> ReadU128Le<&'a mut Self>where Self: Unpin,

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_i128_le<'a>(&'a mut self) -> ReadI128Le<&'a mut Self>where Self: Unpin,

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more
source§

fn read_f32_le<'a>(&'a mut self) -> ReadF32Le<&'a mut Self>where Self: Unpin,

Reads an 32-bit floating point type in little-endian order from the underlying reader. Read more
source§

fn read_f64_le<'a>(&'a mut self) -> ReadF64Le<&'a mut Self>where Self: Unpin,

Reads an 64-bit floating point type in little-endian order from the underlying reader. Read more
source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEnd<'a, Self>where Self: Unpin,

Reads all bytes until EOF in this source, placing them into buf. Read more
source§

fn read_to_string<'a>( &'a mut self, dst: &'a mut String ) -> ReadToString<'a, Self>where Self: Unpin,

Reads all bytes until EOF in this source, appending them to buf. Read more
source§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adaptor which reads at most limit bytes from it. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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 Twhere U: From<T>,

const: unstable · 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> IntoCollection<T> for T

source§

fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,

Converts self into a collection.
source§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

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