Struct rocket::data::Data

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

Type representing the body data of a 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, data guards (types that implement FromData) are created indirectly 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<'r> Data<'r>

source

pub fn open(self, limit: ByteUnit) -> DataStream<'r>

Returns the raw data stream, limited to limit bytes.

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::{Data, ToByteUnit};

fn handler(data: Data<'_>) {
    let stream = data.open(2.mebibytes());
}
source

pub async fn peek(&mut self, num: usize) -> &[u8]

Retrieve at most num bytes from the peek buffer without consuming self.

The peek buffer contains at most 512 bytes of the body of the request. The actual size of the returned buffer is the min of the request’s body, num and 512. The peek_complete method can be used to determine if this buffer contains all of the data in the body of the request.

Examples

In a data guard:

use rocket::request::{self, Request, FromRequest};
use rocket::data::{Data, FromData, Outcome};

#[rocket::async_trait]
impl<'r> FromData<'r> for MyType {
    type Error = MyError;

    async fn from_data(r: &'r Request<'_>, mut data: Data<'r>) -> Outcome<'r, Self> {
        if data.peek(2).await != b"hi" {
            return Outcome::Forward(data)
        }

        /* .. */
    }
}

In a fairing:

use rocket::{Rocket, Request, Data, Response};
use rocket::fairing::{Fairing, Info, Kind};

#[rocket::async_trait]
impl Fairing for MyType {
    fn info(&self) -> Info {
        Info {
            name: "Data Peeker",
            kind: Kind::Request
        }
    }

    async fn on_request(&self, req: &mut Request<'_>, data: &mut Data<'_>) {
        if data.peek(2).await == b"hi" {
            /* do something; body data starts with `"hi"` */
        }

        /* .. */
    }
}
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::Data;

async fn handler(mut data: Data<'_>) {
    if data.peek_complete() {
        println!("All of the data: {:?}", data.peek(512).await);
    }
}

Trait Implementations§

source§

impl<'r> FromData<'r> for Data<'r>

§

type Error = Infallible

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

fn from_data<'life0, 'async_trait>( _: &'r Request<'life0>, data: Data<'r> ) -> Pin<Box<dyn Future<Output = Outcome<'r, Self>> + Send + 'async_trait>>where Self: 'async_trait, 'r: 'async_trait, 'life0: 'async_trait,

Asynchronously validates, parses, and converts an instance of Self from the incoming request body data. Read more
source§

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

§

type Failure = Status

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

type Forward = Data<'r>

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

fn into_outcome(self, status: Status) -> Outcome<'r, 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<'r>) -> Outcome<'r, 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<'r> !RefUnwindSafe for Data<'r>

§

impl<'r> Send for Data<'r>

§

impl<'r> Sync for Data<'r>

§

impl<'r> Unpin for Data<'r>

§

impl<'r> !UnwindSafe for Data<'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<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