mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 12:58:01 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c2359f9c3 | |||
| f59ec44a0b | |||
| b7446db511 | |||
| 8bd7acc9fc | |||
| d120539310 | |||
| 82098f4e49 | |||
| 1c55f58093 | |||
| df3c3cb555 | |||
| c539563687 | |||
| 69de46f9e0 | |||
| d5102a7afd | |||
| c4b5a42615 | |||
| d0502b93ef | |||
| b289edd4a4 | |||
| 3a687e5369 | |||
| 8c30ec9698 | |||
| e40b9e9814 | |||
| 22d0bdae8a | |||
| 84fa3fba58 | |||
| 85001608e0 | |||
| 09fe2dbba5 | |||
| b6ed82dd7d | |||
| 5bbeedadf2 | |||
| d786de6696 | |||
| 5d24d6cfb4 | |||
| c8c22c036f | |||
| b7f30d7f82 | |||
| 4bf5a15d7e | |||
| f394f25956 | |||
| 9d96f95ddd |
@@ -1,7 +0,0 @@
|
||||
paths = [
|
||||
"serde",
|
||||
"serde_codegen",
|
||||
"serde_codegen_internals",
|
||||
"serde_macros",
|
||||
"serde_test",
|
||||
]
|
||||
+3
-3
@@ -25,11 +25,11 @@ script:
|
||||
- (cd testing && travis-cargo --skip nightly test)
|
||||
- (cd testing && travis-cargo --only nightly test -- --features unstable-testing)
|
||||
- (cd serde_macros && travis-cargo --only nightly test -- --features unstable-testing)
|
||||
#- (cd examples/serde-syntex-example && travis-cargo --skip nightly run)
|
||||
#- (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --no-default-features --features unstable)
|
||||
- (cd examples/serde-syntex-example && travis-cargo --skip nightly run)
|
||||
- (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --no-default-features --features unstable)
|
||||
- (cd serde && travis-cargo --only stable doc)
|
||||
after_success:
|
||||
- (cd serde && travis-cargo --only stable doc-upload)
|
||||
- (cd serde && travis-cargo --only stable doc-upload --branch docs)
|
||||
- (cd testing && travis-cargo --only stable coveralls --no-sudo)
|
||||
env:
|
||||
global:
|
||||
|
||||
@@ -29,7 +29,7 @@ version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = "*"
|
||||
serde_json = "0.8"
|
||||
```
|
||||
|
||||
Next, the `src/main.rs` file itself:
|
||||
@@ -68,8 +68,8 @@ fn main() {
|
||||
.insert("array", ArrayBuilder::new()
|
||||
.push(1)
|
||||
.push(2)
|
||||
.unwrap())
|
||||
.unwrap();
|
||||
.build())
|
||||
.build();
|
||||
|
||||
let serialized = serde_json::to_string(&value).unwrap();
|
||||
println!("serialized value: {:?}", serialized);
|
||||
@@ -115,11 +115,11 @@ authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = "*"
|
||||
serde_codegen = "0.8"
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
serde = "0.8"
|
||||
serde_json = "0.8"
|
||||
```
|
||||
|
||||
Next, we define our source file, `src/main.rs.in`. Note this is a different
|
||||
@@ -199,9 +199,9 @@ version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
serde_macros = "*"
|
||||
serde = "0.8"
|
||||
serde_json = "0.8"
|
||||
serde_macros = "0.8"
|
||||
```
|
||||
|
||||
Note that it doesn't need a build script. Now the `src/main.rs`, which enables
|
||||
@@ -256,12 +256,12 @@ default = ["serde_codegen"]
|
||||
nightly = ["serde_macros"]
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = { version = "*", optional = true }
|
||||
serde_codegen = { version = "0.8", optional = true }
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
serde_macros = { version = "*", optional = true }
|
||||
serde = "0.8"
|
||||
serde_json = "0.8"
|
||||
serde_macros = { version = "0.8", optional = true }
|
||||
```
|
||||
|
||||
`build.rs`:
|
||||
@@ -356,11 +356,8 @@ impl serde::Serialize for i32 {
|
||||
```
|
||||
|
||||
As you can see it's pretty simple. More complex types like `BTreeMap` need to
|
||||
pass a
|
||||
[MapVisitor](http://serde-rs.github.io/serde/serde/serde/ser/trait.MapVisitor.html)
|
||||
to the
|
||||
[Serializer](http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html)
|
||||
in order to walk through the type:
|
||||
use a multi-step process (init, elements, end) in order to walk through the
|
||||
type:
|
||||
|
||||
```rust,ignore
|
||||
impl<K, V> Serialize for BTreeMap<K, V>
|
||||
@@ -371,55 +368,17 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapIteratorVisitor<Iter> {
|
||||
iter: Iter,
|
||||
len: Option<usize>,
|
||||
}
|
||||
|
||||
impl<K, V, Iter> MapIteratorVisitor<Iter>
|
||||
where Iter: Iterator<Item=(K, V)>
|
||||
{
|
||||
#[inline]
|
||||
pub fn new(iter: Iter, len: Option<usize>) -> MapIteratorVisitor<Iter> {
|
||||
MapIteratorVisitor {
|
||||
iter: iter,
|
||||
len: len,
|
||||
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
||||
for (k, v) in self {
|
||||
try!(serializer.serialize_map_elt(&mut state, k, v));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
I: Iterator<Item=(K, V)>,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
let value = try!(serializer.serialize_map_elt(key, value));
|
||||
Ok(Some(value))
|
||||
}
|
||||
None => Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
self.len
|
||||
serializer.serialize_map_end(state)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Serializing structs follow this same pattern. In fact, structs are represented
|
||||
as a named map. Its visitor uses a simple state machine to iterate through all
|
||||
the fields:
|
||||
as a named map, with a known length.
|
||||
|
||||
```rust
|
||||
extern crate serde;
|
||||
@@ -434,35 +393,10 @@ impl serde::Serialize for Point {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: serde::Serializer
|
||||
{
|
||||
serializer.serialize_struct("Point", PointMapVisitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct PointMapVisitor<'a> {
|
||||
value: &'a Point,
|
||||
state: u8,
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::MapVisitor for PointMapVisitor<'a> {
|
||||
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("x", &self.value.x))))
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
Ok(Some(try!(serializer.serialize_struct_elt("y", &self.value.y))))
|
||||
}
|
||||
_ => {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
let mut state = try!(serializer.serialize_struct("Point", 2));
|
||||
try!(serializer.serialize_struct_elt(&mut state, "x", &self.x));
|
||||
try!(serializer.serialize_struct_elt(&mut state, "y", &self.y));
|
||||
serializer.serialize_struct_end(state)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -812,12 +746,6 @@ features, and use `default-features = false` in your `Cargo.toml`:
|
||||
If you only use `default-features = false`, you will receive a stock `no_std`
|
||||
serde with no support for any of the collection types.
|
||||
|
||||
Upgrading from Serde 0.6
|
||||
========================
|
||||
|
||||
* `#[serde(skip_serializing_if_none)]` was replaced with `#[serde(skip_serializing_if="Option::is_none")]`.
|
||||
* `#[serde(skip_serializing_if_empty)]` was replaced with `#[serde(skip_serializing_if="Vec::is_empty")]`.
|
||||
|
||||
Serialization Formats Using Serde
|
||||
=================================
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ default = ["serde_codegen"]
|
||||
unstable = ["serde_macros"]
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = { version = "^0.7", optional = true }
|
||||
serde_codegen = { version = "^0.8", optional = true }
|
||||
|
||||
[dependencies]
|
||||
serde = "^0.7"
|
||||
serde_json = "^0.7"
|
||||
serde_macros = { version = "^0.7", optional = true }
|
||||
serde = "^0.8"
|
||||
serde_json = "^0.8"
|
||||
serde_macros = { version = "^0.8", optional = true }
|
||||
|
||||
@@ -16,5 +16,5 @@ Point { x: 1, y: 2 }
|
||||
On nightly, it can use a plugin with:
|
||||
|
||||
```
|
||||
% rustup run nightly cargo run --features nightly --no-default-features
|
||||
% rustup run nightly cargo run --features unstable --no-default-features
|
||||
```
|
||||
|
||||
@@ -53,6 +53,9 @@ use alloc::arc::Arc;
|
||||
#[cfg(all(feature = "unstable", feature = "alloc", not(feature = "std")))]
|
||||
use alloc::boxed::Box;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
use core::nonzero::{NonZero, Zeroable};
|
||||
|
||||
@@ -982,6 +985,135 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deser
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// This is a cleaned-up version of the impl generated by:
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(deny_unknown_fields)]
|
||||
// struct Duration {
|
||||
// secs: u64,
|
||||
// nanos: u32,
|
||||
// }
|
||||
#[cfg(feature = "std")]
|
||||
impl Deserialize for Duration {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
enum Field { Secs, Nanos };
|
||||
|
||||
impl Deserialize for Field {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl Visitor for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E>
|
||||
where E: Error,
|
||||
{
|
||||
match value {
|
||||
0usize => Ok(Field::Secs),
|
||||
1usize => Ok(Field::Nanos),
|
||||
_ => Err(Error::invalid_value("expected a field")),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E>
|
||||
where E: Error,
|
||||
{
|
||||
match value {
|
||||
"secs" => Ok(Field::Secs),
|
||||
"nanos" => Ok(Field::Nanos),
|
||||
_ => Err(Error::unknown_field(value)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(&mut self, value: &[u8]) -> Result<Field, E>
|
||||
where E: Error,
|
||||
{
|
||||
match value {
|
||||
b"secs" => Ok(Field::Secs),
|
||||
b"nanos" => Ok(Field::Nanos),
|
||||
_ => {
|
||||
let value = String::from_utf8_lossy(value);
|
||||
Err(Error::unknown_field(&value))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_struct_field(FieldVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct DurationVisitor;
|
||||
|
||||
impl Visitor for DurationVisitor {
|
||||
type Value = Duration;
|
||||
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Duration, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
let secs: u64 = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
try!(visitor.end());
|
||||
return Err(Error::invalid_length(0));
|
||||
}
|
||||
};
|
||||
let nanos: u32 = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
try!(visitor.end());
|
||||
return Err(Error::invalid_length(1));
|
||||
}
|
||||
};
|
||||
try!(visitor.end());
|
||||
Ok(Duration::new(secs, nanos))
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Duration, V::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
let mut secs: Option<u64> = None;
|
||||
let mut nanos: Option<u32> = None;
|
||||
while let Some(key) = try!(visitor.visit_key::<Field>()) {
|
||||
match key {
|
||||
Field::Secs => {
|
||||
if secs.is_some() {
|
||||
return Err(<V::Error as Error>::duplicate_field("secs"));
|
||||
}
|
||||
secs = Some(try!(visitor.visit_value()));
|
||||
}
|
||||
Field::Nanos => {
|
||||
if nanos.is_some() {
|
||||
return Err(<V::Error as Error>::duplicate_field("nanos"));
|
||||
}
|
||||
nanos = Some(try!(visitor.visit_value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
try!(visitor.end());
|
||||
let secs = match secs {
|
||||
Some(secs) => secs,
|
||||
None => try!(visitor.missing_field("secs")),
|
||||
};
|
||||
let nanos = match nanos {
|
||||
Some(nanos) => nanos,
|
||||
None => try!(visitor.missing_field("nanos")),
|
||||
};
|
||||
Ok(Duration::new(secs, nanos))
|
||||
}
|
||||
}
|
||||
|
||||
const FIELDS: &'static [&'static str] = &["secs", "nanos"];
|
||||
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
|
||||
|
||||
+18
-2
@@ -50,6 +50,8 @@ use std::path;
|
||||
use std::rc::Rc;
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
use alloc::rc::Rc;
|
||||
#[cfg(feature = "std")]
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::Arc;
|
||||
@@ -157,7 +159,7 @@ impl<T> Serialize for [T]
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
for e in self {
|
||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
||||
}
|
||||
serializer.serialize_seq_end(state)
|
||||
@@ -174,7 +176,7 @@ macro_rules! array_impls {
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut state = try!(serializer.serialize_seq_fixed_size($len));
|
||||
for e in self.iter() {
|
||||
for e in self {
|
||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
||||
}
|
||||
serializer.serialize_seq_end(state)
|
||||
@@ -624,6 +626,20 @@ impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for Duration {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
||||
try!(serializer.serialize_struct_elt(&mut state, "secs", self.as_secs()));
|
||||
try!(serializer.serialize_struct_elt(&mut state, "nanos", self.subsec_nanos()));
|
||||
serializer.serialize_struct_end(state)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(all(feature = "std", feature = "unstable"))]
|
||||
impl Serialize for net::IpAddr {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_codegen"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -32,6 +32,6 @@ aster = { version = "^0.22.0", default-features = false }
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
quasi = { version = "^0.16.0", default-features = false }
|
||||
quasi_macros = { version = "^0.16.0", optional = true }
|
||||
serde_codegen_internals = { version = "0.5.0", default-features = false }
|
||||
serde_codegen_internals = { version = "=0.5.0", default-features = false, path = "../serde_codegen_internals" }
|
||||
syntex = { version = "^0.39.0", optional = true }
|
||||
syntex_syntax = { version = "^0.39.0", optional = true }
|
||||
|
||||
@@ -4,14 +4,17 @@ mod inner {
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::thread::spawn;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
// put everything into a thread, so users can use `RUST_MIN_STACK` to increase the amount of stack
|
||||
spawn(|| {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
let src = Path::new("src/lib.rs.in");
|
||||
let dst = Path::new(&out_dir).join("lib.rs");
|
||||
|
||||
quasi_codegen::expand(&src, &dst).unwrap();
|
||||
let src = Path::new("src/lib.rs.in");
|
||||
let dst = Path::new(&out_dir).join("lib.rs");
|
||||
quasi_codegen::expand(&src, &dst).unwrap();
|
||||
}).join().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+75
-127
@@ -76,10 +76,8 @@ fn deserialize_item(
|
||||
#[automatically_derived]
|
||||
impl $impl_generics _serde::de::Deserialize for $ty $where_clause {
|
||||
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
||||
where __D: _serde::de::Deserializer,
|
||||
{
|
||||
$body
|
||||
}
|
||||
where __D: _serde::de::Deserializer
|
||||
$body
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
@@ -136,7 +134,7 @@ fn deserialize_body(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
match item.body {
|
||||
Body::Enum(ref variants) => {
|
||||
deserialize_item_enum(
|
||||
@@ -192,31 +190,24 @@ fn deserialize_body(
|
||||
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
||||
fn deserialize_visitor(
|
||||
builder: &aster::AstBuilder,
|
||||
trait_generics: &ast::Generics,
|
||||
forward_ty_params: Vec<ast::TyParam>,
|
||||
forward_tys: Vec<P<ast::Ty>>
|
||||
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics) {
|
||||
if trait_generics.ty_params.is_empty() && forward_tys.is_empty() {
|
||||
generics: &ast::Generics,
|
||||
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>) {
|
||||
if generics.ty_params.is_empty() {
|
||||
(
|
||||
builder.item().tuple_struct("__Visitor").build(),
|
||||
builder.item().unit_struct("__Visitor"),
|
||||
builder.ty().id("__Visitor"),
|
||||
builder.expr().id("__Visitor"),
|
||||
trait_generics.clone(),
|
||||
)
|
||||
} else {
|
||||
let placeholders : Vec<_> = trait_generics.ty_params.iter()
|
||||
let placeholders : Vec<_> = generics.ty_params.iter()
|
||||
.map(|t| builder.ty().id(t.ident))
|
||||
.collect();
|
||||
let mut trait_generics = trait_generics.clone();
|
||||
let mut ty_params = forward_ty_params.clone();
|
||||
ty_params.extend(trait_generics.ty_params.into_vec());
|
||||
trait_generics.ty_params = P::from_vec(ty_params);
|
||||
|
||||
(
|
||||
builder.item().tuple_struct("__Visitor")
|
||||
.generics().with(trait_generics.clone()).build()
|
||||
.generics().with(generics.clone()).build()
|
||||
.with_tys({
|
||||
let lifetimes = trait_generics.lifetimes.iter()
|
||||
let lifetimes = generics.lifetimes.iter()
|
||||
.map(|lifetime_def| {
|
||||
builder.ty()
|
||||
.phantom_data()
|
||||
@@ -225,7 +216,7 @@ fn deserialize_visitor(
|
||||
.unit()
|
||||
});
|
||||
|
||||
let ty_params = trait_generics.ty_params.iter()
|
||||
let ty_params = generics.ty_params.iter()
|
||||
.map(|ty_param| {
|
||||
builder.ty()
|
||||
.phantom_data()
|
||||
@@ -236,48 +227,31 @@ fn deserialize_visitor(
|
||||
})
|
||||
.build(),
|
||||
builder.ty().path()
|
||||
.segment("__Visitor").with_generics(trait_generics.clone()).build()
|
||||
.segment("__Visitor").with_generics(generics.clone()).build()
|
||||
.build(),
|
||||
builder.expr().call()
|
||||
.path().segment("__Visitor")
|
||||
.with_tys(forward_tys)
|
||||
.with_tys(placeholders)
|
||||
.build().build()
|
||||
.with_args({
|
||||
let len = trait_generics.lifetimes.len() + trait_generics.ty_params.len();
|
||||
let len = generics.lifetimes.len() + generics.ty_params.len();
|
||||
|
||||
(0 .. len).map(|_| builder.expr().phantom_data())
|
||||
})
|
||||
.build(),
|
||||
trait_generics,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn deserializer_ty_param(builder: &aster::AstBuilder) -> ast::TyParam {
|
||||
builder.ty_param("__D")
|
||||
.trait_bound(builder.path()
|
||||
.segment("_serde").build()
|
||||
.segment("de").build()
|
||||
.id("Deserializer")
|
||||
.build())
|
||||
.build()
|
||||
.build()
|
||||
}
|
||||
|
||||
fn deserializer_ty_arg(builder: &aster::AstBuilder) -> P<ast::Ty>{
|
||||
builder.ty().id("__D")
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
struct __Visitor;
|
||||
|
||||
impl _serde::de::Visitor for __Visitor {
|
||||
@@ -300,7 +274,7 @@ fn deserialize_unit_struct(
|
||||
}
|
||||
|
||||
deserializer.deserialize_unit_struct($type_name, __Visitor)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_tuple(
|
||||
@@ -312,14 +286,12 @@ fn deserialize_tuple(
|
||||
ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
|
||||
let is_enum = variant_ident.is_some();
|
||||
@@ -343,7 +315,7 @@ fn deserialize_tuple(
|
||||
None
|
||||
};
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
let visit_seq = deserialize_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
@@ -366,24 +338,22 @@ fn deserialize_tuple(
|
||||
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
||||
};
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $impl_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
$visit_newtype_struct
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
where __V: _serde::de::SeqVisitor
|
||||
$visit_seq
|
||||
}
|
||||
|
||||
$dispatch
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_seq(
|
||||
@@ -394,7 +364,7 @@ fn deserialize_seq(
|
||||
impl_generics: &ast::Generics,
|
||||
fields: &[Field],
|
||||
is_struct: bool,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let mut index_in_seq = 0usize;
|
||||
let let_values: Vec<_> = fields.iter()
|
||||
.enumerate()
|
||||
@@ -461,13 +431,13 @@ fn deserialize_seq(
|
||||
.build()
|
||||
};
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
$let_values
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct(
|
||||
@@ -513,14 +483,12 @@ fn deserialize_struct(
|
||||
ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
&impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
impl_generics,
|
||||
);
|
||||
|
||||
let type_path = match variant_ident {
|
||||
@@ -528,7 +496,7 @@ fn deserialize_struct(
|
||||
None => builder.path().id(type_ident).build(),
|
||||
};
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
let visit_seq = deserialize_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
@@ -538,7 +506,7 @@ fn deserialize_struct(
|
||||
true,
|
||||
);
|
||||
|
||||
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
||||
let (field_visitor, fields_stmt, visit_map) = deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
@@ -558,33 +526,29 @@ fn deserialize_struct(
|
||||
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
||||
};
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
$field_visitor
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $impl_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
where __V: _serde::de::SeqVisitor
|
||||
$visit_seq
|
||||
|
||||
#[inline]
|
||||
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: _serde::de::MapVisitor,
|
||||
{
|
||||
$visit_map_expr
|
||||
}
|
||||
where __V: _serde::de::MapVisitor
|
||||
$visit_map
|
||||
}
|
||||
|
||||
$fields_stmt
|
||||
|
||||
$dispatch
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_item_enum(
|
||||
@@ -595,7 +559,7 @@ fn deserialize_item_enum(
|
||||
ty: P<ast::Ty>,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
@@ -633,7 +597,7 @@ fn deserialize_item_enum(
|
||||
.id("__Field").id(format!("__field{}", i))
|
||||
.build();
|
||||
|
||||
let expr = deserialize_variant(
|
||||
let block = deserialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
@@ -643,24 +607,22 @@ fn deserialize_item_enum(
|
||||
item_attrs,
|
||||
);
|
||||
|
||||
let arm = quote_arm!(cx, $variant_name => { $expr });
|
||||
let arm = quote_arm!(cx, $variant_name => $block);
|
||||
variant_arms.push(arm);
|
||||
}
|
||||
variant_arms.extend(ignored_arm.into_iter());
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
$variant_visitor
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics _serde::de::EnumVisitor for $visitor_ty $where_clause {
|
||||
impl $impl_generics _serde::de::EnumVisitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
@@ -675,7 +637,7 @@ fn deserialize_item_enum(
|
||||
$variants_stmt
|
||||
|
||||
deserializer.deserialize_enum($type_name, VARIANTS, $visitor_expr)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_variant(
|
||||
@@ -686,15 +648,15 @@ fn deserialize_variant(
|
||||
ty: P<ast::Ty>,
|
||||
variant: &Variant,
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let variant_ident = variant.ident;
|
||||
|
||||
match variant.style {
|
||||
Style::Unit => {
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
try!(visitor.visit_unit());
|
||||
Ok($type_ident::$variant_ident)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
Style::Newtype => {
|
||||
deserialize_newtype_variant(
|
||||
@@ -740,7 +702,7 @@ fn deserialize_newtype_variant(
|
||||
variant_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
field: &Field,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let visit = match field.attrs.deserialize_with() {
|
||||
None => {
|
||||
let field_ty = &field.ty;
|
||||
@@ -756,7 +718,9 @@ fn deserialize_newtype_variant(
|
||||
})
|
||||
}
|
||||
};
|
||||
quote_expr!(cx, Ok($type_ident::$variant_ident($visit)))
|
||||
quote_block!(cx, {
|
||||
Ok($type_ident::$variant_ident($visit))
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_field_visitor(
|
||||
@@ -805,17 +769,15 @@ fn deserialize_field_visitor(
|
||||
let fallthrough_index_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
||||
quote_expr!(cx, Ok(__Field::__ignore))
|
||||
} else {
|
||||
quote_expr!(cx, {
|
||||
Err(_serde::de::Error::invalid_value($index_error_msg))
|
||||
})
|
||||
quote_expr!(cx, Err(_serde::de::Error::invalid_value($index_error_msg)))
|
||||
};
|
||||
|
||||
let index_body = quote_expr!(cx,
|
||||
let index_body = quote_block!(cx, {
|
||||
match value {
|
||||
$index_field_arms
|
||||
_ => $fallthrough_index_arm_expr
|
||||
}
|
||||
);
|
||||
}).unwrap();
|
||||
|
||||
// Convert the field names into byte strings.
|
||||
let str_field_names: Vec<_> = field_names.iter()
|
||||
@@ -835,12 +797,12 @@ fn deserialize_field_visitor(
|
||||
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
|
||||
};
|
||||
|
||||
let str_body = quote_expr!(cx,
|
||||
let str_body = quote_block!(cx, {
|
||||
match value {
|
||||
$str_field_arms
|
||||
_ => $fallthrough_str_arm_expr
|
||||
}
|
||||
);
|
||||
}).unwrap();
|
||||
|
||||
// Convert the field names into byte strings.
|
||||
let bytes_field_names: Vec<_> = field_names.iter()
|
||||
@@ -866,12 +828,12 @@ fn deserialize_field_visitor(
|
||||
})
|
||||
};
|
||||
|
||||
let bytes_body = quote_expr!(cx,
|
||||
let bytes_body = quote_block!(cx, {
|
||||
match value {
|
||||
$bytes_field_arms
|
||||
_ => $fallthrough_bytes_arm_expr
|
||||
}
|
||||
);
|
||||
}).unwrap();
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
impl _serde::de::Deserialize for __Field {
|
||||
@@ -879,39 +841,25 @@ fn deserialize_field_visitor(
|
||||
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<__Field, __D::Error>
|
||||
where __D: _serde::de::Deserializer,
|
||||
{
|
||||
struct __FieldVisitor<__D> {
|
||||
phantom: ::std::marker::PhantomData<__D>
|
||||
}
|
||||
struct __FieldVisitor;
|
||||
|
||||
impl<__D> _serde::de::Visitor for __FieldVisitor<__D>
|
||||
where __D: _serde::de::Deserializer
|
||||
{
|
||||
impl _serde::de::Visitor for __FieldVisitor {
|
||||
type Value = __Field;
|
||||
|
||||
fn visit_usize<__E>(&mut self, value: usize) -> ::std::result::Result<__Field, __E>
|
||||
where __E: _serde::de::Error,
|
||||
{
|
||||
$index_body
|
||||
}
|
||||
where __E: _serde::de::Error
|
||||
$index_body
|
||||
|
||||
fn visit_str<__E>(&mut self, value: &str) -> ::std::result::Result<__Field, __E>
|
||||
where __E: _serde::de::Error,
|
||||
{
|
||||
$str_body
|
||||
}
|
||||
where __E: _serde::de::Error
|
||||
$str_body
|
||||
|
||||
fn visit_bytes<__E>(&mut self, value: &[u8]) -> ::std::result::Result<__Field, __E>
|
||||
where __E: _serde::de::Error,
|
||||
{
|
||||
$bytes_body
|
||||
}
|
||||
where __E: _serde::de::Error
|
||||
$bytes_body
|
||||
}
|
||||
|
||||
deserializer.deserialize_struct_field(
|
||||
__FieldVisitor::<__D>{
|
||||
phantom: ::std::marker::PhantomData
|
||||
}
|
||||
)
|
||||
deserializer.deserialize_struct_field(__FieldVisitor)
|
||||
}
|
||||
}
|
||||
).unwrap();
|
||||
@@ -927,7 +875,7 @@ fn deserialize_struct_visitor(
|
||||
impl_generics: &ast::Generics,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
|
||||
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Block>) {
|
||||
let field_exprs = fields.iter()
|
||||
.map(|field| field.attrs.name().deserialize_name())
|
||||
.collect();
|
||||
@@ -940,7 +888,7 @@ fn deserialize_struct_visitor(
|
||||
false,
|
||||
);
|
||||
|
||||
let visit_map_expr = deserialize_map(
|
||||
let visit_map = deserialize_map(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
@@ -968,7 +916,7 @@ fn deserialize_struct_visitor(
|
||||
const FIELDS: &'static [&'static str] = $fields_expr;
|
||||
).unwrap();
|
||||
|
||||
(field_visitor, fields_stmt, visit_map_expr)
|
||||
(field_visitor, fields_stmt, visit_map)
|
||||
}
|
||||
|
||||
fn deserialize_map(
|
||||
@@ -979,7 +927,7 @@ fn deserialize_map(
|
||||
impl_generics: &ast::Generics,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
// Create the field names for the fields.
|
||||
let fields_names = fields.iter()
|
||||
.enumerate()
|
||||
@@ -1086,7 +1034,7 @@ fn deserialize_map(
|
||||
)
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
$let_values
|
||||
|
||||
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
||||
@@ -1102,7 +1050,7 @@ fn deserialize_map(
|
||||
$extract_values
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
||||
|
||||
+36
-39
@@ -70,10 +70,8 @@ fn serialize_item(
|
||||
#[automatically_derived]
|
||||
impl $impl_generics _serde::ser::Serialize for $ty $where_clause {
|
||||
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||
where __S: _serde::ser::Serializer,
|
||||
{
|
||||
$body
|
||||
}
|
||||
where __S: _serde::ser::Serializer
|
||||
$body
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
@@ -119,7 +117,7 @@ fn serialize_body(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
match item.body {
|
||||
Body::Enum(ref variants) => {
|
||||
serialize_item_enum(
|
||||
@@ -179,12 +177,12 @@ fn serialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
|
||||
quote_expr!(cx,
|
||||
quote_block!(cx, {
|
||||
_serializer.serialize_unit_struct($type_name)
|
||||
)
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct(
|
||||
@@ -194,7 +192,7 @@ fn serialize_newtype_struct(
|
||||
item_ty: P<ast::Ty>,
|
||||
field: &Field,
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
|
||||
let mut field_expr = quote_expr!(cx, &self.0);
|
||||
@@ -203,9 +201,9 @@ fn serialize_newtype_struct(
|
||||
&item_ty, impl_generics, &field.ty, path, field_expr);
|
||||
}
|
||||
|
||||
quote_expr!(cx,
|
||||
quote_block!(cx, {
|
||||
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
||||
)
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
@@ -215,7 +213,7 @@ fn serialize_tuple_struct(
|
||||
ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
@@ -229,11 +227,11 @@ fn serialize_tuple_struct(
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
let mut state = try!(_serializer.serialize_tuple_struct($type_name, $len));
|
||||
$serialize_stmts
|
||||
_serializer.serialize_tuple_struct_end(state)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_struct(
|
||||
@@ -243,7 +241,7 @@ fn serialize_struct(
|
||||
ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let serialize_fields = serialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
@@ -268,11 +266,11 @@ fn serialize_struct(
|
||||
})
|
||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
let mut state = try!(_serializer.serialize_struct($type_name, $len));
|
||||
$serialize_fields
|
||||
_serializer.serialize_struct_end(state)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_item_enum(
|
||||
@@ -283,7 +281,7 @@ fn serialize_item_enum(
|
||||
ty: P<ast::Ty>,
|
||||
variants: &[Variant],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let arms: Vec<_> =
|
||||
variants.iter()
|
||||
.enumerate()
|
||||
@@ -301,11 +299,11 @@ fn serialize_item_enum(
|
||||
})
|
||||
.collect();
|
||||
|
||||
quote_expr!(cx,
|
||||
quote_block!(cx, {
|
||||
match *self {
|
||||
$arms
|
||||
}
|
||||
)
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_variant(
|
||||
@@ -326,18 +324,17 @@ fn serialize_variant(
|
||||
match variant.style {
|
||||
Style::Unit => {
|
||||
quote_arm!(cx,
|
||||
$type_ident::$variant_ident => {
|
||||
$type_ident::$variant_ident =>
|
||||
_serde::ser::Serializer::serialize_unit_variant(
|
||||
_serializer,
|
||||
$type_name,
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
)
|
||||
}
|
||||
),
|
||||
)
|
||||
},
|
||||
Style::Newtype => {
|
||||
let expr = serialize_newtype_variant(
|
||||
let block = serialize_newtype_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_name,
|
||||
@@ -349,7 +346,7 @@ fn serialize_variant(
|
||||
);
|
||||
|
||||
quote_arm!(cx,
|
||||
$type_ident::$variant_ident(ref __simple_value) => { $expr }
|
||||
$type_ident::$variant_ident(ref __simple_value) => $block
|
||||
)
|
||||
},
|
||||
Style::Tuple => {
|
||||
@@ -365,7 +362,7 @@ fn serialize_variant(
|
||||
)
|
||||
.build();
|
||||
|
||||
let expr = serialize_tuple_variant(
|
||||
let block = serialize_tuple_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_name,
|
||||
@@ -377,12 +374,12 @@ fn serialize_variant(
|
||||
);
|
||||
|
||||
quote_arm!(cx,
|
||||
$pat => { $expr }
|
||||
$pat => $block
|
||||
)
|
||||
}
|
||||
Style::Struct => {
|
||||
let mut pat = builder.pat().struct_().id(type_ident).id(variant_ident).build();
|
||||
for field in variant.fields.iter() {
|
||||
for field in &variant.fields {
|
||||
let name = match field.ident {
|
||||
Some(name) => name,
|
||||
None => cx.span_bug(field.span, "struct variant has unnamed fields"),
|
||||
@@ -395,7 +392,7 @@ fn serialize_variant(
|
||||
}
|
||||
let pat = pat.build();
|
||||
|
||||
let expr = serialize_struct_variant(
|
||||
let block = serialize_struct_variant(
|
||||
cx,
|
||||
builder,
|
||||
variant_index,
|
||||
@@ -407,7 +404,7 @@ fn serialize_variant(
|
||||
);
|
||||
|
||||
quote_arm!(cx,
|
||||
$pat => { $expr }
|
||||
$pat => $block
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -422,14 +419,14 @@ fn serialize_newtype_variant(
|
||||
item_ty: P<ast::Ty>,
|
||||
generics: &ast::Generics,
|
||||
field: &Field,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let mut field_expr = quote_expr!(cx, __simple_value);
|
||||
if let Some(path) = field.attrs.serialize_with() {
|
||||
field_expr = wrap_serialize_with(cx, builder,
|
||||
&item_ty, generics, &field.ty, path, field_expr);
|
||||
}
|
||||
|
||||
quote_expr!(cx,
|
||||
quote_block!(cx, {
|
||||
_serde::ser::Serializer::serialize_newtype_variant(
|
||||
_serializer,
|
||||
$type_name,
|
||||
@@ -437,7 +434,7 @@ fn serialize_newtype_variant(
|
||||
$variant_name,
|
||||
$field_expr,
|
||||
)
|
||||
)
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
@@ -449,7 +446,7 @@ fn serialize_tuple_variant(
|
||||
generics: &ast::Generics,
|
||||
structure_ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
@@ -462,11 +459,11 @@ fn serialize_tuple_variant(
|
||||
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
let mut state = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
||||
$serialize_stmts
|
||||
_serializer.serialize_tuple_variant_end(state)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
@@ -478,7 +475,7 @@ fn serialize_struct_variant(
|
||||
ty: P<ast::Ty>,
|
||||
fields: &[Field],
|
||||
item_attrs: &attr::Item,
|
||||
) -> P<ast::Expr> {
|
||||
) -> P<ast::Block> {
|
||||
|
||||
let serialize_fields = serialize_struct_visitor(
|
||||
cx,
|
||||
@@ -504,7 +501,7 @@ fn serialize_struct_variant(
|
||||
})
|
||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_block!(cx, {
|
||||
let mut state = try!(_serializer.serialize_struct_variant(
|
||||
$item_name,
|
||||
$variant_index,
|
||||
@@ -513,7 +510,7 @@ fn serialize_struct_variant(
|
||||
));
|
||||
$serialize_fields
|
||||
_serializer.serialize_struct_variant_end(state)
|
||||
})
|
||||
}).unwrap()
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_visitor(
|
||||
|
||||
+22
-6
@@ -1,37 +1,53 @@
|
||||
[package]
|
||||
name = "serde_macros"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://github.com/serde-rs/serde"
|
||||
keywords = ["serde", "serialization"]
|
||||
include = ["Cargo.toml", "src/**/*.rs"]
|
||||
include = ["Cargo.toml", "src/**/*.rs", "build.rs"]
|
||||
build = "build.rs"
|
||||
|
||||
[lib]
|
||||
name = "serde_macros"
|
||||
plugin = true
|
||||
|
||||
[features]
|
||||
unstable-testing = ["clippy", "serde/unstable-testing", "serde_codegen/unstable-testing"]
|
||||
unstable-testing = [
|
||||
"clippy",
|
||||
"skeptic",
|
||||
"serde_json",
|
||||
"serde/unstable-testing",
|
||||
"serde_codegen/unstable-testing"
|
||||
]
|
||||
|
||||
[build-dependencies]
|
||||
skeptic = { version = "^0.6.0", optional = true }
|
||||
|
||||
[dependencies]
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
serde_codegen = { version = "0.8.0", default-features = false, features = ["unstable"] }
|
||||
serde_codegen = { version = "=0.8.1", default-features = false, features = ["unstable"], path = "../serde_codegen" }
|
||||
skeptic = { version = "^0.6.0", optional = true }
|
||||
serde_json = { version = "0.8.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
clippy = "^0.*"
|
||||
compiletest_rs = "^0.2.0"
|
||||
fnv = "1.0"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = "0.8.0"
|
||||
serde_test = "0.8.0"
|
||||
serde = { version = "0.8.0", path = "../serde" }
|
||||
serde_test = { version = "0.8.0", path = "../serde_test" }
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
path = "tests/test.rs"
|
||||
|
||||
[[test]]
|
||||
name = "skeptic"
|
||||
path = "tests/skeptic.rs"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
path = "benches/bench.rs"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#[cfg(feature = "unstable-testing")]
|
||||
mod inner {
|
||||
extern crate skeptic;
|
||||
|
||||
pub fn main() {
|
||||
println!("cargo:rerun-if-changed=../README.md");
|
||||
skeptic::generate_doc_tests(&["../README.md"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "unstable-testing"))]
|
||||
mod inner {
|
||||
pub fn main() {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
inner::main()
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#![cfg(feature = "unstable-testing")]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));
|
||||
@@ -6,3 +6,4 @@ extern crate test;
|
||||
include!("../../testing/tests/test.rs.in");
|
||||
|
||||
mod compile_tests;
|
||||
mod skeptic;
|
||||
|
||||
@@ -11,4 +11,4 @@ keywords = ["serde", "serialization"]
|
||||
include = ["Cargo.toml", "src/**/*.rs"]
|
||||
|
||||
[dependencies]
|
||||
serde = "0.8.0"
|
||||
serde = { version = "0.8.0", path = "../serde" }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::net;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
@@ -745,6 +746,28 @@ declare_tests! {
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_duration {
|
||||
Duration::new(1, 2) => &[
|
||||
Token::StructStart("Duration", 2),
|
||||
Token::StructSep,
|
||||
Token::Str("secs"),
|
||||
Token::U64(1),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("nanos"),
|
||||
Token::U32(2),
|
||||
Token::StructEnd,
|
||||
],
|
||||
Duration::new(1, 2) => &[
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I64(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I64(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_net_ipv4addr {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::net;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str;
|
||||
use std::time::Duration;
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{
|
||||
@@ -324,6 +325,19 @@ declare_ser_tests! {
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_duration {
|
||||
Duration::new(1, 2) => &[
|
||||
Token::StructStart("Duration", 2),
|
||||
Token::StructSep,
|
||||
Token::Str("secs"),
|
||||
Token::U64(1),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("nanos"),
|
||||
Token::U32(2),
|
||||
Token::StructEnd,
|
||||
],
|
||||
}
|
||||
test_net_ipv4addr {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user