Merge pull request #399 from Mange/serde-09

Bump to serde 0.9
This commit is contained in:
Brendan Zabarauskas 2017-04-14 21:51:17 +10:00 committed by GitHub
commit e02eb9811a
3 changed files with 64 additions and 40 deletions

View file

@ -6,6 +6,6 @@ rust:
- stable - stable
script: script:
- cargo build && cargo test - cargo build && cargo test && cargo test --features "eders"
- if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then cargo bench; fi - if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then cargo bench; fi
- if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then cargo build --features "use_simd" && cargo test --features "use_simd" && cargo bench --features "use_simd"; fi - if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then cargo build --features "use_simd" && cargo test --features "use_simd" && cargo bench --features "use_simd"; fi

View file

@ -37,10 +37,10 @@ approx = "0.1"
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.8", optional = true } serde = { version = "0.9", optional = true }
serde_derive = { version = "0.8", optional = true } serde_derive = { version = "0.9", optional = true }
simd = { version = "0.2", optional = true } simd = { version = "0.2", optional = true }
[dev-dependencies] [dev-dependencies]
glium = "0.15" glium = "0.15"
serde_json = "0.8" serde_json = "0.9"

View file

@ -64,11 +64,10 @@ pub struct Decomposed<V: VectorSpace, R> {
pub disp: V, pub disp: V,
} }
impl<P: EuclideanSpace, R: Rotation<P>> Transform<P> for Decomposed<P::Diff, R> where impl<P: EuclideanSpace, R: Rotation<P>> Transform<P> for Decomposed<P::Diff, R>
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 where <P as EuclideanSpace>::Scalar: BaseFloat,
<P as EuclideanSpace>::Scalar: BaseFloat,
// FIXME: Investigate why this is needed! // FIXME: Investigate why this is needed!
<P as EuclideanSpace>::Diff: VectorSpace, <P as EuclideanSpace>::Diff: VectorSpace
{ {
#[inline] #[inline]
fn one() -> Decomposed<P::Diff, R> { fn one() -> Decomposed<P::Diff, R> {
@ -150,7 +149,9 @@ 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> {}
impl<S: VectorSpace, R, E: BaseFloat> ApproxEq for Decomposed<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> where S: ApproxEq<Epsilon = E>,
S::Scalar: ApproxEq<Epsilon = E>,
R: ApproxEq<Epsilon = E>
{ {
type Epsilon = E; type Epsilon = E;
@ -190,18 +191,21 @@ mod eders_ser {
use structure::VectorSpace; use structure::VectorSpace;
use super::Decomposed; use super::Decomposed;
use serde::{self, Serialize}; use serde::{self, Serialize};
use serde::ser::SerializeStruct;
impl<V: VectorSpace, R> Serialize for Decomposed<V, R> impl<V, R> Serialize for Decomposed<V, R>
where V: Serialize, V::Scalar: Serialize, R: Serialize where V: Serialize + VectorSpace,
V::Scalar: Serialize,
R: Serialize
{ {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer where S: serde::Serializer
{ {
let mut state = try!(serializer.serialize_struct("Decomposed", 3)); let mut struc = serializer.serialize_struct("Decomposed", 3)?;
try!(serializer.serialize_struct_elt(&mut state, "scale", &self.scale)); struc.serialize_field("scale", &self.scale)?;
try!(serializer.serialize_struct_elt(&mut state, "rot", &self.rot)); struc.serialize_field("rot", &self.rot)?;
try!(serializer.serialize_struct_elt(&mut state, "disp", &self.disp)); struc.serialize_field("disp", &self.disp)?;
serializer.serialize_struct_end(state) struc.end()
} }
} }
} }
@ -213,6 +217,7 @@ mod eders_de {
use super::Decomposed; use super::Decomposed;
use serde::{self, Deserialize}; use serde::{self, Deserialize};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::fmt;
enum DecomposedField { enum DecomposedField {
Scale, Scale,
@ -221,7 +226,7 @@ mod eders_de {
} }
impl Deserialize for DecomposedField { impl Deserialize for DecomposedField {
fn deserialize<D>(deserializer: &mut D) -> Result<DecomposedField, D::Error> fn deserialize<D>(deserializer: D) -> Result<DecomposedField, D::Error>
where D: serde::Deserializer where D: serde::Deserializer
{ {
struct DecomposedFieldVisitor; struct DecomposedFieldVisitor;
@ -229,7 +234,11 @@ mod eders_de {
impl serde::de::Visitor for DecomposedFieldVisitor { impl serde::de::Visitor for DecomposedFieldVisitor {
type Value = DecomposedField; type Value = DecomposedField;
fn visit_str<E>(&mut self, value: &str) -> Result<DecomposedField, E> fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`scale`, `rot` or `disp`")
}
fn visit_str<E>(self, value: &str) -> Result<DecomposedField, E>
where E: serde::de::Error where E: serde::de::Error
{ {
match value { match value {
@ -246,9 +255,11 @@ mod eders_de {
} }
impl<S: VectorSpace, R> Deserialize for Decomposed<S, R> impl<S: VectorSpace, R> Deserialize for Decomposed<S, R>
where S: Deserialize, S::Scalar: Deserialize, R: Deserialize where S: Deserialize,
S::Scalar: Deserialize,
R: Deserialize
{ {
fn deserialize<D>(deserializer: &mut D) -> Result<Decomposed<S, R>, D::Error> fn deserialize<D>(deserializer: D) -> Result<Decomposed<S, R>, D::Error>
where D: serde::de::Deserializer where D: serde::de::Deserializer
{ {
const FIELDS: &'static [&'static str] = &["scale", "rot", "disp"]; const FIELDS: &'static [&'static str] = &["scale", "rot", "disp"];
@ -259,44 +270,57 @@ mod eders_de {
struct DecomposedVisitor<S: VectorSpace, R>(PhantomData<(S, R)>); struct DecomposedVisitor<S: VectorSpace, R>(PhantomData<(S, R)>);
impl<S: VectorSpace, R> serde::de::Visitor for DecomposedVisitor<S, R> impl<S: VectorSpace, R> serde::de::Visitor for DecomposedVisitor<S, R>
where S: Deserialize, S::Scalar: Deserialize, R: Deserialize where S: Deserialize,
S::Scalar: Deserialize,
R: Deserialize
{ {
type Value = Decomposed<S, R>; type Value = Decomposed<S, R>;
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Decomposed<S, R>, V::Error> fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`scale`, `rot` and `disp` fields")
}
fn visit_map<V>(self, mut visitor: V) -> Result<Decomposed<S, R>, V::Error>
where V: serde::de::MapVisitor where V: serde::de::MapVisitor
{ {
let mut scale = None; let mut scale = None;
let mut rot = None; let mut rot = None;
let mut disp = None; let mut disp = None;
loop { while let Some(key) = visitor.visit_key()? {
match try!(visitor.visit_key()) { match key {
Some(DecomposedField::Scale) => { scale = Some(try!(visitor.visit_value())); }, DecomposedField::Scale => {
Some(DecomposedField::Rot) => { rot = Some(try!(visitor.visit_value())); }, scale = Some(visitor.visit_value()?);
Some(DecomposedField::Disp) => { disp = Some(try!(visitor.visit_value())); }, }
_ => { break; }, DecomposedField::Rot => {
rot = Some(visitor.visit_value()?);
}
DecomposedField::Disp => {
disp = Some(visitor.visit_value()?);
}
} }
} }
let scale = match scale { let scale = match scale {
Some(scale) => scale, Some(scale) => scale,
None => try!(visitor.missing_field("scale")), None => return Err(serde::de::Error::missing_field("scale")),
}; };
let rot = match rot { let rot = match rot {
Some(rot) => rot, Some(rot) => rot,
None => try!(visitor.missing_field("rot")), None => return Err(serde::de::Error::missing_field("rot")),
}; };
let disp = match disp { let disp = match disp {
Some(disp) => disp, Some(disp) => disp,
None => try!(visitor.missing_field("disp")), None => return Err(serde::de::Error::missing_field("disp")),
}; };
try!(visitor.end()); Ok(Decomposed {
scale: scale,
Ok(Decomposed { scale: scale, rot: rot, disp: disp }) rot: rot,
disp: disp,
})
} }
} }
} }