rocket_contrib::msgpack

Struct MsgPack

Source
pub struct MsgPack<T>(pub T);
Expand description

The MsgPack type: implements FromData and Responder, allowing you to easily consume and respond with MessagePack data.

§Receiving MessagePack

If you’re receiving MessagePack data, simply add a data parameter to your route arguments and ensure the type of the parameter is a MsgPack<T>, where T is some type you’d like to parse from MessagePack. T must implement Deserialize from serde. The data is parsed from the HTTP request body.

use rocket_contrib::msgpack::MsgPack;

#[post("/users", format = "msgpack", data = "<user>")]
fn new_user(user: MsgPack<User>) {
    /* ... */
}

You don’t need to use format = "msgpack", but it may be what you want. Using format = msgpack means that any request that doesn’t specify “application/msgpack” as its first Content-Type: header parameter will not be routed to this handler.

§Sending MessagePack

If you’re responding with MessagePack data, return a MsgPack<T> type, where T implements Serialize from serde. The content type of the response is set to application/msgpack automatically.

use rocket_contrib::msgpack::MsgPack;

#[get("/users/<id>")]
fn user(id: usize) -> MsgPack<User> {
    let user_from_id = User::from(id);
    /* ... */
    MsgPack(user_from_id)
}

§Incoming Data Limits

The default size limit for incoming MessagePack data is 1MiB. Setting a limit protects your application from denial of service (DOS) attacks and from resource exhaustion through high memory consumption. The limit can be increased by setting the limits.msgpack configuration parameter. For instance, to increase the MessagePack limit to 5MiB for all environments, you may add the following to your Rocket.toml:

[global.limits]
msgpack = 5242880

Tuple Fields§

§0: T

Implementations§

Source§

impl<T> MsgPack<T>

Source

pub fn into_inner(self) -> T

Consumes the MsgPack wrapper and returns the wrapped item.

§Example
let string = "Hello".to_string();
let my_msgpack = MsgPack(string);
assert_eq!(my_msgpack.into_inner(), "Hello".to_string());

Trait Implementations§

Source§

impl<T: Debug> Debug for MsgPack<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for MsgPack<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for MsgPack<T>

Source§

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

Mutably dereferences the value.
Source§

impl<'a, T: Deserialize<'a>> FromData<'a> for MsgPack<T>

Source§

type Error = Error

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

type Owned = Vec<u8>

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

type Borrowed = [u8]

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

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

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

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

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

impl<T: Serialize> Responder<'static> for MsgPack<T>

Serializes the wrapped value into MessagePack. Returns a response with Content-Type MsgPack and a fixed-size body with the serialization. If serialization fails, an Err of Status::InternalServerError is returned.

Source§

fn respond_to(self, req: &Request<'_>) -> Result<'static>

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

Auto Trait Implementations§

§

impl<T> Freeze for MsgPack<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MsgPack<T>
where T: RefUnwindSafe,

§

impl<T> Send for MsgPack<T>
where T: Send,

§

impl<T> Sync for MsgPack<T>
where T: Sync,

§

impl<T> Unpin for MsgPack<T>
where T: Unpin,

§

impl<T> UnwindSafe for MsgPack<T>
where T: UnwindSafe,

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> IntoSql for T

Source§

fn into_sql<T>(self) -> Self::Expression
where Self: Sized + AsExpression<T>,

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

Source§

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

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