Adds serde support

This commit is contained in:
Romain Vaillant 2016-05-16 14:16:59 +02:00
parent f3673a1d4c
commit cdbb9294e0
11 changed files with 175 additions and 0 deletions

View file

@ -29,11 +29,14 @@ name = "cgmath"
[features] [features]
unstable = [] unstable = []
default = ["rustc-serialize"] default = ["rustc-serialize"]
eders = ["serde", "serde_macros"]
[dependencies] [dependencies]
num-traits = "0.1" num-traits = "0.1"
rand = "0.3" rand = "0.3"
rustc-serialize = { version = "0.3", optional = true } rustc-serialize = { version = "0.3", optional = true }
serde = { version = "0.7", optional = true }
serde_macros = { version = "0.7", optional = true }
[dev-dependencies] [dev-dependencies]
glium = "0.14.0" glium = "0.14.0"

View file

@ -34,6 +34,7 @@ use num::BaseFloat;
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, PartialOrd)] #[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Rad<S> { pub s: S } pub struct Rad<S> { pub s: S }
/// An angle, in degrees. /// An angle, in degrees.
@ -42,6 +43,7 @@ pub struct Rad<S> { pub s: S }
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, PartialOrd)] #[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Deg<S> { pub s: S } pub struct Deg<S> { pub s: S }
/// Create a new angle, in radians /// Create a new angle, in radians

View file

@ -65,6 +65,7 @@ use num::BaseFloat;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Euler<A: Angle> { pub struct Euler<A: Angle> {
/// The angle to apply around the _x_ axis. Also known at the _pitch_. /// The angle to apply around the _x_ axis. Also known at the _pitch_.
pub x: A, pub x: A,

View file

@ -50,12 +50,18 @@
//! use cgmath::prelude::*; //! use cgmath::prelude::*;
//! ``` //! ```
#![cfg_attr(feature = "eders", feature(plugin, custom_derive))]
#![cfg_attr(feature = "eders", plugin(serde_macros))]
pub extern crate num_traits; pub extern crate num_traits;
extern crate rand; extern crate rand;
#[cfg(feature = "rustc-serialize")] #[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize; extern crate rustc_serialize;
#[cfg(feature = "eders")]
extern crate serde;
// Re-exports // Re-exports
pub use approx::*; pub use approx::*;

View file

@ -37,6 +37,7 @@ use vector::{Vector2, Vector3, Vector4};
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix2<S> { pub struct Matrix2<S> {
/// The first column of the matrix. /// The first column of the matrix.
pub x: Vector2<S>, pub x: Vector2<S>,
@ -50,6 +51,7 @@ pub struct Matrix2<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix3<S> { pub struct Matrix3<S> {
/// The first column of the matrix. /// The first column of the matrix.
pub x: Vector3<S>, pub x: Vector3<S>,
@ -65,6 +67,7 @@ pub struct Matrix3<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix4<S> { pub struct Matrix4<S> {
/// The first column of the matrix. /// The first column of the matrix.
pub x: Vector4<S>, pub x: Vector4<S>,

View file

@ -33,6 +33,7 @@ use vector::{Vector1, Vector2, Vector3, Vector4};
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point1<S> { pub struct Point1<S> {
pub x: S, pub x: S,
} }
@ -43,6 +44,7 @@ pub struct Point1<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point2<S> { pub struct Point2<S> {
pub x: S, pub x: S,
pub y: S, pub y: S,
@ -54,6 +56,7 @@ pub struct Point2<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point3<S> { pub struct Point3<S> {
pub x: S, pub x: S,
pub y: S, pub y: S,

View file

@ -68,6 +68,7 @@ pub fn ortho<S: BaseFloat>(left: S, right: S, bottom: S, top: S, near: S, far: S
/// A perspective projection based on a vertical field-of-view angle. /// A perspective projection based on a vertical field-of-view angle.
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct PerspectiveFov<S> { pub struct PerspectiveFov<S> {
pub fovy: Rad<S>, pub fovy: Rad<S>,
pub aspect: S, pub aspect: S,
@ -135,6 +136,7 @@ impl<S: BaseFloat> From<PerspectiveFov<S>> for Matrix4<S> {
/// A perspective projection with arbitrary left/right/bottom/top distances /// A perspective projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Perspective<S> { pub struct Perspective<S> {
pub left: S, pub left: S,
pub right: S, pub right: S,
@ -182,6 +184,7 @@ impl<S: BaseFloat> From<Perspective<S>> for Matrix4<S> {
/// An orthographic projection with arbitrary left/right/bottom/top distances /// An orthographic projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Ortho<S> { pub struct Ortho<S> {
pub left: S, pub left: S,
pub right: S, pub right: S,

View file

@ -38,6 +38,7 @@ use vector::Vector3;
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Quaternion<S> { pub struct Quaternion<S> {
/// The scalar part of the quaternion. /// The scalar part of the quaternion.
pub s: S, pub s: S,

View file

@ -139,6 +139,7 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// ``` /// ```
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Basis2<S> { pub struct Basis2<S> {
mat: Matrix2<S> mat: Matrix2<S>
} }
@ -212,6 +213,7 @@ impl<S: fmt::Debug> fmt::Debug for Basis2<S> {
/// been restricted to a subeset of those implemented on `Matrix3`. /// been restricted to a subeset of those implemented on `Matrix3`.
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Basis3<S> { pub struct Basis3<S> {
mat: Matrix3<S> mat: Matrix3<S>
} }

View file

@ -148,3 +148,150 @@ impl<S: BaseFloat, R: Rotation3<S>> From<Decomposed<Vector3<S>, R>> for Matrix4<
impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R> {} impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R> {}
impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {} impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}
#[cfg(feature = "eders")]
#[doc(hidden)]
mod eders_ser {
use structure::VectorSpace;
use super::Decomposed;
use serde::{self, Serialize};
impl<V: VectorSpace, R> Serialize for Decomposed<V, R>
where V: Serialize, V::Scalar: Serialize, R: Serialize
{
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: serde::Serializer
{
serializer.serialize_struct("Decomposed", DecomposedVisitor {
value: self,
state: 0,
})
}
}
struct DecomposedVisitor<'a, V: 'a + VectorSpace, R: 'a> {
value: &'a Decomposed<V, R>,
state: u8,
}
impl<'a, V: 'a + VectorSpace, R> serde::ser::MapVisitor for DecomposedVisitor<'a, V, R>
where V: Serialize, V::Scalar: Serialize, R: Serialize
{
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: serde::Serializer
{
match self.state {
0 => {
self.state += 1;
Ok(Some(try!(serializer.serialize_struct_elt("scale", &self.value.scale))))
},
1 => {
self.state += 1;
Ok(Some(try!(serializer.serialize_struct_elt("rot", &self.value.rot))))
},
2 => {
self.state += 1;
Ok(Some(try!(serializer.serialize_struct_elt("disp", &self.value.disp))))
},
_ => {
Ok(None)
},
}
}
}
}
#[cfg(feature = "eders")]
#[doc(hidden)]
mod eders_de {
use structure::VectorSpace;
use super::Decomposed;
use serde::{self, Deserialize};
use std::marker::PhantomData;
enum DecomposedField {
Scale,
Rot,
Disp,
}
impl Deserialize for DecomposedField {
fn deserialize<D>(deserializer: &mut D) -> Result<DecomposedField, D::Error>
where D: serde::Deserializer
{
struct DecomposedFieldVisitor;
impl serde::de::Visitor for DecomposedFieldVisitor {
type Value = DecomposedField;
fn visit_str<E>(&mut self, value: &str) -> Result<DecomposedField, E>
where E: serde::de::Error
{
match value {
"scale" => Ok(DecomposedField::Scale),
"rot" => Ok(DecomposedField::Rot),
"disp" => Ok(DecomposedField::Disp),
_ => Err(serde::de::Error::custom("expected scale, rot or disp")),
}
}
}
deserializer.deserialize(DecomposedFieldVisitor)
}
}
impl<S: VectorSpace, R> Deserialize for Decomposed<S, R>
where S: Deserialize, S::Scalar: Deserialize, R: Deserialize
{
fn deserialize<D>(deserializer: &mut D) -> Result<Decomposed<S, R>, D::Error>
where D: serde::de::Deserializer
{
const FIELDS: &'static [&'static str] = &["scale", "rot", "disp"];
deserializer.deserialize_struct("Decomposed", FIELDS, DecomposedVisitor(PhantomData))
}
}
struct DecomposedVisitor<S: VectorSpace, R>(PhantomData<(S, R)>);
impl<S: VectorSpace, R> serde::de::Visitor for DecomposedVisitor<S, R>
where S: Deserialize, S::Scalar: Deserialize, R: Deserialize
{
type Value = Decomposed<S, R>;
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Decomposed<S, R>, V::Error>
where V: serde::de::MapVisitor
{
let mut scale = None;
let mut rot = None;
let mut disp = None;
loop {
match try!(visitor.visit_key()) {
Some(DecomposedField::Scale) => { scale = Some(try!(visitor.visit_value())); },
Some(DecomposedField::Rot) => { rot = Some(try!(visitor.visit_value())); },
Some(DecomposedField::Disp) => { disp = Some(try!(visitor.visit_value())); },
_ => { break; },
}
}
let scale = match scale {
Some(scale) => scale,
None => try!(visitor.missing_field("scale")),
};
let rot = match rot {
Some(rot) => rot,
None => try!(visitor.missing_field("rot")),
};
let disp = match disp {
Some(disp) => disp,
None => try!(visitor.missing_field("disp")),
};
try!(visitor.end());
Ok(Decomposed { scale: scale, rot: rot, disp: disp })
}
}
}

View file

@ -31,6 +31,7 @@ use num::{BaseNum, BaseFloat, PartialOrd};
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector1<S> { pub struct Vector1<S> {
/// The x component of the vector. /// The x component of the vector.
pub x: S, pub x: S,
@ -42,6 +43,7 @@ pub struct Vector1<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector2<S> { pub struct Vector2<S> {
/// The x component of the vector. /// The x component of the vector.
pub x: S, pub x: S,
@ -55,6 +57,7 @@ pub struct Vector2<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector3<S> { pub struct Vector3<S> {
/// The x component of the vector. /// The x component of the vector.
pub x: S, pub x: S,
@ -70,6 +73,7 @@ pub struct Vector3<S> {
#[repr(C, packed)] #[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))] #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector4<S> { pub struct Vector4<S> {
/// The x component of the vector. /// The x component of the vector.
pub x: S, pub x: S,