Compare commits

...

27 Commits

Author SHA1 Message Date
David Tolnay 8a09f05644 Release 0.7.10 2016-06-11 13:08:33 -07:00
David Tolnay 5923a0cd2f Merge pull request #371 from dtolnay/hasher
De/serialize for HashMap<K, V, S> and HashSet<T, S>
2016-06-11 11:51:57 -07:00
David Tolnay 1576b5a8a0 Serde_macros tests depend on fnv 2016-06-11 11:15:10 -07:00
Homu 2c4dbf5a84 Auto merge of #370 - dtolnay:expand, r=erickt
Use serde_codegen::expand in serde_tests/build.rs
2016-06-12 02:51:08 +09:00
David Tolnay 021f4f2d70 Use serde_codegen::expand in serde_tests/build.rs 2016-06-11 10:02:10 -07:00
David Tolnay decc571988 De/serialize for HashSet<T, S> 2016-06-11 10:00:33 -07:00
David Tolnay 322d7a90db Add ser tests for normal HashMap 2016-06-11 10:00:33 -07:00
David Tolnay dd3f653103 Move bounds to where-clause to increase legibility 2016-06-11 10:00:33 -07:00
David Tolnay 46a1860601 De/serialize for HashMap<K, V, S> 2016-06-11 10:00:33 -07:00
Homu 84a573c926 Auto merge of #372 - dtolnay:old, r=erickt
Stop building on 1.5.0

Syntex no longer supports this version of Rust.
2016-06-12 01:09:49 +09:00
Erick Tryzelaar 7dfa8f43f4 Merge pull request #373 from erickt/master
Updating to rustc 1.11.0-nightly (7d2f75a95 2016-06-09)
2016-06-11 09:09:22 -07:00
David Tolnay 7375b4e847 Add travis builds for 1.6.0 and 1.7.0 2016-06-11 08:59:02 -07:00
Erick Tryzelaar 48da62ed07 Updating to rustc 1.11.0-nightly (7d2f75a95 2016-06-09) 2016-06-11 08:19:51 -07:00
David Tolnay 9834af7ed9 Stop building on 1.5.0
Syntex no longer supports this version of Rust.
2016-06-11 01:31:17 -07:00
Homu 6b404d8529 Auto merge of #367 - dtolnay:default, r=oli-obk
Simplify implementation of #[serde(default=...)]
2016-06-10 17:57:39 +09:00
David Tolnay 3119cc8857 Simplify implementation of #[serde(default=...)] 2016-06-09 23:21:42 -07:00
David Tolnay bb059b97c0 Release 0.7.9 2016-06-09 20:16:49 -07:00
Homu b7188f7022 Auto merge of #362 - dtolnay:expand, r=oli-obk
Add serde_codegen::expand to avoid public Syntex dependency

Required for #358. We can remove `serde_codegen::register` in the next breaking release.

This allows Syntex users to avoid being broken by Serde bumping its Syntex dependency.
2016-06-10 07:46:33 +09:00
David Tolnay a64fe99d1b Add cargo override for building examples 2016-06-09 11:23:43 -07:00
David Tolnay c716c4e261 Use AsRef to accept paths in serde_codegen::expand 2016-06-09 11:23:43 -07:00
David Tolnay 3d2e3beafe Add serde_codegen::expand to avoid public Syntex dependency 2016-06-09 11:23:43 -07:00
Homu 1917e54a6e Auto merge of #363 - dtolnay:example, r=oli-obk
Fix nightly check in serde-syntex-example

Fixes #361.
2016-06-10 01:58:51 +09:00
David Tolnay 898b346d48 1.5 does not have a stable libcore 2016-06-09 09:17:21 -07:00
David Tolnay e90adb20ef Run serde-syntex-example in Travis 2016-06-09 01:42:00 -07:00
Homu a52e7f5554 Auto merge of #364 - dtolnay:rustup, r=oli-obk
Use rustup in serde-syntex-example instead of multirust

Multirust is deprecated.
2016-06-09 17:36:01 +09:00
David Tolnay 7afb8b52ae Use rustup in serde-syntex-example instead of multirust 2016-06-09 01:16:31 -07:00
David Tolnay cb4694387e Fix nightly check in serde-syntex-example 2016-06-09 01:08:12 -07:00
23 changed files with 225 additions and 146 deletions
+5 -2
View File
@@ -3,7 +3,8 @@ rust:
- stable - stable
- beta - beta
- nightly - nightly
- 1.5.0 - 1.7.0
- 1.8.0
addons: addons:
apt: apt:
packages: packages:
@@ -18,7 +19,7 @@ script:
- (cd serde && travis-cargo build) - (cd serde && travis-cargo build)
- (cd serde && travis-cargo test) - (cd serde && travis-cargo test)
- (cd serde && travis-cargo --only nightly test -- --features nightly-testing) - (cd serde && travis-cargo --only nightly test -- --features nightly-testing)
- (cd serde && travis-cargo --skip 1.5.0 build -- --no-default-features) - (cd serde && travis-cargo build -- --no-default-features)
- (cd serde && travis-cargo --only nightly build -- --no-default-features) - (cd serde && travis-cargo --only nightly build -- --no-default-features)
- (cd serde && travis-cargo --only nightly build -- --no-default-features --features alloc) - (cd serde && travis-cargo --only nightly build -- --no-default-features --features alloc)
- (cd serde && travis-cargo --only nightly build -- --no-default-features --features collections) - (cd serde && travis-cargo --only nightly build -- --no-default-features --features collections)
@@ -26,6 +27,8 @@ script:
- (cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing) - (cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing)
- (cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing) - (cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing)
- (cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing) - (cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing)
- (cd examples/serde-syntex-example && travis-cargo run)
- (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --features nightly --no-default-features)
- (cd serde && travis-cargo --only stable doc) - (cd serde && travis-cargo --only stable doc)
- (cd serde_codegen && travis-cargo --only stable doc) - (cd serde_codegen && travis-cargo --only stable doc)
- (cd serde_macros && travis-cargo --only nightly doc) - (cd serde_macros && travis-cargo --only nightly doc)
+2 -8
View File
@@ -142,10 +142,7 @@ pub fn main() {
let src = Path::new("src/main.rs.in"); let src = Path::new("src/main.rs.in");
let dst = Path::new(&out_dir).join("main.rs"); let dst = Path::new(&out_dir).join("main.rs");
let mut registry = syntex::Registry::new(); serde_codegen::expand(&src, &dst).unwrap();
serde_codegen::register(&mut registry);
registry.expand("", &src, &dst).unwrap();
} }
``` ```
@@ -203,10 +200,7 @@ mod inner {
let src = Path::new("src/main.rs.in"); let src = Path::new("src/main.rs.in");
let dst = Path::new(&out_dir).join("main.rs"); let dst = Path::new(&out_dir).join("main.rs");
let mut registry = syntex::Registry::new(); serde_codegen::expand(&src, &dst).unwrap();
serde_codegen::register(&mut registry);
registry.expand("", &src, &dst).unwrap();
} }
} }
+5
View File
@@ -0,0 +1,5 @@
paths = [
"../serde",
"../serde_codegen",
"../serde_macros",
]
+3 -3
View File
@@ -9,10 +9,10 @@ default = ["serde_codegen"]
nightly = ["serde_macros"] nightly = ["serde_macros"]
[build-dependencies] [build-dependencies]
serde_codegen = { version = "^0.7.8", optional = true } serde_codegen = { version = "^0.7.10", optional = true }
syntex = "^0.33.0" syntex = "^0.33.0"
[dependencies] [dependencies]
serde = "^0.7.8" serde = "^0.7.10"
serde_json = "^0.7.0" serde_json = "^0.7.0"
serde_macros = { version = "^0.7.8", optional = true } serde_macros = { version = "^0.7.10", optional = true }
+3 -3
View File
@@ -2,12 +2,12 @@ This example demonstrates how to use Serde with Syntex. On stable or nightly
with Syntex, it can be built with: with Syntex, it can be built with:
``` ```
% multirust run stable cargo run % rustup run stable cargo run
Running `target/debug/serde-syntex-example` Running `target/debug/serde-syntex-example`
{"x":1,"y":2} {"x":1,"y":2}
Point { x: 1, y: 2 } Point { x: 1, y: 2 }
% multirust run nightly cargo run % rustup run nightly cargo run
Running `target/debug/serde-syntex-example` Running `target/debug/serde-syntex-example`
{"x":1,"y":2} {"x":1,"y":2}
Point { x: 1, y: 2 } Point { x: 1, y: 2 }
@@ -16,5 +16,5 @@ Point { x: 1, y: 2 }
On nightly, it can use a plugin with: On nightly, it can use a plugin with:
``` ```
% multirust run nightly cargo run --features nightly --no-default-features % rustup run nightly cargo run --features nightly --no-default-features
``` ```
+1 -4
View File
@@ -12,10 +12,7 @@ mod inner {
let src = Path::new("src/main.rs.in"); let src = Path::new("src/main.rs.in");
let dst = Path::new(&out_dir).join("main.rs"); let dst = Path::new(&out_dir).join("main.rs");
let mut registry = syntex::Registry::new(); serde_codegen::expand(&src, &dst).unwrap();
serde_codegen::register(&mut registry);
registry.expand("", &src, &dst).unwrap();
} }
} }
+2 -2
View File
@@ -1,5 +1,5 @@
#![cfg_attr(nightly, feature(custom_derive, plugin))] #![cfg_attr(feature = "serde_macros", feature(custom_derive, plugin))]
#![cfg_attr(nightly, plugin(serde_macros))] #![cfg_attr(feature = "serde_macros", plugin(serde_macros))]
extern crate serde; extern crate serde;
extern crate serde_json; extern crate serde_json;
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde" name = "serde"
version = "0.7.8" version = "0.7.10"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework" description = "A generic serialization/deserialization framework"
+46 -54
View File
@@ -32,7 +32,7 @@ use collections::enum_set::{CLike, EnumSet};
#[cfg(all(feature = "nightly", feature = "collections"))] #[cfg(all(feature = "nightly", feature = "collections"))]
use collections::borrow::ToOwned; use collections::borrow::ToOwned;
use core::hash::Hash; use core::hash::{Hash, BuildHasher};
use core::marker::PhantomData; use core::marker::PhantomData;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::net; use std::net;
@@ -381,29 +381,30 @@ impl<T> Deserialize for PhantomData<T> where T: Deserialize {
macro_rules! seq_impl { macro_rules! seq_impl {
( (
$ty:ty, $ty:ty,
< $($constraints:ident),* >, $visitor_ty:ident < $($typaram:ident : $bound1:ident $(+ $bound2:ident)*),* >,
$visitor_name:ident,
$visitor:ident, $visitor:ident,
$ctor:expr, $ctor:expr,
$with_capacity:expr, $with_capacity:expr,
$insert:expr $insert:expr
) => { ) => {
/// A visitor that produces a sequence. /// A visitor that produces a sequence.
pub struct $visitor_name<T> { pub struct $visitor_ty<$($typaram),*> {
marker: PhantomData<T>, marker: PhantomData<$ty>,
} }
impl<T> $visitor_name<T> { impl<$($typaram),*> $visitor_ty<$($typaram),*>
where $($typaram: $bound1 $(+ $bound2)*),*
{
/// Construct a new sequence visitor. /// Construct a new sequence visitor.
pub fn new() -> Self { pub fn new() -> Self {
$visitor_name { $visitor_ty {
marker: PhantomData, marker: PhantomData,
} }
} }
} }
impl<T> Visitor for $visitor_name<T> impl<$($typaram),*> Visitor for $visitor_ty<$($typaram),*>
where T: $($constraints +)*, where $($typaram: $bound1 $(+ $bound2)*),*
{ {
type Value = $ty; type Value = $ty;
@@ -430,13 +431,13 @@ macro_rules! seq_impl {
} }
} }
impl<T> Deserialize for $ty impl<$($typaram),*> Deserialize for $ty
where T: $($constraints +)*, where $($typaram: $bound1 $(+ $bound2)*),*
{ {
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.deserialize_seq($visitor_name::new()) deserializer.deserialize_seq($visitor_ty::new())
} }
} }
} }
@@ -445,8 +446,7 @@ macro_rules! seq_impl {
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
seq_impl!( seq_impl!(
BinaryHeap<T>, BinaryHeap<T>,
<Deserialize, Ord>, BinaryHeapVisitor<T: Deserialize + Ord>,
BinaryHeapVisitor,
visitor, visitor,
BinaryHeap::new(), BinaryHeap::new(),
BinaryHeap::with_capacity(visitor.size_hint().0), BinaryHeap::with_capacity(visitor.size_hint().0),
@@ -455,8 +455,7 @@ seq_impl!(
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
seq_impl!( seq_impl!(
BTreeSet<T>, BTreeSet<T>,
<Deserialize, Eq, Ord>, BTreeSetVisitor<T: Deserialize + Eq + Ord>,
BTreeSetVisitor,
visitor, visitor,
BTreeSet::new(), BTreeSet::new(),
BTreeSet::new(), BTreeSet::new(),
@@ -465,8 +464,7 @@ seq_impl!(
#[cfg(all(feature = "nightly", feature = "collections"))] #[cfg(all(feature = "nightly", feature = "collections"))]
seq_impl!( seq_impl!(
EnumSet<T>, EnumSet<T>,
<Deserialize, CLike>, EnumSetVisitor<T: Deserialize + CLike>,
EnumSetVisitor,
visitor, visitor,
EnumSet::new(), EnumSet::new(),
EnumSet::new(), EnumSet::new(),
@@ -475,8 +473,7 @@ seq_impl!(
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
seq_impl!( seq_impl!(
LinkedList<T>, LinkedList<T>,
<Deserialize>, LinkedListVisitor<T: Deserialize>,
LinkedListVisitor,
visitor, visitor,
LinkedList::new(), LinkedList::new(),
LinkedList::new(), LinkedList::new(),
@@ -484,19 +481,18 @@ seq_impl!(
#[cfg(feature = "std")] #[cfg(feature = "std")]
seq_impl!( seq_impl!(
HashSet<T>, HashSet<T, S>,
<Deserialize, Eq, Hash>, HashSetVisitor<T: Deserialize + Eq + Hash,
HashSetVisitor, S: BuildHasher + Default>,
visitor, visitor,
HashSet::new(), HashSet::with_hasher(S::default()),
HashSet::with_capacity(visitor.size_hint().0), HashSet::with_capacity_and_hasher(visitor.size_hint().0, S::default()),
HashSet::insert); HashSet::insert);
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
seq_impl!( seq_impl!(
Vec<T>, Vec<T>,
<Deserialize>, VecVisitor<T: Deserialize>,
VecVisitor,
visitor, visitor,
Vec::new(), Vec::new(),
Vec::with_capacity(visitor.size_hint().0), Vec::with_capacity(visitor.size_hint().0),
@@ -505,8 +501,7 @@ seq_impl!(
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
seq_impl!( seq_impl!(
VecDeque<T>, VecDeque<T>,
<Deserialize>, VecDequeVisitor<T: Deserialize>,
VecDequeVisitor,
visitor, visitor,
VecDeque::new(), VecDeque::new(),
VecDeque::with_capacity(visitor.size_hint().0), VecDeque::with_capacity(visitor.size_hint().0),
@@ -726,30 +721,29 @@ tuple_impls! {
macro_rules! map_impl { macro_rules! map_impl {
( (
$ty:ty, $ty:ty,
< $($constraints:ident),* >, $visitor_ty:ident < $($typaram:ident : $bound1:ident $(+ $bound2:ident)*),* >,
$visitor_name:ident,
$visitor:ident, $visitor:ident,
$ctor:expr, $ctor:expr,
$with_capacity:expr, $with_capacity:expr
$insert:expr
) => { ) => {
/// A visitor that produces a map. /// A visitor that produces a map.
pub struct $visitor_name<K, V> { pub struct $visitor_ty<$($typaram),*> {
marker: PhantomData<$ty>, marker: PhantomData<$ty>,
} }
impl<K, V> $visitor_name<K, V> { impl<$($typaram),*> $visitor_ty<$($typaram),*>
where $($typaram: $bound1 $(+ $bound2)*),*
{
/// Construct a `MapVisitor*<T>`. /// Construct a `MapVisitor*<T>`.
pub fn new() -> Self { pub fn new() -> Self {
$visitor_name { $visitor_ty {
marker: PhantomData, marker: PhantomData,
} }
} }
} }
impl<K, V> Visitor for $visitor_name<K, V> impl<$($typaram),*> Visitor for $visitor_ty<$($typaram),*>
where K: $($constraints +)*, where $($typaram: $bound1 $(+ $bound2)*),*
V: Deserialize,
{ {
type Value = $ty; type Value = $ty;
@@ -767,7 +761,7 @@ macro_rules! map_impl {
let mut values = $with_capacity; let mut values = $with_capacity;
while let Some((key, value)) = try!($visitor.visit()) { while let Some((key, value)) = try!($visitor.visit()) {
$insert(&mut values, key, value); values.insert(key, value);
} }
try!($visitor.end()); try!($visitor.end());
@@ -776,14 +770,13 @@ macro_rules! map_impl {
} }
} }
impl<K, V> Deserialize for $ty impl<$($typaram),*> Deserialize for $ty
where K: $($constraints +)*, where $($typaram: $bound1 $(+ $bound2)*),*
V: Deserialize,
{ {
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.deserialize_map($visitor_name::new()) deserializer.deserialize_map($visitor_ty::new())
} }
} }
} }
@@ -792,22 +785,21 @@ macro_rules! map_impl {
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
map_impl!( map_impl!(
BTreeMap<K, V>, BTreeMap<K, V>,
<Deserialize, Eq, Ord>, BTreeMapVisitor<K: Deserialize + Eq + Ord,
BTreeMapVisitor, V: Deserialize>,
visitor, visitor,
BTreeMap::new(), BTreeMap::new(),
BTreeMap::new(), BTreeMap::new());
BTreeMap::insert);
#[cfg(feature = "std")] #[cfg(feature = "std")]
map_impl!( map_impl!(
HashMap<K, V>, HashMap<K, V, S>,
<Deserialize, Eq, Hash>, HashMapVisitor<K: Deserialize + Eq + Hash,
HashMapVisitor, V: Deserialize,
S: BuildHasher + Default>,
visitor, visitor,
HashMap::new(), HashMap::with_hasher(S::default()),
HashMap::with_capacity(visitor.size_hint().0), HashMap::with_capacity_and_hasher(visitor.size_hint().0, S::default()));
HashMap::insert);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
+5 -3
View File
@@ -31,7 +31,7 @@ use collections::enum_set::{CLike, EnumSet};
#[cfg(all(feature = "nightly", feature = "collections"))] #[cfg(all(feature = "nightly", feature = "collections"))]
use collections::borrow::ToOwned; use collections::borrow::ToOwned;
use core::hash::Hash; use core::hash::{Hash, BuildHasher};
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use core::iter; use core::iter;
#[cfg(feature = "std")] #[cfg(feature = "std")]
@@ -330,8 +330,9 @@ impl<T> Serialize for EnumSet<T>
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl<T> Serialize for HashSet<T> impl<T, H> Serialize for HashSet<T, H>
where T: Serialize + Eq + Hash, where T: Serialize + Eq + Hash,
H: BuildHasher,
{ {
#[inline] #[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
@@ -651,9 +652,10 @@ impl<K, V> Serialize for BTreeMap<K, V>
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl<K, V> Serialize for HashMap<K, V> impl<K, V, H> Serialize for HashMap<K, V, H>
where K: Serialize + Eq + Hash, where K: Serialize + Eq + Hash,
V: Serialize, V: Serialize,
H: BuildHasher,
{ {
#[inline] #[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
+7 -7
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_codegen" name = "serde_codegen"
version = "0.7.8" version = "0.7.10"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "Macros to auto-generate implementations for the serde framework" description = "Macros to auto-generate implementations for the serde framework"
@@ -17,13 +17,13 @@ nightly-testing = ["clippy"]
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"] with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
[build-dependencies] [build-dependencies]
quasi_codegen = { version = "^0.11.0", optional = true } quasi_codegen = { version = "^0.12.0", optional = true }
syntex = { version = "^0.33.0", optional = true } syntex = { version = "^0.33.0", optional = true }
[dependencies] [dependencies]
aster = { version = "^0.17.0", default-features = false } aster = { version = "^0.18.0", default-features = false }
clippy = { version = "^0.*", optional = true } clippy = { version = "^0.*", optional = true }
quasi = { version = "^0.11.0", default-features = false } quasi = { version = "^0.12.0", default-features = false }
quasi_macros = { version = "^0.11.0", optional = true } quasi_macros = { version = "^0.12.0", optional = true }
syntex = { version = "^0.33.0", optional = true } syntex = { version = "^0.35.0", optional = true }
syntex_syntax = { version = "^0.33.0", optional = true } syntex_syntax = { version = "^0.35.0", optional = true }
+1 -3
View File
@@ -8,13 +8,11 @@ mod inner {
pub fn main() { pub fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = env::var_os("OUT_DIR").unwrap();
let mut registry = syntex::Registry::new();
quasi_codegen::register(&mut registry);
let src = Path::new("src/lib.rs.in"); let src = Path::new("src/lib.rs.in");
let dst = Path::new(&out_dir).join("lib.rs"); let dst = Path::new(&out_dir).join("lib.rs");
registry.expand("", &src, &dst).unwrap(); quasi_codegen::expand(&src, &dst).unwrap();
} }
} }
+20 -22
View File
@@ -212,13 +212,24 @@ pub struct FieldAttrs {
skip_serializing_field: bool, skip_serializing_field: bool,
skip_deserializing_field: bool, skip_deserializing_field: bool,
skip_serializing_if: Option<ast::Path>, skip_serializing_if: Option<ast::Path>,
default_expr_if_missing: Option<P<ast::Expr>>, default: FieldDefault,
serialize_with: Option<ast::Path>, serialize_with: Option<ast::Path>,
deserialize_with: Option<ast::Path>, deserialize_with: Option<ast::Path>,
ser_bound: Option<Vec<ast::WherePredicate>>, ser_bound: Option<Vec<ast::WherePredicate>>,
de_bound: Option<Vec<ast::WherePredicate>>, de_bound: Option<Vec<ast::WherePredicate>>,
} }
/// Represents the default to use for a field when deserializing.
#[derive(Debug, PartialEq)]
pub enum FieldDefault {
/// Field must always be specified because it does not have a default.
None,
/// The default is given by `std::default::Default::default()`.
Default,
/// The default is given by this function.
Path(ast::Path),
}
impl FieldAttrs { impl FieldAttrs {
/// Extract out the `#[serde(...)]` attributes from a struct field. /// Extract out the `#[serde(...)]` attributes from a struct field.
pub fn from_field(cx: &ExtCtxt, pub fn from_field(cx: &ExtCtxt,
@@ -236,7 +247,7 @@ impl FieldAttrs {
skip_serializing_field: false, skip_serializing_field: false,
skip_deserializing_field: false, skip_deserializing_field: false,
skip_serializing_if: None, skip_serializing_if: None,
default_expr_if_missing: None, default: FieldDefault::None,
serialize_with: None, serialize_with: None,
deserialize_with: None, deserialize_with: None,
ser_bound: None, ser_bound: None,
@@ -266,17 +277,13 @@ impl FieldAttrs {
// Parse `#[serde(default)]` // Parse `#[serde(default)]`
ast::MetaItemKind::Word(ref name) if name == &"default" => { ast::MetaItemKind::Word(ref name) if name == &"default" => {
let default_expr = builder.expr().default(); field_attrs.default = FieldDefault::Default;
field_attrs.default_expr_if_missing = Some(default_expr);
} }
// Parse `#[serde(default="...")]` // Parse `#[serde(default="...")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => { ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
let wrapped_expr = wrap_default( let path = try!(parse_lit_into_path(cx, name, lit));
try!(parse_lit_into_path(cx, name, lit)), field_attrs.default = FieldDefault::Path(path);
);
field_attrs.default_expr_if_missing = Some(wrapped_expr);
} }
// Parse `#[serde(skip_serializing)]` // Parse `#[serde(skip_serializing)]`
@@ -290,9 +297,8 @@ impl FieldAttrs {
// Initialize field to Default::default() unless a different // Initialize field to Default::default() unless a different
// default is specified by `#[serde(default="...")]` // default is specified by `#[serde(default="...")]`
if field_attrs.default_expr_if_missing.is_none() { if field_attrs.default == FieldDefault::None {
let default_expr = builder.expr().default(); field_attrs.default = FieldDefault::Default;
field_attrs.default_expr_if_missing = Some(default_expr);
} }
} }
@@ -363,8 +369,8 @@ impl FieldAttrs {
self.skip_serializing_if.as_ref() self.skip_serializing_if.as_ref()
} }
pub fn default_expr_if_missing(&self) -> Option<&P<ast::Expr>> { pub fn default(&self) -> &FieldDefault {
self.default_expr_if_missing.as_ref() &self.default
} }
pub fn serialize_with(&self) -> Option<&ast::Path> { pub fn serialize_with(&self) -> Option<&ast::Path> {
@@ -584,11 +590,3 @@ fn parse_lit_into_where(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Vec<
Ok(where_clause.predicates) Ok(where_clause.predicates)
}) })
} }
/// This function wraps the expression in `#[serde(default="...")]` in a function to prevent it
/// from accessing the internal `Deserialize` state.
fn wrap_default(path: ast::Path) -> P<ast::Expr> {
AstBuilder::new().expr().call()
.build_path(path)
.build()
}
+2 -2
View File
@@ -60,13 +60,13 @@ pub fn with_bound<F>(
filter: F, filter: F,
bound: &ast::Path, bound: &ast::Path,
) -> Result<ast::Generics, Error> ) -> Result<ast::Generics, Error>
where F: Fn(&ast::StructField, &attr::FieldAttrs) -> bool, where F: Fn(&attr::FieldAttrs) -> bool,
{ {
Ok(builder.from_generics(generics.clone()) Ok(builder.from_generics(generics.clone())
.with_predicates( .with_predicates(
try!(all_fields_with_attrs(cx, item)) try!(all_fields_with_attrs(cx, item))
.iter() .iter()
.filter(|&&(ref field, ref attrs)| filter(field, attrs)) .filter(|&&(_, ref attrs)| filter(attrs))
.map(|&(ref field, _)| &field.ty) .map(|&(ref field, _)| &field.ty)
// TODO this filter can be removed later, see comment on function // TODO this filter can be removed later, see comment on function
.filter(|ty| contains_generic(ty, generics)) .filter(|ty| contains_generic(ty, generics))
+12 -18
View File
@@ -134,7 +134,7 @@ fn build_impl_generics(
// deserialized by us so we do not generate a bound. Fields with a `bound` // deserialized by us so we do not generate a bound. Fields with a `bound`
// attribute specify their own bound so we do not generate one. All other fields // attribute specify their own bound so we do not generate one. All other fields
// may need a `T: Deserialize` bound where T is the type of the field. // may need a `T: Deserialize` bound where T is the type of the field.
fn needs_deserialize_bound(_: &ast::StructField, attrs: &attr::FieldAttrs) -> bool { fn needs_deserialize_bound(attrs: &attr::FieldAttrs) -> bool {
!attrs.skip_deserializing_field() !attrs.skip_deserializing_field()
&& attrs.deserialize_with().is_none() && attrs.deserialize_with().is_none()
&& attrs.de_bound().is_none() && attrs.de_bound().is_none()
@@ -142,21 +142,8 @@ fn needs_deserialize_bound(_: &ast::StructField, attrs: &attr::FieldAttrs) -> bo
// Fields with a `default` attribute (not `default=...`), and fields with a // Fields with a `default` attribute (not `default=...`), and fields with a
// `skip_deserializing` attribute that do not also have `default=...`. // `skip_deserializing` attribute that do not also have `default=...`.
fn requires_default(field: &ast::StructField, attrs: &attr::FieldAttrs) -> bool { fn requires_default(attrs: &attr::FieldAttrs) -> bool {
for meta_items in field.attrs.iter().filter_map(attr::get_serde_meta_items) { attrs.default() == &attr::FieldDefault::Default
for meta_item in meta_items {
match meta_item.node {
ast::MetaItemKind::Word(ref name) if name == &"default" => {
return true
}
ast::MetaItemKind::NameValue(ref name, _) if name == &"default" => {
return false
}
_ => {}
}
}
}
attrs.skip_deserializing_field()
} }
fn deserialize_body( fn deserialize_body(
@@ -1234,9 +1221,16 @@ fn expr_is_missing(
cx: &ExtCtxt, cx: &ExtCtxt,
attrs: &attr::FieldAttrs, attrs: &attr::FieldAttrs,
) -> P<ast::Expr> { ) -> P<ast::Expr> {
if let Some(expr) = attrs.default_expr_if_missing() { match *attrs.default() {
return expr.clone(); attr::FieldDefault::Default => {
return quote_expr!(cx, ::std::default::Default::default());
}
attr::FieldDefault::Path(ref path) => {
return quote_expr!(cx, $path());
}
attr::FieldDefault::None => { /* below */ }
} }
let name = attrs.name().deserialize_name_expr(); let name = attrs.name().deserialize_name_expr();
match attrs.deserialize_with() { match attrs.deserialize_with() {
None => { None => {
+15
View File
@@ -22,6 +22,11 @@ extern crate syntax;
#[cfg(not(feature = "with-syntex"))] #[cfg(not(feature = "with-syntex"))]
extern crate rustc_plugin; extern crate rustc_plugin;
#[cfg(feature = "with-syntex")]
use std::io;
#[cfg(feature = "with-syntex")]
use std::path::Path;
#[cfg(not(feature = "with-syntex"))] #[cfg(not(feature = "with-syntex"))]
use syntax::feature_gate::AttributeType; use syntax::feature_gate::AttributeType;
@@ -31,6 +36,16 @@ include!(concat!(env!("OUT_DIR"), "/lib.rs"));
#[cfg(not(feature = "with-syntex"))] #[cfg(not(feature = "with-syntex"))]
include!("lib.rs.in"); include!("lib.rs.in");
#[cfg(feature = "with-syntex")]
pub fn expand<S, D>(src: S, dst: D) -> io::Result<()>
where S: AsRef<Path>,
D: AsRef<Path>,
{
let mut registry = syntex::Registry::new();
register(&mut registry);
registry.expand("", src.as_ref(), dst.as_ref())
}
#[cfg(feature = "with-syntex")] #[cfg(feature = "with-syntex")]
pub fn register(reg: &mut syntex::Registry) { pub fn register(reg: &mut syntex::Registry) {
use syntax::{ast, fold}; use syntax::{ast, fold};
+1 -1
View File
@@ -128,7 +128,7 @@ fn build_impl_generics(
// serialized by us so we do not generate a bound. Fields with a `bound` // serialized by us so we do not generate a bound. Fields with a `bound`
// attribute specify their own bound so we do not generate one. All other fields // attribute specify their own bound so we do not generate one. All other fields
// may need a `T: Serialize` bound where T is the type of the field. // may need a `T: Serialize` bound where T is the type of the field.
fn needs_serialize_bound(_: &ast::StructField, attrs: &attr::FieldAttrs) -> bool { fn needs_serialize_bound(attrs: &attr::FieldAttrs) -> bool {
!attrs.skip_serializing_field() !attrs.skip_serializing_field()
&& attrs.serialize_with().is_none() && attrs.serialize_with().is_none()
&& attrs.ser_bound().is_none() && attrs.ser_bound().is_none()
+4 -3
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_macros" name = "serde_macros"
version = "0.7.8" version = "0.7.10"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "Macros to auto-generate implementations for the serde framework" description = "Macros to auto-generate implementations for the serde framework"
@@ -18,12 +18,13 @@ nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-tes
[dependencies] [dependencies]
clippy = { version = "^0.*", optional = true } clippy = { version = "^0.*", optional = true }
serde_codegen = { version = "^0.7.8", path = "../serde_codegen", default-features = false, features = ["nightly"] } serde_codegen = { version = "^0.7.10", path = "../serde_codegen", default-features = false, features = ["nightly"] }
[dev-dependencies] [dev-dependencies]
compiletest_rs = "^0.1.1" compiletest_rs = "^0.1.1"
fnv = "1.0"
rustc-serialize = "^0.3.16" rustc-serialize = "^0.3.16"
serde = { version = "^0.7.8", path = "../serde" } serde = { version = "^0.7.10", path = "../serde" }
[[test]] [[test]]
name = "test" name = "test"
+5 -4
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_tests" name = "serde_tests"
version = "0.7.8" version = "0.7.10"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework" description = "A generic serialization/deserialization framework"
@@ -14,14 +14,15 @@ build = "build.rs"
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"] nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
[build-dependencies] [build-dependencies]
syntex = { version = "^0.33.0" } syntex = { version = "^0.35.0" }
syntex_syntax = { version = "^0.33.0" } syntex_syntax = { version = "^0.35.0" }
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] } serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
[dev-dependencies] [dev-dependencies]
fnv = "1.0"
rustc-serialize = "^0.3.16" rustc-serialize = "^0.3.16"
serde = { version = "*", path = "../serde" } serde = { version = "*", path = "../serde" }
syntex = "^0.33.0" syntex = "^0.35.0"
[dependencies] [dependencies]
clippy = { version = "^0.*", optional = true } clippy = { version = "^0.*", optional = true }
+1 -5
View File
@@ -13,10 +13,6 @@ fn main() {
] { ] {
let src = Path::new(src); let src = Path::new(src);
let dst = Path::new(&out_dir).join(dst); let dst = Path::new(&out_dir).join(dst);
serde_codegen::expand(&src, &dst).unwrap();
let mut registry = syntex::Registry::new();
serde_codegen::register(&mut registry);
registry.expand("", &src, &dst).unwrap();
} }
} }
+16
View File
@@ -62,6 +62,14 @@ macro_rules! hashset {
$(set.insert($value);)+ $(set.insert($value);)+
set set
} }
};
($hasher:ident @ $($value:expr),+) => {
{
use std::hash::BuildHasherDefault;
let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
$(set.insert($value);)+
set
}
} }
} }
@@ -75,5 +83,13 @@ macro_rules! hashmap {
$(map.insert($key, $value);)+ $(map.insert($key, $value);)+
map map
} }
};
($hasher:ident @ $($key:expr => $value:expr),+) => {
{
use std::hash::BuildHasherDefault;
let mut map = HashMap::with_hasher(BuildHasherDefault::<$hasher>::default());
$(map.insert($key, $value);)+
map
}
} }
} }
+26
View File
@@ -2,6 +2,9 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::net; use std::net;
use std::path::PathBuf; use std::path::PathBuf;
extern crate fnv;
use self::fnv::FnvHasher;
use token::{ use token::{
Error, Error,
Token, Token,
@@ -284,6 +287,18 @@ declare_tests! {
Token::TupleStructStart("Anything", Some(0)), Token::TupleStructStart("Anything", Some(0)),
Token::SeqEnd, Token::SeqEnd,
], ],
hashset![FnvHasher @ 1, 2, 3] => vec![
Token::SeqStart(Some(3)),
Token::SeqSep,
Token::I32(1),
Token::SeqSep,
Token::I32(2),
Token::SeqSep,
Token::I32(3),
Token::SeqEnd,
],
} }
test_vec { test_vec {
Vec::<isize>::new() => vec![ Vec::<isize>::new() => vec![
@@ -532,6 +547,17 @@ declare_tests! {
Token::StructStart("Anything", Some(0)), Token::StructStart("Anything", Some(0)),
Token::MapEnd, Token::MapEnd,
], ],
hashmap![FnvHasher @ 1 => 2, 3 => 4] => vec![
Token::MapStart(Some(2)),
Token::MapSep,
Token::I32(1),
Token::I32(2),
Token::MapSep,
Token::I32(3),
Token::I32(4),
Token::MapEnd,
],
} }
test_struct { test_struct {
Struct { a: 1, b: 2, c: 0 } => vec![ Struct { a: 1, b: 2, c: 0 } => vec![
+42 -1
View File
@@ -1,10 +1,13 @@
use std::collections::BTreeMap; use std::collections::{BTreeMap, HashMap, HashSet};
use std::net; use std::net;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str; use std::str;
use token::{self, Token}; use token::{self, Token};
extern crate fnv;
use self::fnv::FnvHasher;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
#[derive(Serialize)] #[derive(Serialize)]
@@ -144,6 +147,24 @@ declare_ser_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
} }
test_hashset {
HashSet::<isize>::new() => &[
Token::SeqStart(Some(0)),
Token::SeqEnd,
],
hashset![1] => &[
Token::SeqStart(Some(1)),
Token::SeqSep,
Token::I32(1),
Token::SeqEnd,
],
hashset![FnvHasher @ 1] => &[
Token::SeqStart(Some(1)),
Token::SeqSep,
Token::I32(1),
Token::SeqEnd,
],
}
test_tuple { test_tuple {
(1,) => &[ (1,) => &[
Token::TupleStart(1), Token::TupleStart(1),
@@ -204,6 +225,26 @@ declare_ser_tests! {
Token::MapEnd, Token::MapEnd,
], ],
} }
test_hashmap {
HashMap::<isize, isize>::new() => &[
Token::MapStart(Some(0)),
Token::MapEnd,
],
hashmap![1 => 2] => &[
Token::MapStart(Some(1)),
Token::MapSep,
Token::I32(1),
Token::I32(2),
Token::MapEnd,
],
hashmap![FnvHasher @ 1 => 2] => &[
Token::MapStart(Some(1)),
Token::MapSep,
Token::I32(1),
Token::I32(2),
Token::MapEnd,
],
}
test_unit_struct { test_unit_struct {
UnitStruct => &[Token::UnitStruct("UnitStruct")], UnitStruct => &[Token::UnitStruct("UnitStruct")],
} }