rocket::data

Struct Data

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

Type representing the data in the body of an incoming request.

This type is the only means by which the body of a request can be retrieved. This type is not usually used directly. Instead, types that implement FromData are used via code generation by specifying the data = "<var>" route parameter as follows:

#[post("/submit", data = "<var>")]
fn submit(var: DataGuard) { /* ... */ }

Above, DataGuard can be any type that implements FromData. Note that Data itself implements FromData.

§Reading Data

Data may be read from a Data object by calling either the open() or peek() methods.

The open method consumes the Data object and returns the raw data stream. The Data object is consumed for safety reasons: consuming the object ensures that holding a Data object means that all of the data is available for reading.

The peek method returns a slice containing at most 512 bytes of buffered body data. This enables partially or fully reading from a Data object without consuming the Data object.

Implementations§

Source§

impl Data

Source

pub fn open(self) -> DataStream

Returns the raw data stream.

The stream contains all of the data in the body of the request, including that in the peek buffer. The method consumes the Data instance. This ensures that a Data type always represents all of the data in a request.

§Example
use rocket::Data;

fn handler(data: Data) {
    let stream = data.open();
}
Source

pub fn peek(&self) -> &[u8]

Retrieve the peek buffer.

The peek buffer contains at most 512 bytes of the body of the request. The actual size of the returned buffer varies by web request. The peek_complete method can be used to determine if this buffer contains all of the data in the body of the request.

§Example
use rocket::Data;

fn handler(data: Data) {
    let peek = data.peek();
}
Source

pub fn peek_complete(&self) -> bool

Returns true if the peek buffer contains all of the data in the body of the request. Returns false if it does not or if it is not known if it does.

§Example
use rocket::Data;

fn handler(data: Data) {
    if data.peek_complete() {
        println!("All of the data: {:?}", data.peek());
    }
}
Source

pub fn stream_to<W: Write>(self, writer: &mut W) -> Result<u64>

A helper method to write the body of the request to any Write type.

This method is identical to io::copy(&mut data.open(), writer).

§Example
use std::io;
use rocket::Data;

fn handler(mut data: Data) -> io::Result<String> {
    // write all of the data to stdout
    data.stream_to(&mut io::stdout())
        .map(|n| format!("Wrote {} bytes.", n))
}
Source

pub fn stream_to_file<P: AsRef<Path>>(self, path: P) -> Result<u64>

A helper method to write the body of the request to a file at the path determined by path.

This method is identical to io::copy(&mut self.open(), &mut File::create(path)?).

§Example
use std::io;
use rocket::Data;

fn handler(mut data: Data) -> io::Result<String> {
    data.stream_to_file("/static/file")
        .map(|n| format!("Wrote {} bytes to /static/file", n))
}

Trait Implementations§

Source§

impl Drop for Data

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'f> FromData<'f> for Data

The identity implementation of FromData. Always returns Success.

Source§

type Error = !

The associated error to be returned when the guard fails.
Source§

type Owned = Data

The owned type returned from FromData::transform(). Read more
Source§

type Borrowed = Data

The borrowed type consumed by FromData::from_data() when FromData::transform() returns a Transform::Borrowed. Read more
Source§

fn transform( _: &Request<'_>, data: Data, ) -> Transform<Outcome<Self::Owned, Self::Error>>

Transforms data into a value of type Self::Owned. Read more
Source§

fn from_data( _: &Request<'_>, outcome: Transformed<'f, Self>, ) -> Outcome<Self, Self::Error>

Validates, parses, and converts the incoming request body data into an instance of Self. Read more
Source§

impl<'a, S, E> IntoOutcome<S, (Status, E), Data> for Result<S, E>

Source§

type Failure = Status

The type to use when returning an Outcome::Failure.
Source§

type Forward = Data

The type to use when returning an Outcome::Forward.
Source§

fn into_outcome(self, status: Status) -> Outcome<S, E>

Converts self into an Outcome. If self represents a success, an Outcome::Success is returned. Otherwise, an Outcome::Failure is returned with failure as the inner value.
Source§

fn or_forward(self, data: Data) -> Outcome<S, E>

Converts self into an Outcome. If self represents a success, an Outcome::Success is returned. Otherwise, an Outcome::Forward is returned with forward as the inner value.

Auto Trait Implementations§

§

impl Freeze for Data

§

impl RefUnwindSafe for Data

§

impl Send for Data

§

impl Sync for Data

§

impl Unpin for Data

§

impl UnwindSafe for Data

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, I> AsResult<T, I> for T
where I: Input,

Source§

fn as_result(self) -> Result<T, ParseErr<I>>

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.

§

impl<T> IntoCollection<T> for T

§

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

Converts self into a collection.
§

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

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> Typeable for T
where T: Any,

Source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
Source§

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

Source§

fn vzip(self) -> V