From ae796b41ad6ae70fcbb0225723bb6851f50b8f31 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 29 Jun 2021 03:14:57 -0700 Subject: [PATCH] Impl std traits, 'UriDisplay' on 'MsgPack'. This brings the 'MsgPack' impls to parity with 'Json'. --- core/lib/src/serde/msgpack.rs | 53 ++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/core/lib/src/serde/msgpack.rs b/core/lib/src/serde/msgpack.rs index 0e593f83..57047969 100644 --- a/core/lib/src/serde/msgpack.rs +++ b/core/lib/src/serde/msgpack.rs @@ -32,6 +32,7 @@ use crate::data::{Limits, Data, FromData, Outcome}; use crate::response::{self, Responder, content}; use crate::http::Status; use crate::form::prelude as form; +use crate::http::uri::fmt; use serde::{Serialize, Deserialize}; @@ -40,15 +41,34 @@ pub use rmp_serde::decode::Error; /// The MessagePack guard: easily consume and return MessagePack. /// +/// ## Sending MessagePack +/// +/// To respond with serialized MessagePack data, return a `MsgPack` type, +/// where `T` implements [`Serialize`] from [`serde`]. The content type of the +/// response is set to `application/msgpack` automatically. +/// +/// ```rust +/// # #[macro_use] extern crate rocket; +/// # type User = usize; +/// use rocket::serde::msgpack::MsgPack; +/// +/// #[get("/users/")] +/// fn user(id: usize) -> MsgPack { +/// 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 parse request body data as MessagePack , add a `data` route argument with -/// a target type of `MsgPack`, where `T` is some type you'd like to parse -/// from JSON. `T` must implement [`serde::Deserialize`]. +/// To deserialize request body data as MessagePack, add a `data` route +/// argument with a target type of `MsgPack`, where `T` is some type you'd +/// like to parse from JSON. `T` must implement [`serde::Deserialize`]. /// /// ```rust /// # #[macro_use] extern crate rocket; @@ -102,26 +122,7 @@ pub use rmp_serde::decode::Error; /// [global.limits] /// msgpack = 5242880 /// ``` -/// -/// ## Sending MessagePack -/// -/// If you're responding with MessagePack data, return a `MsgPack` type, -/// where `T` implements [`Serialize`] from [`serde`]. The content type of the -/// response is set to `application/msgpack` automatically. -/// -/// ```rust -/// # #[macro_use] extern crate rocket; -/// # type User = usize; -/// use rocket::serde::msgpack::MsgPack; -/// -/// #[get("/users/")] -/// fn user(id: usize) -> MsgPack { -/// let user_from_id = User::from(id); -/// /* ... */ -/// MsgPack(user_from_id) -/// } -/// ``` -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct MsgPack(pub T); impl MsgPack { @@ -210,6 +211,12 @@ impl<'v, T: Deserialize<'v> + Send> form::FromFormField<'v> for MsgPack { } } +impl> fmt::UriDisplay for MsgPack { + fn fmt(&self, f: &mut fmt::Formatter<'_, fmt::Query>) -> std::fmt::Result { + self.0.fmt(f) + } +} + impl From for MsgPack { fn from(value: T) -> Self { MsgPack(value)