From 825f5f29753c43695b6e9194437d3a401d090924 Mon Sep 17 00:00:00 2001 From: bachm Date: Sat, 23 Aug 2014 14:34:33 +0200 Subject: [PATCH] deriving Encodable, Decodable for all structs --- src/aabb.rs | 4 ++-- src/angle.rs | 6 ++++-- src/cgmath.rs | 2 ++ src/cylinder.rs | 2 +- src/frustum.rs | 4 ++-- src/line.rs | 2 +- src/matrix.rs | 6 +++--- src/obb.rs | 4 ++-- src/plane.rs | 2 +- src/point.rs | 4 ++-- src/projection.rs | 6 +++--- src/quaternion.rs | 2 +- src/ray.rs | 2 +- src/rotation.rs | 4 ++-- src/sphere.rs | 2 +- src/transform.rs | 2 ++ src/vector.rs | 2 +- 17 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/aabb.rs b/src/aabb.rs index 88fe0c6..5fd5000 100644 --- a/src/aabb.rs +++ b/src/aabb.rs @@ -84,7 +84,7 @@ pub trait Aabb, P: Point> { } /// A two-dimensional AABB, aka a rectangle. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Aabb2 { pub min: Point2, pub max: Point2, @@ -129,7 +129,7 @@ impl fmt::Show for Aabb2 { } /// A three-dimensional AABB, aka a rectangular prism. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Aabb3 { pub min: Point3, pub max: Point3, diff --git a/src/angle.rs b/src/angle.rs index c300d82..468224e 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -22,9 +22,11 @@ use approx::ApproxEq; use num::BaseFloat; /// An angle, in radians -#[deriving(Clone, PartialEq, PartialOrd, Hash)] pub struct Rad { pub s: S } +#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable)] +pub struct Rad { pub s: S } /// An angle, in degrees -#[deriving(Clone, PartialEq, PartialOrd, Hash)] pub struct Deg { pub s: S } +#[deriving(Clone, PartialEq, PartialOrd, Hash, Encodable, Decodable)] +pub struct Deg { pub s: S } /// Create a new angle, in radians #[inline] pub fn rad(s: S) -> Rad { Rad { s: s } } diff --git a/src/cgmath.rs b/src/cgmath.rs index b02bca2..23b4ae4 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -35,6 +35,8 @@ //! `look_at`, `from_angle`, `from_euler`, and `from_axis_angle` methods. //! These are provided for convenience. +extern crate serialize; + // Re-exports pub use array::{Array1, Array2, FixedArray}; diff --git a/src/cylinder.rs b/src/cylinder.rs index a22f218..be8a835 100644 --- a/src/cylinder.rs +++ b/src/cylinder.rs @@ -18,7 +18,7 @@ use point::Point3; use vector::Vector3; -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Cylinder { pub center: Point3, pub axis: Vector3, diff --git a/src/frustum.rs b/src/frustum.rs index 6adbd1f..648bcc9 100644 --- a/src/frustum.rs +++ b/src/frustum.rs @@ -22,7 +22,7 @@ use plane::Plane; use point::Point3; use vector::{Vector, EuclideanVector}; -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Frustum { pub left: Plane, pub right: Plane, @@ -59,7 +59,7 @@ Frustum { } } -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct FrustumPoints { pub near_top_left: Point3, pub near_top_right: Point3, diff --git a/src/line.rs b/src/line.rs index 3b397d7..d686078 100644 --- a/src/line.rs +++ b/src/line.rs @@ -24,7 +24,7 @@ use ray::{Ray2}; use intersect::Intersect; /// A generic directed line segment from `origin` to `dest`. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Line

{ pub origin: P, pub dest: P, diff --git a/src/matrix.rs b/src/matrix.rs index 5f62fcb..d035a79 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -29,15 +29,15 @@ use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; /// A 2 x 2, column major matrix -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Matrix2 { pub x: Vector2, pub y: Vector2 } /// A 3 x 3, column major matrix -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 } /// A 4 x 4, column major matrix -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } diff --git a/src/obb.rs b/src/obb.rs index 9466359..c1a1c21 100644 --- a/src/obb.rs +++ b/src/obb.rs @@ -18,14 +18,14 @@ use point::{Point2, Point3}; use vector::{Vector2, Vector3}; -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Obb2 { pub center: Point2, pub axis: Vector2, pub extents: Vector2, } -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Obb3 { pub center: Point3, pub axis: Vector3, diff --git a/src/plane.rs b/src/plane.rs index 63ff2d0..c155fb5 100644 --- a/src/plane.rs +++ b/src/plane.rs @@ -40,7 +40,7 @@ use vector::{Vector, EuclideanVector}; /// The `A*x + B*y + C*z - D = 0` form is preferred over the other common /// alternative, `A*x + B*y + C*z + D = 0`, because it tends to avoid /// superfluous negations (see _Real Time Collision Detection_, p. 55). -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Plane { pub n: Vector3, pub d: S, diff --git a/src/point.rs b/src/point.rs index cb7390c..2f01e11 100644 --- a/src/point.rs +++ b/src/point.rs @@ -27,11 +27,11 @@ use num::{BaseNum, BaseFloat}; use vector::*; /// A point in 2-dimensional space. -#[deriving(PartialEq, Clone, Hash)] +#[deriving(PartialEq, Clone, Hash, Encodable, Decodable)] pub struct Point2 { pub x: S, pub y: S } /// A point in 3-dimensional space. -#[deriving(PartialEq, Clone, Hash)] +#[deriving(PartialEq, Clone, Hash, Encodable, Decodable)] pub struct Point3 { pub x: S, pub y: S, pub z: S } diff --git a/src/projection.rs b/src/projection.rs index 9ed41c0..56e4331 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -69,7 +69,7 @@ pub trait Projection: ToMatrix4 { } /// A perspective projection based on a vertical field-of-view angle. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct PerspectiveFov { pub fovy: A, pub aspect: S, @@ -143,7 +143,7 @@ impl> ToMatrix4 for PerspectiveFov { } /// A perspective projection with arbitrary left/right/bottom/top distances -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Perspective { pub left: S, right: S, pub bottom: S, top: S, @@ -193,7 +193,7 @@ impl ToMatrix4 for Perspective { } /// An orthographic projection with arbitrary left/right/bottom/top distances -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Ortho { pub left: S, right: S, pub bottom: S, top: S, diff --git a/src/quaternion.rs b/src/quaternion.rs index 0cb4134..08e55a5 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -28,7 +28,7 @@ use vector::{Vector3, Vector, EuclideanVector}; /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Quaternion { pub s: S, pub v: Vector3 } /// Represents types which can be expressed as a quaternion. diff --git a/src/ray.rs b/src/ray.rs index 7d88b9c..0bb0c47 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -19,7 +19,7 @@ use vector::{Vector, Vector2, Vector3}; /// A generic ray starting at `origin` and extending infinitely in /// `direction`. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Ray { pub origin: P, pub direction: V, diff --git a/src/rotation.rs b/src/rotation.rs index 3d58b5c..7587443 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -160,7 +160,7 @@ pub trait Rotation3: Rotation, Point3> /// let unit_y3 = rot_half.concat(&rot_half).rotate_vector(&unit_x); /// assert!(unit_y3.approx_eq(&unit_y2)); /// ``` -#[deriving(PartialEq, Clone)] +#[deriving(PartialEq, Clone, Encodable, Decodable)] pub struct Basis2 { mat: Matrix2 } @@ -238,7 +238,7 @@ impl Rotation2 for Basis2 { /// inversion, can be implemented more efficiently than the implementations for /// `math::Matrix3`. To ensure orthogonality is maintained, the operations have /// been restricted to a subeset of those implemented on `Matrix3`. -#[deriving(PartialEq, Clone)] +#[deriving(PartialEq, Clone, Encodable, Decodable)] pub struct Basis3 { mat: Matrix3 } diff --git a/src/sphere.rs b/src/sphere.rs index 4960456..4f61ce0 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -23,7 +23,7 @@ use vector::Vector; use std::num::zero; -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Encodable, Decodable)] pub struct Sphere { pub center: Point3, pub radius: S, diff --git a/src/transform.rs b/src/transform.rs index 123f8d4..629784f 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -76,6 +76,7 @@ pub trait Transform, P: Point> { /// A generic transformation consisting of a rotation, /// displacement vector and scale amount. +#[deriving(Encodable, Decodable)] pub struct Decomposed { pub scale: S, pub rot: R, @@ -158,6 +159,7 @@ impl> fmt::Show for Decomposed { pub mat: Matrix4, } diff --git a/src/vector.rs b/src/vector.rs index 80e0d5e..9922860 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -183,7 +183,7 @@ pub trait Vector: Array1 // Utility macro for generating associated functions for the vectors macro_rules! vec( ($Self:ident <$S:ident> { $($field:ident),+ }, $n:expr) => ( - #[deriving(PartialEq, Eq, Clone, Hash)] + #[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable)] pub struct $Self { $(pub $field: S),+ } impl<$S> $Self<$S> {