From 0c08d2ffc84b2c63d2a7d3f2d9b28954e64590c6 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Thu, 7 Mar 2019 15:41:26 +0100 Subject: [PATCH] Derive Serialize and Deserialize for box types. --- src/box2d.rs | 25 +++---------------------- src/box3d.rs | 27 ++++----------------------- 2 files changed, 7 insertions(+), 45 deletions(-) diff --git a/src/box2d.rs b/src/box2d.rs index 30bf0800..ff9403de 100644 --- a/src/box2d.rs +++ b/src/box2d.rs @@ -19,7 +19,7 @@ use approxord::{min, max}; use num_traits::NumCast; #[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use core::borrow::Borrow; use core::cmp::PartialOrd; @@ -30,6 +30,8 @@ use core::ops::{Add, Div, Mul, Sub}; /// An axis aligned rectangle represented by its minimum and maximum coordinates. #[repr(C)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>")))] pub struct TypedBox2D { pub min: TypedPoint2D, pub max: TypedPoint2D, @@ -38,27 +40,6 @@ pub struct TypedBox2D { /// The default box 2d type with no unit. pub type Box2D = TypedBox2D; -#[cfg(feature = "serde")] -impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedBox2D { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (min, max) = try!(Deserialize::deserialize(deserializer)); - Ok(TypedBox2D::new(min, max)) - } -} - -#[cfg(feature = "serde")] -impl Serialize for TypedBox2D { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (&self.min, &self.max).serialize(serializer) - } -} - impl Hash for TypedBox2D { fn hash(&self, h: &mut H) { self.min.hash(h); diff --git a/src/box3d.rs b/src/box3d.rs index b3425a61..1966b281 100644 --- a/src/box3d.rs +++ b/src/box3d.rs @@ -18,7 +18,7 @@ use approxord::{min, max}; use num_traits::NumCast; #[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use core::borrow::Borrow; use core::cmp::PartialOrd; @@ -29,7 +29,9 @@ use core::ops::{Add, Div, Mul, Sub}; /// An axis aligned 3D box represented by its minimum and maximum coordinates. #[repr(C)] -pub struct TypedBox3D { +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>")))] +pub struct TypedBox3D { pub min: TypedPoint3D, pub max: TypedPoint3D, } @@ -37,27 +39,6 @@ pub struct TypedBox3D { /// The default box 3d type with no unit. pub type Box3D = TypedBox3D; -#[cfg(feature = "serde")] -impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedBox3D { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (min, max) = try!(Deserialize::deserialize(deserializer)); - Ok(TypedBox3D::new(min, max)) - } -} - -#[cfg(feature = "serde")] -impl Serialize for TypedBox3D { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (&self.min, &self.max).serialize(serializer) - } -} - impl Hash for TypedBox3D { fn hash(&self, h: &mut H) { self.min.hash(h);