Struct rocket::response::Flash[][src]

pub struct Flash<R> { /* fields omitted */ }
Expand description

Sets a “flash” cookie that will be removed when it is accessed. The analogous request type is FlashMessage.

This type makes it easy to send messages across requests. It is typically used for “status” messages after redirects. For instance, if a user attempts to visit a page he/she does not have access to, you may want to redirect the user to a safe place and show a message indicating what happened on the redirected page. The message should only persist for a single request. This can be accomplished with this type.

Usage

Each Flash message consists of a kind and message. A generic constructor (new) can be used to construct a message of any kind, while the warning, success, and error constructors create messages with the corresponding kinds.

Messages can be retrieved on the request side via the FlashMessage type and the kind and message methods.

Response

The Responder implementation for Flash sets the message cookie and then uses the passed in responder res to complete the response. In other words, it simply sets a cookie and delegates the rest of the response handling to the wrapped responder.

Example

The following routes illustrate the use of a Flash message on both the request and response sides.

use rocket::response::{Flash, Redirect};
use rocket::request::FlashMessage;

#[post("/login/<name>")]
fn login(name: &str) -> Result<&'static str, Flash<Redirect>> {
    if name == "special_user" {
        Ok("Hello, special user!")
    } else {
        Err(Flash::error(Redirect::to(uri!(index)), "Invalid username."))
    }
}

#[get("/")]
fn index(flash: Option<FlashMessage<'_>>) -> String {
    flash.map(|flash| format!("{}: {}", flash.kind(), flash.message()))
         .unwrap_or_else(|| "Welcome!".to_string())
}

On the response side (in login), a Flash error message is set if some fictional authentication failed, and the user is redirected to "/". On the request side (in index), the handler emits the flash message if there is one and otherwise emits a standard welcome message. Note that if the user were to refresh the index page after viewing a flash message, the user would receive the standard welcome message.

Implementations

impl<R> Flash<R>[src]

pub fn new<K: Into<String>, M: Into<String>>(
    res: R,
    kind: K,
    message: M
) -> Flash<R>
[src]

Constructs a new Flash message with the given kind, message, and underlying responder.

Examples

Construct a “suggestion” message with contents “Try this out!” that redirects to “/”.

use rocket::response::{Redirect, Flash};

let message = Flash::new(Redirect::to("/"), "suggestion", "Try this out!");

pub fn success<S: Into<String>>(responder: R, message: S) -> Flash<R>[src]

Constructs a “success” Flash message with the given responder and message.

Examples

Construct a “success” message with contents “It worked!” that redirects to “/”.

use rocket::response::{Redirect, Flash};

let message = Flash::success(Redirect::to("/"), "It worked!");

pub fn warning<S: Into<String>>(responder: R, message: S) -> Flash<R>[src]

Constructs a “warning” Flash message with the given responder and message.

Examples

Construct a “warning” message with contents “Watch out!” that redirects to “/”.

use rocket::response::{Redirect, Flash};

let message = Flash::warning(Redirect::to("/"), "Watch out!");

pub fn error<S: Into<String>>(responder: R, message: S) -> Flash<R>[src]

Constructs an “error” Flash message with the given responder and message.

Examples

Construct an “error” message with contents “Whoops!” that redirects to “/”.

use rocket::response::{Redirect, Flash};

let message = Flash::error(Redirect::to("/"), "Whoops!");

impl<'r> Flash<&'a CookieJar<'a>>[src]

pub fn into_inner(self) -> (String, String)[src]

Returns a tuple of (kind, message), consuming self.

pub fn kind(&self) -> &str[src]

Returns the kind of this message.

pub fn message(&self) -> &str[src]

Returns the message contents of this message.

Trait Implementations

impl<R: Debug> Debug for Flash<R>[src]

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

Formats the value using the given formatter. Read more

impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Flash<R>[src]

Sets the message cookie and then uses the wrapped responder to complete the response. In other words, simply sets a cookie and delegates the rest of the response handling to the wrapped responder. As a result, the Outcome of the response is the Outcome of the wrapped Responder.

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

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

Auto Trait Implementations

impl<R> RefUnwindSafe for Flash<R> where
    R: RefUnwindSafe

impl<R> Send for Flash<R> where
    R: Send

impl<R> Sync for Flash<R> where
    R: Sync

impl<R> Unpin for Flash<R> where
    R: Unpin

impl<R> UnwindSafe for Flash<R> where
    R: 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, 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