Struct rocket::data::Capped[][src]

pub struct Capped<T> {
    pub value: T,
    pub n: N,
}
Expand description

Encapsulates a value capped to a data limit.

A Capped<T> type represents a T that has been limited (capped) to some number of bytes. The internal N specifies whether the value is complete (also Capped::is_complete()) or whether it was capped prematurely. A Capped is returned by various methods of DataStream. Some Capped<T> types, like Capped<String> and Capped<TempFile>, implement traits like FromData and FromForm.

Example

Since Capped<TempFile> implements FromData, it can be used as a data guard. The following Rocket route accepts a raw upload and stores the upload in a different directory depending on whether the file exceeded the data limit or not. See TempFile for details on temporary file storage locations and limits.

use rocket::data::Capped;
use rocket::fs::TempFile;

#[post("/upload", data = "<file>")]
async fn upload(mut file: Capped<TempFile<'_>>) -> std::io::Result<()> {
    if file.is_complete() {
        file.persist_to("/tmp/complete/file.txt").await?;
    } else {
        file.persist_to("/tmp/incomplete/file.txt").await?;
    }

    Ok(())
}

Fields

value: T

The capped value itself.

n: N

The number of bytes written and whether value is complete.

Implementations

impl<T> Capped<T>[src]

pub fn new(value: T, n: N) -> Self[src]

Creates a new Capped from a value and an n. Prefer to use Capped::from() when possible.

Example

use rocket::data::{Capped, N};

let n = N { written: 2, complete: true };
let capped = Capped::new("hi".to_string(), n);

pub fn complete(value: T, len: usize) -> Self[src]

Creates a new Capped from a value and the length of value n, marking value as complete. Prefer to use Capped::from() when possible.

Example

use rocket::data::{Capped, N};

let string = "hi";
let capped = Capped::complete("hi", string.len());

pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Capped<U>[src]

Converts a Capped<T> to Capped<U> by applying f to the contained value.

Example

use rocket::data::{Capped, N};

let n = N { written: 2, complete: true };
let capped: Capped<usize> = Capped::new(10usize, n);
let mapped: Capped<String> = capped.map(|n| n.to_string());

pub fn is_empty(&self) -> bool[src]

Returns true if self.n.written is 0, that is, no bytes were written to value.

Example

use rocket::data::{Capped, N};

let n = N { written: 2, complete: true };
let capped = Capped::new("hi".to_string(), n);
assert!(!capped.is_empty());

let n = N { written: 0, complete: true };
let capped = Capped::new("".to_string(), n);
assert!(capped.is_empty());

pub fn is_complete(&self) -> bool[src]

Returns true if self.n.complete, that is, value represents the entire data stream.

Example

use rocket::data::{Capped, N};

let n = N { written: 2, complete: true };
let capped = Capped::new("hi".to_string(), n);
assert!(capped.is_complete());

let n = N { written: 4, complete: false };
let capped = Capped::new("hell".to_string(), n);
assert!(!capped.is_complete());

pub fn into_inner(self) -> T[src]

Returns the internal value.

Example

use rocket::data::{Capped, N};

let n = N { written: 2, complete: true };
let capped = Capped::new("hi".to_string(), n);
assert_eq!(capped.into_inner(), "hi");

Trait Implementations

impl<T: Clone> Clone for Capped<T>[src]

fn clone(&self) -> Capped<T>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for Capped<T>[src]

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

Formats the value using the given formatter. Read more

impl<T> Deref for Capped<T>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T> DerefMut for Capped<T>[src]

fn deref_mut(&mut self) -> &mut Self::Target[src]

Mutably dereferences the value.

impl<T: AsRef<[u8]>> From<T> for Capped<T>[src]

fn from(value: T) -> Self[src]

Creates a Capped<T> from a value, setting complete to true.

impl<'r> FromData<'r> for Capped<String>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'r> FromData<'r> for Capped<&'r str>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'r> FromData<'r> for Capped<&'r RawStr>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'impl0, 'r> FromData<'r> for Capped<Cow<'impl0, str>>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'r> FromData<'r> for Capped<&'r [u8]>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'r> FromData<'r> for Capped<Vec<u8>>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'impl0, 'r> FromData<'r> for Capped<TempFile<'impl0>>[src]

type Error = Error

The associated error to be returned when the guard fails.

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

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

impl<'v> FromFormField<'v> for Capped<&'v str>[src]

fn from_value(field: ValueField<'v>) -> Result<'v, Self>[src]

Parse a value of T from a form value field. Read more

fn from_data<'life0, 'async_trait>(
    f: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<'v, Self>> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Parse a value of T from a form data field. Read more

fn default() -> Option<Self>[src]

Returns a default value, if any exists, to be used during lenient parsing when the form field is missing. Read more

impl<'v> FromFormField<'v> for Capped<String>[src]

fn from_value(field: ValueField<'v>) -> Result<'v, Self>[src]

Parse a value of T from a form value field. Read more

fn from_data<'life0, 'async_trait>(
    f: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<'v, Self>> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Parse a value of T from a form data field. Read more

fn default() -> Option<Self>[src]

Returns a default value, if any exists, to be used during lenient parsing when the form field is missing. Read more

impl<'v> FromFormField<'v> for Capped<Cow<'v, str>>[src]

fn from_value(field: ValueField<'v>) -> Result<'v, Self>[src]

Parse a value of T from a form value field. Read more

fn from_data<'life0, 'async_trait>(
    field: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<'v, Self>> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Parse a value of T from a form data field. Read more

fn default() -> Option<Self>[src]

Returns a default value, if any exists, to be used during lenient parsing when the form field is missing. Read more

impl<'v> FromFormField<'v> for Capped<TempFile<'v>>[src]

fn from_value(field: ValueField<'v>) -> Result<Self, Errors<'v>>[src]

Parse a value of T from a form value field. Read more

fn from_data<'life0, 'async_trait>(
    f: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<Self, Errors<'v>>> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Parse a value of T from a form data field. Read more

fn default() -> Option<Self>[src]

Returns a default value, if any exists, to be used during lenient parsing when the form field is missing. Read more

impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for Capped<T>[src]

fn respond_to(self, request: &'r Request<'_>) -> Result<'o>[src]

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

impl<T: Copy> Copy for Capped<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Capped<T> where
    T: RefUnwindSafe

impl<T> Send for Capped<T> where
    T: Send

impl<T> Sync for Capped<T> where
    T: Sync

impl<T> Unpin for Capped<T> where
    T: Unpin

impl<T> UnwindSafe for Capped<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

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

Converts self into a collection.

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

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V