Struct rocket_contrib::msgpack::MsgPack [−][src]
The MsgPack
data guard and responder: easily consume and respond with
MessagePack.
Receiving MessagePack
MsgPack
is both a data guard and a form guard.
Data Guard
To parse 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_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.
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_contrib::msgpack::MsgPack; #[derive(FromForm)] struct User<'r> { name: &'r str, metadata: MsgPack<Metadata> } #[post("/users", data = "<form>")] fn new_user(form: Form<User<'_>>) { /* ... */ }
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
Implementations
impl<T> MsgPack<T>
[src]
pub fn into_inner(self) -> T
[src]
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> Deref for MsgPack<T>
[src]
impl<T> DerefMut for MsgPack<T>
[src]
impl<T> From<T> for MsgPack<T>
[src]
impl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T>
[src]
type Error = Error
The associated error to be returned when the guard fails.
fn from_data<'life0, 'async_trait>(
req: &'r Request<'life0>,
data: Data
) -> Pin<Box<dyn Future<Output = Outcome<Self, Self::Error>> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
[src]
req: &'r Request<'life0>,
data: Data
) -> Pin<Box<dyn Future<Output = Outcome<Self, Self::Error>> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
impl<'v, T: DeserializeOwned + Send> FromFormField<'v> for MsgPack<T>
[src]
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,
[src]
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,
pub fn from_value(field: ValueField<'v>) -> Result<Self, Errors<'v>>
[src]
pub fn default() -> Option<Self>
[src]
impl<'r, T: Serialize> Responder<'r, 'static> for MsgPack<T>
[src]
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, req: &'r Request<'_>) -> Result<'static>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for MsgPack<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for MsgPack<T> where
T: Send,
T: Send,
impl<T> Sync for MsgPack<T> where
T: Sync,
T: Sync,
impl<T> Unpin for MsgPack<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for MsgPack<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<'v, T> FromForm<'v> for T where
T: FromFormField<'v>,
[src]
T: FromFormField<'v>,
type Context = FromFieldContext<'v, T>
The form guard’s parsing context.
pub fn init(opts: Options) -> <T as FromForm<'v>>::Context
[src]
pub fn push_value(
ctxt: &mut <T as FromForm<'v>>::Context,
field: ValueField<'v>
)
[src]
ctxt: &mut <T as FromForm<'v>>::Context,
field: ValueField<'v>
)
pub fn push_data<'life0, 'life1, 'async_trait>(
ctxt: &'life0 mut FromFieldContext<'v, T>,
field: DataField<'v, 'life1>
) -> Pin<Box<dyn Future<Output = ()> + 'async_trait + Send, Global>> where
'v: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
T: 'async_trait,
[src]
ctxt: &'life0 mut FromFieldContext<'v, T>,
field: DataField<'v, 'life1>
) -> Pin<Box<dyn Future<Output = ()> + 'async_trait + Send, Global>> where
'v: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
T: 'async_trait,
pub fn finalize(ctxt: <T as FromForm<'v>>::Context) -> Result<T, Errors<'v>>
[src]
pub fn push_error(_ctxt: &mut Self::Context, _error: Error<'r>)
[src]
pub fn default() -> Option<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoCollection<T> for T
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
A: Array<Item = T>,
pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<T> IntoSql for T
pub fn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
Self: AsExpression<T>,
pub fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
&'a Self: AsExpression<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,