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
sourceimpl<T> MsgPack<T>
impl<T> MsgPack<T>
sourcepub fn into_inner(self) -> T
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
sourceimpl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T>
impl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T>
sourcefn 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,
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,
Asynchronously validates, parses, and converts an instance of Self
from the incoming request body data. Read more
sourceimpl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for MsgPack<T>
impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for MsgPack<T>
sourcefn 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,
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,
Parse a value of T
from a form data field. Read more
sourcefn from_value(field: ValueField<'v>) -> Result<'v, Self>
fn from_value(field: ValueField<'v>) -> Result<'v, Self>
Parse a value of T
from a form value field. Read more
sourceimpl<T: Ord> Ord for MsgPack<T>
impl<T: Ord> Ord for MsgPack<T>
sourceimpl<T: PartialOrd> PartialOrd<MsgPack<T>> for MsgPack<T>
impl<T: PartialOrd> PartialOrd<MsgPack<T>> for MsgPack<T>
sourcefn partial_cmp(&self, other: &MsgPack<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &MsgPack<T>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<'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.
sourcefn respond_to(self, req: &'r Request<'_>) -> Result<'static>
fn respond_to(self, req: &'r Request<'_>) -> Result<'static>
Returns Ok
if a Response
could be generated successfully. Otherwise,
returns an Err
with a failing Status
. Read more
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 T where
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for T where
T: 'a,
fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>
impl<'a, T> AsTaggedImplicit<'a> for T where
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for T where
T: 'a,
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn 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>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more