pub struct MsgPack<T>(pub T);
msgpack
only.Expand description
The MessagePack guard: easily consume and return MessagePack.
Sending MessagePack
To respond with serialized 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::serde::msgpack::MsgPack;
#[get("/users/<id>")]
fn user(id: usize) -> MsgPack<User> {
let user_from_id = User::from(id);
/* ... */
MsgPack(user_from_id)
}
Receiving MessagePack
MsgPack
is both a data guard and a form guard.
Data Guard
To deserialize request body data as MessagePack, add a data
route
argument with a target type of MsgPack<T>
, where T
is some type you’d
like to parse from JSON. T
must implement serde::Deserialize
.
use rocket::serde::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.
Form Guard
MsgPack<T>
, as a form guard, accepts value and data fields and parses the
data as a T
. Simple use MsgPack<T>
:
use rocket::form::{Form, FromForm};
use rocket::serde::msgpack::MsgPack;
#[derive(FromForm)]
struct User<'r> {
name: &'r str,
metadata: MsgPack<Metadata>
}
#[post("/users", data = "<form>")]
fn new_user(form: Form<User<'_>>) {
/* ... */
}
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§
Trait Implementations§
source§impl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T>
impl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T>
source§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
Self: 'async_trait,
'r: 'async_trait,
'life0: 'async_trait,
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 Self: 'async_trait, 'r: 'async_trait, 'life0: 'async_trait,
Self
from the incoming request body data. Read moresource§impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for MsgPack<T>
impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for MsgPack<T>
source§fn from_data<'life0, 'async_trait>(
f: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<Self, Errors<'v>>> + Send + 'async_trait>>where
Self: 'async_trait,
'v: 'async_trait,
'life0: 'async_trait,
fn from_data<'life0, 'async_trait>( f: DataField<'v, 'life0> ) -> Pin<Box<dyn Future<Output = Result<Self, Errors<'v>>> + Send + 'async_trait>>where Self: 'async_trait, 'v: 'async_trait, 'life0: 'async_trait,
T
from a form data field. Read moresource§fn from_value(field: ValueField<'v>) -> Result<'v, Self>
fn from_value(field: ValueField<'v>) -> Result<'v, Self>
T
from a form value field. Read moresource§impl<T: Ord> Ord for MsgPack<T>
impl<T: Ord> Ord for MsgPack<T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq> PartialEq<MsgPack<T>> for MsgPack<T>
impl<T: PartialEq> PartialEq<MsgPack<T>> for MsgPack<T>
source§impl<T: PartialOrd> PartialOrd<MsgPack<T>> for MsgPack<T>
impl<T: PartialOrd> PartialOrd<MsgPack<T>> for MsgPack<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'r, T: Serialize> Responder<'r, 'static> for MsgPack<T>
impl<'r, T: Serialize> Responder<'r, '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.
impl<T: Eq> Eq for MsgPack<T>
impl<T> StructuralEq for MsgPack<T>
impl<T> StructuralPartialEq for MsgPack<T>
Auto Trait Implementations§
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§
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FromFd for Twhere
T: From<OwnedFd>,
impl<T> FromFd for Twhere T: From<OwnedFd>,
§impl<T> FromFilelike for Twhere
T: From<OwnedFd>,
impl<T> FromFilelike for Twhere T: From<OwnedFd>,
§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self
from the given filelike object. Read more§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere Owned: IntoFilelike,
Self
from the given filelike object
converted from into_owned
. Read more§impl<T> FromSocketlike for Twhere
T: From<OwnedFd>,
impl<T> FromSocketlike for Twhere T: From<OwnedFd>,
§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self
from the given socketlike object.§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere Owned: IntoSocketlike,
Self
from the given socketlike object
converted from into_owned
.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,
self
into a collection.