Struct rocket_contrib::MsgPack [−][src]
pub struct MsgPack<T>(pub T);
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
or DeserializeOwned
from
Serde. The data is parsed from the HTTP
request body.
#[post("/users/", format = "application/msgpack", data = "<user>")] fn new_user(user: MsgPack<User>) { ... }
You don't need to use format = "application/msgpack"
, but it may be
what you want. Using format = application/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. By default, Rocket will
accept a Content Type of any of the following for MessagePack data:
application/msgpack
, application/x-msgpack
, bin/msgpack
, or
bin/x-msgpack
.
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.
#[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
Methods
impl<T> MsgPack<T>
[src]
impl<T> MsgPack<T>
pub fn into_inner(self) -> T
[src]
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
impl<T: Debug> Debug for MsgPack<T>
[src]
impl<T: Debug> Debug for MsgPack<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T: DeserializeOwned> FromData for MsgPack<T>
[src]
impl<T: DeserializeOwned> FromData for MsgPack<T>
type Error = MsgPackError
The associated error to be returned when the guard fails.
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error>
[src]
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error>
Validates, parses, and converts an instance of Self
from the incoming request body data. Read more
impl<T: Serialize> Responder<'static> for MsgPack<T>
[src]
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.
fn respond_to(self, _: &Request) -> Result<'static>
[src]
fn respond_to(self, _: &Request) -> Result<'static>
Returns Ok
if a Response
could be generated successfully. Otherwise, returns an Err
with a failing Status
. Read more
impl<T> Deref for MsgPack<T>
[src]
impl<T> Deref for MsgPack<T>
type Target = T
The resulting type after dereferencing.
ⓘImportant traits for &'a mut Rfn deref<'a>(&'a self) -> &'a T
[src]
fn deref<'a>(&'a self) -> &'a T
Dereferences the value.
impl<T> DerefMut for MsgPack<T>
[src]
impl<T> DerefMut for MsgPack<T>