Merge pull request #360 from Liotitch/master

Adds nightly serde support
This commit is contained in:
Brendan Zabarauskas 2016-05-17 00:00:16 +10:00
commit 85ee08a592
13 changed files with 265 additions and 36 deletions

View file

@ -7,7 +7,8 @@ rust:
env:
global:
secure: afYeHpC8BJeyajd3vd84xRBYJKxQorKfh4lGvesu/hqh0bHc6lxrqGt0Pthb2m2YDODZiGSbx9yr9UetelZIJUSA3LoiG5PdDrtH/Kwdn5xGcBWSZl3v/sH+oB3AeR7Eji6mj0mrydQxn8PQbEACvR6lpGCYE2R48KrftYmD6Js=
- TRAVIS_CARGO_NIGHTLY_FEATURE="eders"
- secure: afYeHpC8BJeyajd3vd84xRBYJKxQorKfh4lGvesu/hqh0bHc6lxrqGt0Pthb2m2YDODZiGSbx9yr9UetelZIJUSA3LoiG5PdDrtH/Kwdn5xGcBWSZl3v/sH+oB3AeR7Eji6mj0mrydQxn8PQbEACvR6lpGCYE2R48KrftYmD6Js=
before_script:
- |

View file

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

View file

@ -32,14 +32,18 @@ use num::BaseFloat;
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Rad<S> { pub s: S }
/// An angle, in degrees.
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Deg<S> { pub s: S }
/// Create a new angle, in radians

View file

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

View file

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

View file

@ -35,7 +35,9 @@ use vector::{Vector2, Vector3, Vector4};
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix2<S> {
/// The first column of the matrix.
pub x: Vector2<S>,
@ -47,7 +49,9 @@ pub struct Matrix2<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix3<S> {
/// The first column of the matrix.
pub x: Vector3<S>,
@ -61,7 +65,9 @@ pub struct Matrix3<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Matrix4<S> {
/// The first column of the matrix.
pub x: Vector4<S>,

View file

@ -31,7 +31,9 @@ use vector::{Vector1, Vector2, Vector3, Vector4};
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point1<S> {
pub x: S,
}
@ -40,7 +42,9 @@ pub struct Point1<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point2<S> {
pub x: S,
pub y: S,
@ -50,7 +54,9 @@ pub struct Point2<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Point3<S> {
pub x: S,
pub y: S,

View file

@ -66,7 +66,9 @@ 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.
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct PerspectiveFov<S> {
pub fovy: Rad<S>,
pub aspect: S,
@ -132,7 +134,9 @@ impl<S: BaseFloat> From<PerspectiveFov<S>> for Matrix4<S> {
}
/// A perspective projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Perspective<S> {
pub left: S,
pub right: S,
@ -178,7 +182,9 @@ impl<S: BaseFloat> From<Perspective<S>> for Matrix4<S> {
}
/// An orthographic projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Ortho<S> {
pub left: S,
pub right: S,

View file

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

View file

@ -137,7 +137,9 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// let unit_y3 = (rot_half * rot_half).rotate_vector(unit_x);
/// assert!(unit_y3.approx_eq(&unit_y2));
/// ```
#[derive(PartialEq, Copy, Clone, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Copy, Clone)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Basis2<S> {
mat: Matrix2<S>
}
@ -209,7 +211,9 @@ impl<S: fmt::Debug> fmt::Debug for Basis2<S> {
/// 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`.
#[derive(PartialEq, Copy, Clone, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Copy, Clone)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Basis3<S> {
mat: Matrix3<S>
}

View file

@ -56,7 +56,8 @@ pub trait Transform<P: EuclideanSpace>: Sized {
/// A generic transformation consisting of a rotation,
/// displacement vector and scale amount.
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
pub struct Decomposed<V: VectorSpace, R> {
pub scale: V::Scalar,
pub rot: R,
@ -147,3 +148,162 @@ 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: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}
impl<S: VectorSpace, R, E: BaseFloat> ApproxEq for Decomposed<S, R>
where S: ApproxEq<Epsilon = E>, S::Scalar: ApproxEq<Epsilon = E>, R: ApproxEq<Epsilon = E>
{
type Epsilon = E;
fn approx_eq_eps(&self, other: &Self, epsilon: &Self::Epsilon) -> bool {
self.scale.approx_eq_eps(&other.scale, epsilon) &&
self.rot.approx_eq_eps(&other.rot, epsilon) &&
self.disp.approx_eq_eps(&other.disp, epsilon)
}
}
#[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

@ -29,7 +29,9 @@ use num::{BaseNum, BaseFloat, PartialOrd};
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector1<S> {
/// The x component of the vector.
pub x: S,
@ -39,7 +41,9 @@ pub struct Vector1<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector2<S> {
/// The x component of the vector.
pub x: S,
@ -51,7 +55,9 @@ pub struct Vector2<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector3<S> {
/// The x component of the vector.
pub x: S,
@ -65,7 +71,9 @@ pub struct Vector3<S> {
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Vector4<S> {
/// The x component of the vector.
pub x: S,

View file

@ -16,6 +16,9 @@
extern crate cgmath;
#[cfg(feature = "eders")]
extern crate serde_json;
use cgmath::*;
#[test]
@ -23,8 +26,8 @@ fn test_invert() {
let v = Vector3::new(1.0f64, 2.0, 3.0);
let t = Decomposed {
scale: 1.5f64,
rot: Quaternion::new(0.5f64,0.5,0.5,0.5),
disp: Vector3::new(6.0f64,-7.0,8.0)
rot: Quaternion::new(0.5f64, 0.5, 0.5, 0.5),
disp: Vector3::new(6.0f64, -7.0, 8.0),
};
let ti = t.inverse_transform().expect("Expected successful inversion");
let vt = t.transform_vector(v);
@ -41,3 +44,18 @@ fn test_look_at() {
let view_point = Point3::new(0.0f64, 1.0, 5.0);
assert!(t.transform_point(point).approx_eq(&view_point));
}
#[cfg(feature = "eders")]
#[test]
fn test_serialize() {
let t = Decomposed {
scale: 1.5f64,
rot: Quaternion::new(0.5f64, 0.5, 0.5, 0.5),
disp: Vector3::new(6.0f64, -7.0, 8.0),
};
let serialized = serde_json::to_string(&t).unwrap();
let deserialized: Decomposed<Vector3<f64>, Quaternion<f64>> = serde_json::from_str(&serialized).unwrap();
assert!(t.approx_eq(&deserialized));
}