Struct rocket_contrib::msgpack::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§
Trait Implementations§
source§impl<'a, T: Deserialize<'a>> FromData<'a> for MsgPack<T>
impl<'a, T: Deserialize<'a>> FromData<'a> for MsgPack<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
&self
to an expression for Diesel’s query builder. Read more