mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-21 02:41:04 +00:00
Compare commits
39 Commits
v0.8.0-rc2
...
v0.8.0
| Author | SHA1 | Date | |
|---|---|---|---|
| f12f640590 | |||
| d02e959b3f | |||
| ea833d3427 | |||
| 78e74886be | |||
| 061a1d8a8c | |||
| de9fd3b04e | |||
| e36f33589c | |||
| a892a13473 | |||
| 556d5bdc27 | |||
| c6acec29e5 | |||
| dce02c624b | |||
| 77e56613a5 | |||
| 3b7fa47b2e | |||
| f5fd7f5950 | |||
| fb6fc4e19f | |||
| 85772726ee | |||
| f05ba9fdf2 | |||
| 2e829ae4e6 | |||
| 25a5dd1579 | |||
| 1831b471f9 | |||
| 49ff56aa15 | |||
| 89549e2567 | |||
| 124bacd871 | |||
| 4280dd466d | |||
| 65eb116a85 | |||
| e15940f355 | |||
| 68440952ab | |||
| d751b4c39a | |||
| d10a69b243 | |||
| 1a1b6fbf85 | |||
| 93968455f3 | |||
| 4722571a4d | |||
| 36a7bf6244 | |||
| 89f0ad99a6 | |||
| 05ad8662e2 | |||
| 15c09a8d2c | |||
| 80a27cbb4a | |||
| 13e1a129dd | |||
| 334a6e788a |
@@ -81,7 +81,7 @@ fn main() {
|
|||||||
|
|
||||||
This produces the following output when run:
|
This produces the following output when run:
|
||||||
|
|
||||||
```
|
```sh
|
||||||
% cargo run
|
% cargo run
|
||||||
serialized vec: "[1,2]"
|
serialized vec: "[1,2]"
|
||||||
deserialized vec: [1, 2]
|
deserialized vec: [1, 2]
|
||||||
@@ -123,7 +123,7 @@ serde_json = "*"
|
|||||||
```
|
```
|
||||||
|
|
||||||
Next, we define our source file, `src/main.rs.in`. Note this is a different
|
Next, we define our source file, `src/main.rs.in`. Note this is a different
|
||||||
extension than usual becaues we need to do code generation:
|
extension than usual because we need to do code generation:
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -175,7 +175,7 @@ pub fn main() {
|
|||||||
|
|
||||||
All this produces this when run:
|
All this produces this when run:
|
||||||
|
|
||||||
```
|
```sh
|
||||||
% cargo run
|
% cargo run
|
||||||
{"x":1,"y":2}
|
{"x":1,"y":2}
|
||||||
Point { x: 1, y: 2 }
|
Point { x: 1, y: 2 }
|
||||||
@@ -232,7 +232,7 @@ fn main() {
|
|||||||
|
|
||||||
This also produces the same output:
|
This also produces the same output:
|
||||||
|
|
||||||
```
|
```sh
|
||||||
% cargo run
|
% cargo run
|
||||||
{"x":1,"y":2}
|
{"x":1,"y":2}
|
||||||
Point { x: 1, y: 2 }
|
Point { x: 1, y: 2 }
|
||||||
@@ -314,14 +314,14 @@ The `src/main.rs.in` is the same as before.
|
|||||||
|
|
||||||
Then to run with stable:
|
Then to run with stable:
|
||||||
|
|
||||||
```
|
```sh
|
||||||
% cargo build
|
% cargo build
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
Or with nightly:
|
Or with nightly:
|
||||||
|
|
||||||
```
|
```sh
|
||||||
% cargo build --features nightly --no-default-features
|
% cargo build --features nightly --no-default-features
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
@@ -825,6 +825,7 @@ Serialization Formats Using Serde
|
|||||||
| ------ | ---- |
|
| ------ | ---- |
|
||||||
| Bincode | [bincode](https://crates.io/crates/bincode) |
|
| Bincode | [bincode](https://crates.io/crates/bincode) |
|
||||||
| env vars | [envy](https://crates.io/crates/envy) |
|
| env vars | [envy](https://crates.io/crates/envy) |
|
||||||
|
| Hjson | [serde\_hjson](https://crates.io/crates/serde-hjson) |
|
||||||
| JSON | [serde\_json](https://crates.io/crates/serde_json) |
|
| JSON | [serde\_json](https://crates.io/crates/serde_json) |
|
||||||
| MessagePack | [rmp](https://crates.io/crates/rmp) |
|
| MessagePack | [rmp](https://crates.io/crates/rmp) |
|
||||||
| XML | [serde\_xml](https://github.com/serde-rs/xml) |
|
| XML | [serde\_xml](https://github.com/serde-rs/xml) |
|
||||||
|
|||||||
+1
-3
@@ -1,8 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
# DO NOT RELEASE ANY MORE 0.7 RELEASES FROM THIS BRANCH
|
version = "0.8.0"
|
||||||
# USE THE 0.7.x BRANCH
|
|
||||||
version = "0.8.0-rc2"
|
|
||||||
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"
|
||||||
|
|||||||
+13
-3
@@ -356,7 +356,7 @@ pub struct PhantomDataVisitor<T> {
|
|||||||
marker: PhantomData<T>,
|
marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Visitor for PhantomDataVisitor<T> where T: Deserialize {
|
impl<T> Visitor for PhantomDataVisitor<T> {
|
||||||
type Value = PhantomData<T>;
|
type Value = PhantomData<T>;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -367,7 +367,7 @@ impl<T> Visitor for PhantomDataVisitor<T> where T: Deserialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deserialize for PhantomData<T> where T: Deserialize {
|
impl<T> Deserialize for PhantomData<T> {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<PhantomData<T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<PhantomData<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
@@ -593,7 +593,7 @@ macro_rules! array_impls {
|
|||||||
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
deserializer.deserialize_fixed_size_array($len, $visitor::new())
|
deserializer.deserialize_seq_fixed_size($len, $visitor::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
@@ -939,6 +939,16 @@ impl<T: Deserialize> Deserialize for Box<[T]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
|
impl Deserialize for Box<str> {
|
||||||
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
|
where D: Deserializer
|
||||||
|
{
|
||||||
|
let s = try!(String::deserialize(deserializer));
|
||||||
|
Ok(s.into_boxed_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Arc<T> {
|
impl<T: Deserialize> Deserialize for Arc<T> {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
|
||||||
|
|||||||
+152
-193
@@ -10,6 +10,73 @@ use collections::{String, Vec};
|
|||||||
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Macro helper to not have to re-implement all the defaulted methods.
|
||||||
|
/// Every given method ignores all arguments and forwards to `deserialize`.
|
||||||
|
/// Note that `deserialize_enum` simply returns an `Error::invalid_type`.
|
||||||
|
macro_rules! de_forward_to_deserialize {
|
||||||
|
($($func:ident),*) => {
|
||||||
|
$(de_forward_to_deserialize!{func: $func})*
|
||||||
|
};
|
||||||
|
(func: deserialize_unit_struct) => {
|
||||||
|
de_forward_to_deserialize!{named: deserialize_unit_struct}
|
||||||
|
};
|
||||||
|
(func: deserialize_newtype_struct) => {
|
||||||
|
de_forward_to_deserialize!{named: deserialize_newtype_struct}
|
||||||
|
};
|
||||||
|
(func: deserialize_tuple) => {
|
||||||
|
de_forward_to_deserialize!{tup_fn: deserialize_tuple}
|
||||||
|
};
|
||||||
|
(func: deserialize_seq_fixed_size) => {
|
||||||
|
de_forward_to_deserialize!{tup_fn: deserialize_seq_fixed_size}
|
||||||
|
};
|
||||||
|
(func: deserialize_tuple_struct) => {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize_tuple_struct<__V>(&mut self, _: &str, _: usize, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(func: deserialize_struct) => {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize_struct<__V>(&mut self, _: &str, _: &[&str], visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(func: deserialize_enum) => {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize_enum<__V>(&mut self, _: &str, _: &[&str], _: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::EnumVisitor {
|
||||||
|
Err($crate::de::Error::invalid_type($crate::de::Type::Enum))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(named: $func:ident) => {
|
||||||
|
#[inline]
|
||||||
|
fn $func<__V>(&mut self, _: &str, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(tup_fn: $func: ident) => {
|
||||||
|
#[inline]
|
||||||
|
fn $func<__V>(&mut self, _: usize, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(func: $func:ident) => {
|
||||||
|
#[inline]
|
||||||
|
fn $func<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: $crate::de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// modules come after macros
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
mod from_primitive;
|
mod from_primitive;
|
||||||
@@ -243,278 +310,166 @@ pub trait Deserializer {
|
|||||||
where V: Visitor;
|
where V: Visitor;
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `bool` value.
|
/// This method hints that the `Deserialize` type is expecting a `bool` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_bool<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_bool<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `usize` value.
|
/// This method hints that the `Deserialize` type is expecting an `usize` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_u64`.
|
||||||
fn deserialize_usize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_usize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_u64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `u8` value.
|
/// This method hints that the `Deserialize` type is expecting an `u8` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_u64`.
|
||||||
fn deserialize_u8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_u64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `u16` value.
|
/// This method hints that the `Deserialize` type is expecting an `u16` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_u64`.
|
||||||
fn deserialize_u16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_u64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `u32` value.
|
/// This method hints that the `Deserialize` type is expecting an `u32` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_u64`.
|
||||||
fn deserialize_u32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_u64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `u64` value.
|
/// This method hints that the `Deserialize` type is expecting an `u64` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_u64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_u64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `isize` value.
|
/// This method hints that the `Deserialize` type is expecting an `isize` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_i64`.
|
||||||
fn deserialize_isize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_isize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_i64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `i8` value.
|
/// This method hints that the `Deserialize` type is expecting an `i8` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_i64`.
|
||||||
fn deserialize_i8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_i64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `i16` value.
|
/// This method hints that the `Deserialize` type is expecting an `i16` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_i64`.
|
||||||
fn deserialize_i16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_i64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `i32` value.
|
/// This method hints that the `Deserialize` type is expecting an `i32` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_i64`.
|
||||||
fn deserialize_i32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_i64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `i64` value.
|
/// This method hints that the `Deserialize` type is expecting an `i64` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_i64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_i64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `f32` value.
|
/// This method hints that the `Deserialize` type is expecting a `f32` value.
|
||||||
#[inline]
|
/// A reasonable default is to forward to `deserialize_f64`.
|
||||||
fn deserialize_f32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_f32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_f64(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `f64` value.
|
/// This method hints that the `Deserialize` type is expecting a `f64` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_f64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_f64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `char` value.
|
/// This method hints that the `Deserialize` type is expecting a `char` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_char<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_char<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `&str` value.
|
/// This method hints that the `Deserialize` type is expecting a `&str` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_str<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_str<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `String` value.
|
/// This method hints that the `Deserialize` type is expecting a `String` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_string<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_string<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_str(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `unit` value.
|
/// This method hints that the `Deserialize` type is expecting an `unit` value.
|
||||||
#[inline]
|
|
||||||
fn deserialize_unit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_unit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an `Option` value. This allows
|
/// This method hints that the `Deserialize` type is expecting an `Option` value. This allows
|
||||||
/// deserializers that encode an optional value as a nullable value to convert the null value
|
/// deserializers that encode an optional value as a nullable value to convert the null value
|
||||||
/// into a `None`, and a regular value as `Some(value)`.
|
/// into a `None`, and a regular value as `Some(value)`.
|
||||||
#[inline]
|
|
||||||
fn deserialize_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a sequence value. This allows
|
/// This method hints that the `Deserialize` type is expecting a sequence value. This allows
|
||||||
/// deserializers to parse sequences that aren't tagged as sequences.
|
/// deserializers to parse sequences that aren't tagged as sequences.
|
||||||
#[inline]
|
|
||||||
fn deserialize_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a fixed size array. This allows
|
/// This method hints that the `Deserialize` type is expecting a fixed size array. This allows
|
||||||
/// deserializers to parse arrays that aren't tagged as arrays.
|
/// deserializers to parse arrays that aren't tagged as arrays.
|
||||||
///
|
fn deserialize_seq_fixed_size<V>(&mut self,
|
||||||
/// By default, this deserializes arrays from a sequence.
|
len: usize,
|
||||||
#[inline]
|
|
||||||
fn deserialize_fixed_size_array<V>(&mut self,
|
|
||||||
_len: usize,
|
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows
|
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows
|
||||||
/// deserializers that provide a custom byte vector serialization to properly deserialize the
|
/// deserializers that provide a custom byte vector serialization to properly deserialize the
|
||||||
/// type.
|
/// type.
|
||||||
#[inline]
|
|
||||||
fn deserialize_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_seq(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a map of values. This allows
|
/// This method hints that the `Deserialize` type is expecting a map of values. This allows
|
||||||
/// deserializers to parse sequences that aren't tagged as maps.
|
/// deserializers to parse sequences that aren't tagged as maps.
|
||||||
#[inline]
|
|
||||||
fn deserialize_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a unit struct. This allows
|
/// This method hints that the `Deserialize` type is expecting a unit struct. This allows
|
||||||
/// deserializers to a unit struct that aren't tagged as a unit struct.
|
/// deserializers to a unit struct that aren't tagged as a unit struct.
|
||||||
#[inline]
|
|
||||||
fn deserialize_unit_struct<V>(&mut self,
|
fn deserialize_unit_struct<V>(&mut self,
|
||||||
_name: &'static str,
|
name: &'static str,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_unit(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a newtype struct. This allows
|
/// This method hints that the `Deserialize` type is expecting a newtype struct. This allows
|
||||||
/// deserializers to a newtype struct that aren't tagged as a newtype struct.
|
/// deserializers to a newtype struct that aren't tagged as a newtype struct.
|
||||||
#[inline]
|
/// A reasonable default is to simply deserialize the expected value directly.
|
||||||
fn deserialize_newtype_struct<V>(&mut self,
|
fn deserialize_newtype_struct<V>(&mut self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_tuple_struct(name, 1, visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a tuple struct. This allows
|
/// This method hints that the `Deserialize` type is expecting a tuple struct. This allows
|
||||||
/// deserializers to parse sequences that aren't tagged as sequences.
|
/// deserializers to parse sequences that aren't tagged as sequences.
|
||||||
#[inline]
|
|
||||||
fn deserialize_tuple_struct<V>(&mut self,
|
fn deserialize_tuple_struct<V>(&mut self,
|
||||||
_name: &'static str,
|
name: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_tuple(len, visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a struct. This allows
|
/// This method hints that the `Deserialize` type is expecting a struct. This allows
|
||||||
/// deserializers to parse sequences that aren't tagged as maps.
|
/// deserializers to parse sequences that aren't tagged as maps.
|
||||||
#[inline]
|
|
||||||
fn deserialize_struct<V>(&mut self,
|
fn deserialize_struct<V>(&mut self,
|
||||||
_name: &'static str,
|
name: &'static str,
|
||||||
_fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize_map(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting some sort of struct field
|
/// This method hints that the `Deserialize` type is expecting some sort of struct field
|
||||||
/// name. This allows deserializers to choose between &str, usize, or &[u8] to properly
|
/// name. This allows deserializers to choose between &str, usize, or &[u8] to properly
|
||||||
/// deserialize a struct field.
|
/// deserialize a struct field.
|
||||||
#[inline]
|
|
||||||
fn deserialize_struct_field<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_struct_field<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor,
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting a tuple value. This allows
|
/// This method hints that the `Deserialize` type is expecting a tuple value. This allows
|
||||||
/// deserializers that provide a custom tuple serialization to properly deserialize the type.
|
/// deserializers that provide a custom tuple serialization to properly deserialize the type.
|
||||||
#[inline]
|
fn deserialize_tuple<V>(&mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
fn deserialize_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
where V: Visitor;
|
||||||
where V: Visitor,
|
|
||||||
{
|
|
||||||
self.deserialize_seq(visitor)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type is expecting an enum value. This allows
|
/// This method hints that the `Deserialize` type is expecting an enum value. This allows
|
||||||
/// deserializers that provide a custom enumeration serialization to properly deserialize the
|
/// deserializers that provide a custom enumeration serialization to properly deserialize the
|
||||||
/// type.
|
/// type.
|
||||||
#[inline]
|
|
||||||
fn deserialize_enum<V>(&mut self,
|
fn deserialize_enum<V>(&mut self,
|
||||||
_enum: &'static str,
|
name: &'static str,
|
||||||
_variants: &'static [&'static str],
|
variants: &'static [&'static str],
|
||||||
_visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: EnumVisitor,
|
where V: EnumVisitor;
|
||||||
{
|
|
||||||
Err(Error::invalid_type(Type::Enum))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method hints that the `Deserialize` type needs to deserialize a value whose type
|
/// This method hints that the `Deserialize` type needs to deserialize a value whose type
|
||||||
/// doesn't matter because it is ignored.
|
/// doesn't matter because it is ignored.
|
||||||
#[inline]
|
|
||||||
fn deserialize_ignored_any<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_ignored_any<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor;
|
||||||
{
|
|
||||||
self.deserialize(visitor)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -525,9 +480,10 @@ pub trait Visitor {
|
|||||||
type Value: Deserialize;
|
type Value: Deserialize;
|
||||||
|
|
||||||
/// `visit_bool` deserializes a `bool` into a `Value`.
|
/// `visit_bool` deserializes a `bool` into a `Value`.
|
||||||
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
|
fn visit_bool<E>(&mut self, v: bool) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::Bool))
|
Err(Error::invalid_type(Type::Bool))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -560,9 +516,10 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_i64` deserializes a `i64` into a `Value`.
|
/// `visit_i64` deserializes a `i64` into a `Value`.
|
||||||
fn visit_i64<E>(&mut self, _v: i64) -> Result<Self::Value, E>
|
fn visit_i64<E>(&mut self, v: i64) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::I64))
|
Err(Error::invalid_type(Type::I64))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -595,9 +552,10 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_u64` deserializes a `u64` into a `Value`.
|
/// `visit_u64` deserializes a `u64` into a `Value`.
|
||||||
fn visit_u64<E>(&mut self, _v: u64) -> Result<Self::Value, E>
|
fn visit_u64<E>(&mut self, v: u64) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::U64))
|
Err(Error::invalid_type(Type::U64))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -609,9 +567,10 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_f64` deserializes a `f64` into a `Value`.
|
/// `visit_f64` deserializes a `f64` into a `Value`.
|
||||||
fn visit_f64<E>(&mut self, _v: f64) -> Result<Self::Value, E>
|
fn visit_f64<E>(&mut self, v: f64) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::F64))
|
Err(Error::invalid_type(Type::F64))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,9 +583,10 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_str` deserializes a `&str` into a `Value`.
|
/// `visit_str` deserializes a `&str` into a `Value`.
|
||||||
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
|
fn visit_str<E>(&mut self, v: &str) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::Str))
|
Err(Error::invalid_type(Type::Str))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -650,9 +610,10 @@ pub trait Visitor {
|
|||||||
|
|
||||||
/// `visit_unit_struct` deserializes a unit struct into a `Value`.
|
/// `visit_unit_struct` deserializes a unit struct into a `Value`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_unit_struct<E>(&mut self, _name: &'static str) -> Result<Self::Value, E>
|
fn visit_unit_struct<E>(&mut self, name: &'static str) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = name;
|
||||||
self.visit_unit()
|
self.visit_unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,37 +625,42 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_some` deserializes a value into a `Value`.
|
/// `visit_some` deserializes a value into a `Value`.
|
||||||
fn visit_some<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
|
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
|
let _ = deserializer;
|
||||||
Err(Error::invalid_type(Type::Option))
|
Err(Error::invalid_type(Type::Option))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_newtype_struct` deserializes a value into a `Value`.
|
/// `visit_newtype_struct` deserializes a value into a `Value`.
|
||||||
fn visit_newtype_struct<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
|
fn visit_newtype_struct<D>(&mut self, deserializer: &mut D) -> Result<Self::Value, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
{
|
{
|
||||||
|
let _ = deserializer;
|
||||||
Err(Error::invalid_type(Type::NewtypeStruct))
|
Err(Error::invalid_type(Type::NewtypeStruct))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_seq` deserializes a `SeqVisitor` into a `Value`.
|
/// `visit_seq` deserializes a `SeqVisitor` into a `Value`.
|
||||||
fn visit_seq<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: SeqVisitor,
|
where V: SeqVisitor,
|
||||||
{
|
{
|
||||||
|
let _ = visitor;
|
||||||
Err(Error::invalid_type(Type::Seq))
|
Err(Error::invalid_type(Type::Seq))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_map` deserializes a `MapVisitor` into a `Value`.
|
/// `visit_map` deserializes a `MapVisitor` into a `Value`.
|
||||||
fn visit_map<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
fn visit_map<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||||
where V: MapVisitor,
|
where V: MapVisitor,
|
||||||
{
|
{
|
||||||
|
let _ = visitor;
|
||||||
Err(Error::invalid_type(Type::Map))
|
Err(Error::invalid_type(Type::Map))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_bytes` deserializes a `&[u8]` into a `Value`.
|
/// `visit_bytes` deserializes a `&[u8]` into a `Value`.
|
||||||
fn visit_bytes<E>(&mut self, _v: &[u8]) -> Result<Self::Value, E>
|
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
let _ = v;
|
||||||
Err(Error::invalid_type(Type::Bytes))
|
Err(Error::invalid_type(Type::Bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -870,33 +836,26 @@ pub trait VariantVisitor {
|
|||||||
Err(Error::invalid_type(Type::UnitVariant))
|
Err(Error::invalid_type(Type::UnitVariant))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_newtype` is called when deserializing a variant with a single value. By default this
|
/// `visit_newtype` is called when deserializing a variant with a single value.
|
||||||
/// uses the `visit_tuple` method to deserialize the value.
|
/// A good default is often to use the `visit_tuple` method to deserialize a `(value,)`.
|
||||||
#[inline]
|
|
||||||
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
||||||
where T: Deserialize,
|
where T: Deserialize;
|
||||||
{
|
|
||||||
let (value,) = try!(self.visit_tuple(1, impls::TupleVisitor1::new()));
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `visit_tuple` is called when deserializing a tuple-like variant.
|
/// `visit_tuple` is called when deserializing a tuple-like variant.
|
||||||
|
/// If no tuple variants are expected, yield a
|
||||||
|
/// `Err(serde::de::Error::invalid_type(serde::de::Type::TupleVariant))`
|
||||||
fn visit_tuple<V>(&mut self,
|
fn visit_tuple<V>(&mut self,
|
||||||
_len: usize,
|
len: usize,
|
||||||
_visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor;
|
||||||
{
|
|
||||||
Err(Error::invalid_type(Type::TupleVariant))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `visit_struct` is called when deserializing a struct-like variant.
|
/// `visit_struct` is called when deserializing a struct-like variant.
|
||||||
|
/// If no struct variants are expected, yield a
|
||||||
|
/// `Err(serde::de::Error::invalid_type(serde::de::Type::StructVariant))`
|
||||||
fn visit_struct<V>(&mut self,
|
fn visit_struct<V>(&mut self,
|
||||||
_fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
_visitor: V) -> Result<V::Value, Self::Error>
|
visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: Visitor
|
where V: Visitor;
|
||||||
{
|
|
||||||
Err(Error::invalid_type(Type::StructVariant))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
||||||
|
|||||||
@@ -174,6 +174,22 @@ impl<E> de::Deserializer for UnitDeserializer<E>
|
|||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
@@ -209,6 +225,23 @@ macro_rules! primitive_deserializer {
|
|||||||
{
|
{
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
|
||||||
where V: de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
@@ -273,6 +306,22 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
|
|||||||
{
|
{
|
||||||
visitor.visit(self)
|
visitor.visit(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E>
|
impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E>
|
||||||
@@ -289,6 +338,29 @@ impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E>
|
|||||||
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
||||||
|
where T: super::Deserialize,
|
||||||
|
{
|
||||||
|
let (value,) = try!(self.visit_tuple(1, super::impls::TupleVisitor1::new()));
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_tuple<V>(&mut self,
|
||||||
|
_len: usize,
|
||||||
|
_visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: super::Visitor
|
||||||
|
{
|
||||||
|
Err(super::Error::invalid_type(super::Type::TupleVariant))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_struct<V>(&mut self,
|
||||||
|
_fields: &'static [&'static str],
|
||||||
|
_visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: super::Visitor
|
||||||
|
{
|
||||||
|
Err(super::Error::invalid_type(super::Type::StructVariant))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -331,6 +403,22 @@ impl<E> de::Deserializer for StringDeserializer<E>
|
|||||||
{
|
{
|
||||||
visitor.visit(self)
|
visitor.visit(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
@@ -348,6 +436,29 @@ impl<'a, E> de::VariantVisitor for StringDeserializer<E>
|
|||||||
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
||||||
|
where T: super::Deserialize,
|
||||||
|
{
|
||||||
|
let (value,) = try!(self.visit_tuple(1, super::impls::TupleVisitor1::new()));
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_tuple<V>(&mut self,
|
||||||
|
_len: usize,
|
||||||
|
_visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: super::Visitor
|
||||||
|
{
|
||||||
|
Err(super::Error::invalid_type(super::Type::TupleVariant))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_struct<V>(&mut self,
|
||||||
|
_fields: &'static [&'static str],
|
||||||
|
_visitor: V) -> Result<V::Value, Self::Error>
|
||||||
|
where V: super::Visitor
|
||||||
|
{
|
||||||
|
Err(super::Error::invalid_type(super::Type::StructVariant))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -384,6 +495,23 @@ impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
|
|||||||
{
|
{
|
||||||
visitor.visit_seq(self)
|
visitor.visit_seq(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
|
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
|
||||||
@@ -490,6 +618,23 @@ impl<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
|
|||||||
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
|
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
|
||||||
visitor.visit_seq(&mut self.visitor)
|
visitor.visit_seq(&mut self.visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -537,6 +682,23 @@ impl<I, K, V, E> de::Deserializer for MapDeserializer<I, K, V, E>
|
|||||||
{
|
{
|
||||||
visitor.visit_map(self)
|
visitor.visit_map(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
|
impl<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
|
||||||
@@ -648,6 +810,23 @@ impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
|
|||||||
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
|
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
|
||||||
visitor.visit_map(&mut self.visitor)
|
visitor.visit_map(&mut self.visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -678,6 +857,23 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
|
|||||||
None => Err(de::Error::end_of_stream()),
|
None => Err(de::Error::end_of_stream()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -712,4 +908,21 @@ impl<E> de::Deserializer for ByteBufDeserializer<E>
|
|||||||
None => Err(de::Error::end_of_stream()),
|
None => Err(de::Error::end_of_stream()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
de_forward_to_deserialize!{
|
||||||
|
deserialize_bool,
|
||||||
|
deserialize_f64, deserialize_f32,
|
||||||
|
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
|
||||||
|
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
|
||||||
|
deserialize_char, deserialize_str, deserialize_string,
|
||||||
|
deserialize_ignored_any,
|
||||||
|
deserialize_bytes,
|
||||||
|
deserialize_unit_struct, deserialize_unit,
|
||||||
|
deserialize_seq, deserialize_seq_fixed_size,
|
||||||
|
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
|
||||||
|
deserialize_tuple,
|
||||||
|
deserialize_enum,
|
||||||
|
deserialize_struct, deserialize_tuple_struct,
|
||||||
|
deserialize_option
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
//! Implementations for all of Rust's builtin types.
|
//! Implementations for all of Rust's builtin types. Tuples implement the `Serialize` trait if they
|
||||||
|
//! have at most 16 fields. Arrays implement the `Serialize` trait if their length is 32 or less.
|
||||||
|
//! You can always forward array serialization to slice serialization, which works for any length.
|
||||||
|
//! Long tuples are best replaced by tuple structs, for which you can use `derive(Serialize)`. In
|
||||||
|
//! that case the number of fields is irrelevant.
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
@@ -518,7 +522,8 @@ macro_rules! serialize_map {
|
|||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
||||||
for (k, v) in self {
|
for (k, v) in self {
|
||||||
try!(serializer.serialize_map_elt(&mut state, k, v));
|
try!(serializer.serialize_map_key(&mut state, k));
|
||||||
|
try!(serializer.serialize_map_value(&mut state, v));
|
||||||
}
|
}
|
||||||
serializer.serialize_map_end(state)
|
serializer.serialize_map_end(state)
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-11
@@ -1,4 +1,14 @@
|
|||||||
//! Generic serialization framework.
|
//! Generic serialization framework.
|
||||||
|
//! # For Developers who want to serialize objects
|
||||||
|
//! Implement the `Serialize` trait for the type of objects you want to serialize. Call methods of
|
||||||
|
//! the `serializer` object. For which methods to call and how to do so, look at the documentation
|
||||||
|
//! of the `Serializer` trait.
|
||||||
|
//!
|
||||||
|
//! # For Serialization Format Developers
|
||||||
|
//! Implement the `Serializer` trait for a structure that contains fields that enable it to write
|
||||||
|
//! the serialization result to your target. When a method's argument is an object of type
|
||||||
|
//! `Serialize`, you can either forward the serializer object (`self`) or create a new one,
|
||||||
|
//! depending on the quirks of your format.
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::error;
|
use std::error;
|
||||||
@@ -15,11 +25,11 @@ pub mod impls;
|
|||||||
/// `Error` is a trait that allows a `Serialize` to generically create a
|
/// `Error` is a trait that allows a `Serialize` to generically create a
|
||||||
/// `Serializer` error.
|
/// `Serializer` error.
|
||||||
pub trait Error: Sized + error::Error {
|
pub trait Error: Sized + error::Error {
|
||||||
/// Raised when there is general error when deserializing a type.
|
/// Raised when there is a general error when serializing a type.
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn custom<T: Into<String>>(msg: T) -> Self;
|
fn custom<T: Into<String>>(msg: T) -> Self;
|
||||||
|
|
||||||
/// Raised when there is general error when deserializing a type.
|
/// Raised when there is a general error when serializing a type.
|
||||||
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
fn custom<T: Into<&'static str>>(msg: T) -> Self;
|
fn custom<T: Into<&'static str>>(msg: T) -> Self;
|
||||||
|
|
||||||
@@ -41,6 +51,26 @@ pub trait Serialize {
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A trait that describes a type that can serialize a stream of values into the underlying format.
|
/// A trait that describes a type that can serialize a stream of values into the underlying format.
|
||||||
|
///
|
||||||
|
/// # For `Serialize` Developers
|
||||||
|
/// Non-aggrergate types like integers and strings can be serialized directly by calling the
|
||||||
|
/// appropriate function. For Aggregate types there's an initial `serialize_T` method that yields
|
||||||
|
/// a State object that you should not interact with. For each part of the aggregate there's a
|
||||||
|
/// `serialize_T_elt` method that allows you to pass values or key/value pairs. The types of the
|
||||||
|
/// values or the keys may change between calls, but the serialization format may not necessarily
|
||||||
|
/// accept it. The `serialize_T_elt` method also takes a mutable reference to the state object.
|
||||||
|
/// Make sure that you always use the same state object and only the state object that was returned
|
||||||
|
/// by the `serialize_T` method. Finally, when your object is done, call the `serialize_T_end`
|
||||||
|
/// method and pass the state object by value
|
||||||
|
///
|
||||||
|
/// # For Serialization Format Developers
|
||||||
|
/// If your format has different situations where it accepts different types, create a
|
||||||
|
/// `Serializer` for each situation. You can create the sub-`Serializer` in one of the aggregate
|
||||||
|
/// `serialize_T` methods and return it as a state object. Remember to also set the corresponding
|
||||||
|
/// associated type `TState`. In the `serialize_T_elt` methods you will be given a mutable
|
||||||
|
/// reference to that state. You do not need to do any additional checks for the correctness of the
|
||||||
|
/// state object, as it is expected that the user will not modify it. Due to the generic nature
|
||||||
|
/// of the `Serialize` impls, modifying the object is impossible on stable Rust.
|
||||||
pub trait Serializer {
|
pub trait Serializer {
|
||||||
/// The error type that can be returned if some error occurs during serialization.
|
/// The error type that can be returned if some error occurs during serialization.
|
||||||
type Error: Error;
|
type Error: Error;
|
||||||
@@ -131,7 +161,8 @@ pub trait Serializer {
|
|||||||
/// Serializes an `f64` value.
|
/// Serializes an `f64` value.
|
||||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Serializes a character.
|
/// Serializes a character. If the format does not support characters,
|
||||||
|
/// it is reasonable to serialize it as a single element `str` or a `u32`.
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error>;
|
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `&str`.
|
/// Serializes a `&str`.
|
||||||
@@ -140,10 +171,17 @@ pub trait Serializer {
|
|||||||
/// Enables serializers to serialize byte slices more compactly or more
|
/// Enables serializers to serialize byte slices more compactly or more
|
||||||
/// efficiently than other types of slices. If no efficient implementation
|
/// efficiently than other types of slices. If no efficient implementation
|
||||||
/// is available, a reasonable implementation would be to forward to
|
/// is available, a reasonable implementation would be to forward to
|
||||||
/// `serialize_seq`.
|
/// `serialize_seq`. If forwarded, the implementation looks usually just like this:
|
||||||
|
/// ```rust
|
||||||
|
/// let mut state = try!(self.serialize_seq(value));
|
||||||
|
/// for b in value {
|
||||||
|
/// try!(self.serialize_seq_elt(&mut state, b));
|
||||||
|
/// }
|
||||||
|
/// self.serialize_seq_end(state)
|
||||||
|
/// ```
|
||||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>;
|
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `()` value.
|
/// Serializes a `()` value. It's reasonable to just not serialize anything.
|
||||||
fn serialize_unit(&mut self) -> Result<(), Self::Error>;
|
fn serialize_unit(&mut self) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Serializes a unit struct value. A reasonable implementation would be to
|
/// Serializes a unit struct value. A reasonable implementation would be to
|
||||||
@@ -166,7 +204,7 @@ pub trait Serializer {
|
|||||||
/// Allows a tuple struct with a single element, also known as a newtype
|
/// Allows a tuple struct with a single element, also known as a newtype
|
||||||
/// struct, to be more efficiently serialized than a tuple struct with
|
/// struct, to be more efficiently serialized than a tuple struct with
|
||||||
/// multiple items. A reasonable implementation would be to forward to
|
/// multiple items. A reasonable implementation would be to forward to
|
||||||
/// `serialize_tuple_struct`.
|
/// `serialize_tuple_struct` or to just serialize the inner value without wrapping.
|
||||||
fn serialize_newtype_struct<T: Serialize>(
|
fn serialize_newtype_struct<T: Serialize>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
@@ -296,18 +334,25 @@ pub trait Serializer {
|
|||||||
) -> Result<(), Self::Error>;
|
) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Begins to serialize a map. This call must be followed by zero or more
|
/// Begins to serialize a map. This call must be followed by zero or more
|
||||||
/// calls to `serialize_map_elt`, then a call to `serialize_map_end`.
|
/// calls to `serialize_map_key` and `serialize_map_value`, then a call to
|
||||||
|
/// `serialize_map_end`.
|
||||||
fn serialize_map(
|
fn serialize_map(
|
||||||
&mut self,
|
&mut self,
|
||||||
len: Option<usize>,
|
len: Option<usize>,
|
||||||
) -> Result<Self::MapState, Self::Error>;
|
) -> Result<Self::MapState, Self::Error>;
|
||||||
|
|
||||||
/// Serialize a map element. Must have previously called `serialize_map`.
|
/// Serialize a map key. Must have previously called `serialize_map`.
|
||||||
fn serialize_map_elt<K: Serialize, V: Serialize>(
|
fn serialize_map_key<T: Serialize>(
|
||||||
&mut self,
|
&mut self,
|
||||||
state: &mut Self::MapState,
|
state: &mut Self::MapState,
|
||||||
key: K,
|
key: T
|
||||||
value: V,
|
) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Serialize a map value. Must have previously called `serialize_map`.
|
||||||
|
fn serialize_map_value<T: Serialize>(
|
||||||
|
&mut self,
|
||||||
|
state: &mut Self::MapState,
|
||||||
|
value: T
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
/// Finishes serializing a map.
|
/// Finishes serializing a map.
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen"
|
name = "serde_codegen"
|
||||||
# DO NOT RELEASE ANY MORE 0.7 RELEASES FROM THIS BRANCH
|
version = "0.8.0"
|
||||||
# USE THE 0.7.x BRANCH
|
|
||||||
version = "0.8.0-rc2"
|
|
||||||
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"
|
||||||
@@ -26,14 +24,14 @@ with-syntex = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
quasi_codegen = { version = "^0.15.0", optional = true }
|
quasi_codegen = { version = "^0.16.0", optional = true }
|
||||||
syntex = { version = "^0.38.0", optional = true }
|
syntex = { version = "^0.39.0", optional = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aster = { version = "^0.21.1", default-features = false }
|
aster = { version = "^0.22.0", default-features = false }
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
quasi = { version = "^0.15.0", default-features = false }
|
quasi = { version = "^0.16.0", default-features = false }
|
||||||
quasi_macros = { version = "^0.15.0", optional = true }
|
quasi_macros = { version = "^0.16.0", optional = true }
|
||||||
serde_codegen_internals = { version = "0.4.0-rc1", default-features = false }
|
serde_codegen_internals = { version = "0.5.0", default-features = false }
|
||||||
syntex = { version = "^0.38.0", optional = true }
|
syntex = { version = "^0.39.0", optional = true }
|
||||||
syntex_syntax = { version = "^0.38.0", optional = true }
|
syntex_syntax = { version = "^0.39.0", optional = true }
|
||||||
|
|||||||
+63
-86
@@ -1,7 +1,8 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use aster::AstBuilder;
|
use aster::AstBuilder;
|
||||||
|
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::ptr::P;
|
|
||||||
use syntax::visit;
|
use syntax::visit;
|
||||||
|
|
||||||
use internals::ast::Item;
|
use internals::ast::Item;
|
||||||
@@ -47,6 +48,17 @@ pub fn with_where_predicates_from_fields<F>(
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Puts the given bound on any generic type parameters that are used in fields
|
||||||
|
// for which filter returns true.
|
||||||
|
//
|
||||||
|
// For example, the following struct needs the bound `A: Serialize, B: Serialize`.
|
||||||
|
//
|
||||||
|
// struct S<'b, A, B: 'b, C> {
|
||||||
|
// a: A,
|
||||||
|
// b: Option<&'b B>
|
||||||
|
// #[serde(skip_serializing)]
|
||||||
|
// c: C,
|
||||||
|
// }
|
||||||
pub fn with_bound<F>(
|
pub fn with_bound<F>(
|
||||||
builder: &AstBuilder,
|
builder: &AstBuilder,
|
||||||
item: &Item,
|
item: &Item,
|
||||||
@@ -56,95 +68,60 @@ pub fn with_bound<F>(
|
|||||||
) -> ast::Generics
|
) -> ast::Generics
|
||||||
where F: Fn(&attr::Field) -> bool,
|
where F: Fn(&attr::Field) -> bool,
|
||||||
{
|
{
|
||||||
|
struct FindTyParams {
|
||||||
|
// Set of all generic type parameters on the current struct (A, B, C in
|
||||||
|
// the example). Initialized up front.
|
||||||
|
all_ty_params: HashSet<ast::Name>,
|
||||||
|
// Set of generic type parameters used in fields for which filter
|
||||||
|
// returns true (A and B in the example). Filled in as the visitor sees
|
||||||
|
// them.
|
||||||
|
relevant_ty_params: HashSet<ast::Name>,
|
||||||
|
}
|
||||||
|
impl visit::Visitor for FindTyParams {
|
||||||
|
fn visit_path(&mut self, path: &ast::Path, _id: ast::NodeId) {
|
||||||
|
if let Some(seg) = path.segments.last() {
|
||||||
|
if seg.identifier.name.as_str() == "PhantomData" {
|
||||||
|
// Hardcoded exception, because PhantomData<T> implements
|
||||||
|
// Serialize and Deserialize whether or not T implements it.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !path.global && path.segments.len() == 1 {
|
||||||
|
let id = path.segments[0].identifier.name;
|
||||||
|
if self.all_ty_params.contains(&id) {
|
||||||
|
self.relevant_ty_params.insert(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
visit::walk_path(self, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let all_ty_params: HashSet<_> = generics.ty_params.iter()
|
||||||
|
.map(|ty_param| ty_param.ident.name)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let relevant_tys = item.body.all_fields()
|
||||||
|
.filter(|&field| filter(&field.attrs))
|
||||||
|
.map(|field| &field.ty);
|
||||||
|
|
||||||
|
let mut visitor = FindTyParams {
|
||||||
|
all_ty_params: all_ty_params,
|
||||||
|
relevant_ty_params: HashSet::new(),
|
||||||
|
};
|
||||||
|
for ty in relevant_tys {
|
||||||
|
visit::walk_ty(&mut visitor, ty);
|
||||||
|
}
|
||||||
|
|
||||||
builder.from_generics(generics.clone())
|
builder.from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
item.body.all_fields()
|
generics.ty_params.iter()
|
||||||
.filter(|&field| filter(&field.attrs))
|
.map(|ty_param| ty_param.ident.name)
|
||||||
.map(|field| &field.ty)
|
.filter(|id| visitor.relevant_ty_params.contains(id))
|
||||||
.filter(|ty| !contains_recursion(ty, item.ident))
|
.map(|id| builder.where_predicate()
|
||||||
.map(|ty| strip_reference(ty))
|
// the type parameter that is being bounded e.g. T
|
||||||
.map(|ty| builder.where_predicate()
|
.bound().build(builder.ty().id(id))
|
||||||
// the type that is being bounded e.g. T
|
|
||||||
.bound().build(ty.clone())
|
|
||||||
// the bound e.g. Serialize
|
// the bound e.g. Serialize
|
||||||
.bound().trait_(bound.clone()).build()
|
.bound().trait_(bound.clone()).build()
|
||||||
.build()))
|
.build()))
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
// We do not attempt to generate any bounds based on field types that are
|
|
||||||
// directly recursive, as in:
|
|
||||||
//
|
|
||||||
// struct Test<D> {
|
|
||||||
// next: Box<Test<D>>,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// This does not catch field types that are mutually recursive with some other
|
|
||||||
// type. For those, we require bounds to be specified by a `bound` attribute if
|
|
||||||
// the inferred ones are not correct.
|
|
||||||
//
|
|
||||||
// struct Test<D> {
|
|
||||||
// #[serde(bound="D: Serialize + Deserialize")]
|
|
||||||
// next: Box<Other<D>>,
|
|
||||||
// }
|
|
||||||
// struct Other<D> {
|
|
||||||
// #[serde(bound="D: Serialize + Deserialize")]
|
|
||||||
// next: Box<Test<D>>,
|
|
||||||
// }
|
|
||||||
fn contains_recursion(ty: &ast::Ty, ident: ast::Ident) -> bool {
|
|
||||||
struct FindRecursion {
|
|
||||||
ident: ast::Ident,
|
|
||||||
found_recursion: bool,
|
|
||||||
}
|
|
||||||
impl visit::Visitor for FindRecursion {
|
|
||||||
fn visit_path(&mut self, path: &ast::Path, _id: ast::NodeId) {
|
|
||||||
if !path.global
|
|
||||||
&& path.segments.len() == 1
|
|
||||||
&& path.segments[0].identifier == self.ident {
|
|
||||||
self.found_recursion = true;
|
|
||||||
} else {
|
|
||||||
visit::walk_path(self, path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut visitor = FindRecursion {
|
|
||||||
ident: ident,
|
|
||||||
found_recursion: false,
|
|
||||||
};
|
|
||||||
visit::walk_ty(&mut visitor, ty);
|
|
||||||
visitor.found_recursion
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is required to handle types that use both a reference and a value of
|
|
||||||
// the same type, as in:
|
|
||||||
//
|
|
||||||
// enum Test<'a, T> where T: 'a {
|
|
||||||
// Lifetime(&'a T),
|
|
||||||
// NoLifetime(T),
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Preserving references, we would generate an impl like:
|
|
||||||
//
|
|
||||||
// impl<'a, T> Serialize for Test<'a, T>
|
|
||||||
// where &'a T: Serialize,
|
|
||||||
// T: Serialize { ... }
|
|
||||||
//
|
|
||||||
// And taking a reference to one of the elements would fail with:
|
|
||||||
//
|
|
||||||
// error: cannot infer an appropriate lifetime for pattern due
|
|
||||||
// to conflicting requirements [E0495]
|
|
||||||
// Test::NoLifetime(ref v) => { ... }
|
|
||||||
// ^~~~~
|
|
||||||
//
|
|
||||||
// Instead, we strip references before adding `T: Serialize` bounds in order to
|
|
||||||
// generate:
|
|
||||||
//
|
|
||||||
// impl<'a, T> Serialize for Test<'a, T>
|
|
||||||
// where T: Serialize { ... }
|
|
||||||
fn strip_reference(mut ty: &P<ast::Ty>) -> &P<ast::Ty> {
|
|
||||||
while let ast::TyKind::Rptr(_, ref mut_ty) = ty.node {
|
|
||||||
ty = &mut_ty.ty;
|
|
||||||
}
|
|
||||||
ty
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen_internals"
|
name = "serde_codegen_internals"
|
||||||
version = "0.4.0-rc1"
|
version = "0.5.0"
|
||||||
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 = "AST representation used by Serde codegen. Unstable."
|
description = "AST representation used by Serde codegen. Unstable."
|
||||||
@@ -16,5 +16,5 @@ with-syntex = ["syntex_syntax", "syntex_errors"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
syntex_syntax = { version = "^0.38.0", optional = true }
|
syntex_syntax = { version = "^0.39.0", optional = true }
|
||||||
syntex_errors = { version = "^0.38.0", optional = true }
|
syntex_errors = { version = "^0.39.0", optional = true }
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
# DO NOT RELEASE ANY MORE 0.7 RELEASES FROM THIS BRANCH
|
version = "0.8.0"
|
||||||
# USE THE 0.7.x BRANCH
|
|
||||||
version = "0.8.0-rc2"
|
|
||||||
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"
|
||||||
@@ -20,15 +18,15 @@ unstable-testing = ["clippy", "serde/unstable-testing", "serde_codegen/unstable-
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
serde_codegen = { version = "^0.8.0-rc2", default-features = false, features = ["unstable"] }
|
serde_codegen = { version = "0.8.0", default-features = false, features = ["unstable"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
clippy = "^0.*"
|
clippy = "^0.*"
|
||||||
compiletest_rs = "^0.2.0"
|
compiletest_rs = "^0.2.0"
|
||||||
fnv = "1.0"
|
fnv = "1.0"
|
||||||
rustc-serialize = "^0.3.16"
|
rustc-serialize = "^0.3.16"
|
||||||
serde = "0.8.0-rc2"
|
serde = "0.8.0"
|
||||||
serde_test = "0.8.0-rc2"
|
serde_test = "0.8.0"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "test"
|
name = "test"
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#![feature(test, custom_attribute, custom_derive, plugin)]
|
#![feature(test, custom_attribute, custom_derive, plugin)]
|
||||||
#![plugin(serde_macros)]
|
#![plugin(serde_macros)]
|
||||||
|
|
||||||
extern crate serde;
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
include!("../../testing/tests/test.rs.in");
|
include!("../../testing/tests/test.rs.in");
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_test"
|
name = "serde_test"
|
||||||
# DO NOT RELEASE ANY MORE 0.7 RELEASES FROM THIS BRANCH
|
version = "0.8.0"
|
||||||
# USE THE 0.7.x BRANCH
|
|
||||||
version = "0.8.0-rc2"
|
|
||||||
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 = "Token De/Serializer for testing De/Serialize implementations"
|
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||||
@@ -13,4 +11,4 @@ keywords = ["serde", "serialization"]
|
|||||||
include = ["Cargo.toml", "src/**/*.rs"]
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = "0.8.0-rc2"
|
serde = "0.8.0"
|
||||||
|
|||||||
+90
-1
@@ -110,6 +110,95 @@ impl<I> de::Deserializer for Deserializer<I>
|
|||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
|
fn deserialize_seq<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_struct_field<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_map<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_unit<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_bytes<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_ignored_any<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_string<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_str<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_char<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_f32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_f64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_bool<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_usize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_isize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||||
where V: Visitor,
|
where V: Visitor,
|
||||||
{
|
{
|
||||||
@@ -241,7 +330,7 @@ impl<I> de::Deserializer for Deserializer<I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_fixed_size_array<V>(&mut self,
|
fn deserialize_seq_fixed_size<V>(&mut self,
|
||||||
len: usize,
|
len: usize,
|
||||||
visitor: V) -> Result<V::Value, Error>
|
visitor: V) -> Result<V::Value, Error>
|
||||||
where V: Visitor,
|
where V: Visitor,
|
||||||
|
|||||||
@@ -259,9 +259,12 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map_elt<K, V>(&mut self, _: &mut (), key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
fn serialize_map_key<T>(&mut self, _: &mut (), key: T) -> Result<(), Self::Error> where T: Serialize {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||||
try!(key.serialize(self));
|
key.serialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_map_value<T>(&mut self, _: &mut (), value: T) -> Result<(), Self::Error> where T: Serialize {
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_testing"
|
name = "serde_testing"
|
||||||
version = "0.8.0-rc2"
|
version = "0.8.0"
|
||||||
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"
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate serde;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
||||||
|
|||||||
+146
-23
@@ -1,20 +1,21 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
extern crate serde;
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
use self::serde::Serialize;
|
use serde::bytes::{ByteBuf, Bytes};
|
||||||
use self::serde::bytes::{ByteBuf, Bytes};
|
use serde::ser;
|
||||||
|
use serde::de;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
struct Error;
|
struct Error;
|
||||||
|
|
||||||
impl serde::ser::Error for Error {
|
impl ser::Error for Error {
|
||||||
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::de::Error for Error {
|
impl de::Error for Error {
|
||||||
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
||||||
|
|
||||||
fn end_of_stream() -> Error { Error }
|
fn end_of_stream() -> Error { Error }
|
||||||
@@ -50,7 +51,7 @@ impl BytesSerializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::Serializer for BytesSerializer {
|
impl Serializer for BytesSerializer {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type SeqState = ();
|
type SeqState = ();
|
||||||
type MapState = ();
|
type MapState = ();
|
||||||
@@ -137,19 +138,19 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<V>(&mut self, _value: V) -> Result<(), Error>
|
fn serialize_some<V>(&mut self, _value: V) -> Result<(), Error>
|
||||||
where V: serde::Serialize,
|
where V: Serialize,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<V>(&mut self, _: &'static str, _value: V) -> Result<(), Error>
|
fn serialize_newtype_struct<V>(&mut self, _: &'static str, _value: V) -> Result<(), Error>
|
||||||
where V: serde::Serialize,
|
where V: Serialize,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<V>(&mut self, _: &'static str, _: usize, _: &'static str, _value: V) -> Result<(), Error>
|
fn serialize_newtype_variant<V>(&mut self, _: &'static str, _: usize, _: &'static str, _value: V) -> Result<(), Error>
|
||||||
where V: serde::Serialize,
|
where V: Serialize,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -165,7 +166,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_seq_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
fn serialize_seq_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
||||||
where T: serde::Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -181,7 +182,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
fn serialize_tuple_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
||||||
where T: serde::Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -197,7 +198,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
fn serialize_tuple_struct_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
||||||
where T: serde::Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -213,7 +214,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
fn serialize_tuple_variant_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
||||||
where T: serde::Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -228,9 +229,14 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map_elt<K, V>(&mut self, _: &mut (), _key: K, _value: V) -> Result<(), Error>
|
fn serialize_map_key<T>(&mut self, _: &mut (), _key: T) -> Result<(), Error>
|
||||||
where K: serde::Serialize,
|
where T: Serialize
|
||||||
V: serde::Serialize,
|
{
|
||||||
|
Err(Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_map_value<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -246,7 +252,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
fn serialize_struct_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
||||||
where V: serde::Serialize,
|
where V: Serialize,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -262,7 +268,7 @@ impl serde::Serializer for BytesSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
fn serialize_struct_variant_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
||||||
where V: serde::Serialize,
|
where V: Serialize,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
@@ -292,20 +298,137 @@ impl BytesDeserializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::Deserializer for BytesDeserializer {
|
impl Deserializer for BytesDeserializer {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn deserialize<V>(&mut self, _visitor: V) -> Result<V::Value, Error>
|
fn deserialize<V>(&mut self, _visitor: V) -> Result<V::Value, Error>
|
||||||
where V: serde::de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
Err(Error)
|
Err(Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_bytes<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
fn deserialize_bytes<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||||
where V: serde::de::Visitor,
|
where V: de::Visitor,
|
||||||
{
|
{
|
||||||
visitor.visit_byte_buf(self.bytes.take().unwrap())
|
visitor.visit_byte_buf(self.bytes.take().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deserialize_seq<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_struct_field<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_map<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_unit<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_ignored_any<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_string<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_str<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_char<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_i8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_u8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_f32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_f64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_bool<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_usize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_isize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
|
||||||
|
where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_option<__V>(&mut self, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_seq_fixed_size<__V>(&mut self, _: usize, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_unit_struct<__V>(&mut self, _: &str, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_newtype_struct<__V>(&mut self, _: &str, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_tuple_struct<__V>(&mut self, _: &str, _: usize, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_struct<__V>(&mut self, _: &str, _: &[&str], visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_tuple<__V>(&mut self, _: usize, visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::Visitor {
|
||||||
|
self.deserialize(visitor)
|
||||||
|
}
|
||||||
|
fn deserialize_enum<__V>(&mut self, _: &str, _: &[&str], _visitor: __V)
|
||||||
|
-> Result<__V::Value, Self::Error> where __V: de::EnumVisitor {
|
||||||
|
Err(Error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -328,10 +451,10 @@ fn test_bytes_ser_bytes() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_byte_buf_de_bytes() {
|
fn test_byte_buf_de_bytes() {
|
||||||
let mut de = BytesDeserializer::new(vec![]);
|
let mut de = BytesDeserializer::new(vec![]);
|
||||||
let bytes = serde::Deserialize::deserialize(&mut de);
|
let bytes = Deserialize::deserialize(&mut de);
|
||||||
assert_eq!(bytes, Ok(ByteBuf::new()));
|
assert_eq!(bytes, Ok(ByteBuf::new()));
|
||||||
|
|
||||||
let mut de = BytesDeserializer::new(vec![1, 2, 3]);
|
let mut de = BytesDeserializer::new(vec![1, 2, 3]);
|
||||||
let bytes = serde::Deserialize::deserialize(&mut de);
|
let bytes = Deserialize::deserialize(&mut de);
|
||||||
assert_eq!(bytes, Ok(ByteBuf::from(vec![1, 2, 3])));
|
assert_eq!(bytes, Ok(ByteBuf::from(vec![1, 2, 3])));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
|||||||
use std::net;
|
use std::net;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
extern crate serde;
|
use serde::Deserialize;
|
||||||
use self::serde::Deserialize;
|
|
||||||
|
|
||||||
extern crate fnv;
|
extern crate fnv;
|
||||||
use self::fnv::FnvHasher;
|
use self::fnv::FnvHasher;
|
||||||
|
|||||||
@@ -6,14 +6,19 @@ extern crate serde;
|
|||||||
use self::serde::ser::{Serialize, Serializer};
|
use self::serde::ser::{Serialize, Serializer};
|
||||||
use self::serde::de::{Deserialize, Deserializer};
|
use self::serde::de::{Deserialize, Deserializer};
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gen() {
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct With<T> {
|
struct With<T> {
|
||||||
t: T,
|
t: T,
|
||||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
x: X,
|
x: X,
|
||||||
}
|
}
|
||||||
|
assert::<With<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct WithRef<'a, T: 'a> {
|
struct WithRef<'a, T: 'a> {
|
||||||
@@ -22,6 +27,19 @@ struct WithRef<'a, T: 'a> {
|
|||||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
x: X,
|
x: X,
|
||||||
}
|
}
|
||||||
|
assert::<WithRef<i32>>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct PhantomX {
|
||||||
|
x: PhantomData<X>,
|
||||||
|
}
|
||||||
|
assert::<PhantomX>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct PhantomT<T> {
|
||||||
|
t: PhantomData<T>,
|
||||||
|
}
|
||||||
|
assert::<PhantomT<X>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Bounds<T: Serialize + Deserialize> {
|
struct Bounds<T: Serialize + Deserialize> {
|
||||||
@@ -30,6 +48,7 @@ struct Bounds<T: Serialize + Deserialize> {
|
|||||||
boxed: Box<T>,
|
boxed: Box<T>,
|
||||||
option_boxed: Option<Box<T>>,
|
option_boxed: Option<Box<T>>,
|
||||||
}
|
}
|
||||||
|
assert::<Bounds<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct NoBounds<T> {
|
struct NoBounds<T> {
|
||||||
@@ -38,6 +57,7 @@ struct NoBounds<T> {
|
|||||||
boxed: Box<T>,
|
boxed: Box<T>,
|
||||||
option_boxed: Option<Box<T>>,
|
option_boxed: Option<Box<T>>,
|
||||||
}
|
}
|
||||||
|
assert::<NoBounds<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
enum EnumWith<T> {
|
enum EnumWith<T> {
|
||||||
@@ -54,18 +74,21 @@ enum EnumWith<T> {
|
|||||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
x: X },
|
x: X },
|
||||||
}
|
}
|
||||||
|
assert::<EnumWith<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
||||||
t: T,
|
t: T,
|
||||||
rrrt: &'a &'b &'c T,
|
rrrt: &'a &'b &'c T,
|
||||||
}
|
}
|
||||||
|
assert_ser::<MultipleRef<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Newtype(
|
struct Newtype(
|
||||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
X
|
X
|
||||||
);
|
);
|
||||||
|
assert::<Newtype>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Tuple<T>(
|
struct Tuple<T>(
|
||||||
@@ -73,6 +96,7 @@ struct Tuple<T>(
|
|||||||
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
X,
|
X,
|
||||||
);
|
);
|
||||||
|
assert::<Tuple<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
enum TreeNode<D> {
|
enum TreeNode<D> {
|
||||||
@@ -84,12 +108,47 @@ enum TreeNode<D> {
|
|||||||
data: D,
|
data: D,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
assert::<TreeNode<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ListNode<D> {
|
struct ListNode<D> {
|
||||||
data: D,
|
data: D,
|
||||||
next: Box<ListNode<D>>,
|
next: Box<ListNode<D>>,
|
||||||
}
|
}
|
||||||
|
assert::<ListNode<i32>>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct RecursiveA {
|
||||||
|
b: Box<RecursiveB>,
|
||||||
|
}
|
||||||
|
assert::<RecursiveA>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
enum RecursiveB {
|
||||||
|
A(RecursiveA),
|
||||||
|
}
|
||||||
|
assert::<RecursiveB>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct RecursiveGenericA<T> {
|
||||||
|
t: T,
|
||||||
|
b: Box<RecursiveGenericB<T>>,
|
||||||
|
}
|
||||||
|
assert::<RecursiveGenericA<i32>>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
enum RecursiveGenericB<T> {
|
||||||
|
T(T),
|
||||||
|
A(RecursiveGenericA<T>),
|
||||||
|
}
|
||||||
|
assert::<RecursiveGenericB<i32>>();
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct OptionStatic<'a> {
|
||||||
|
a: Option<&'a str>,
|
||||||
|
b: Option<&'static str>,
|
||||||
|
}
|
||||||
|
assert_ser::<OptionStatic>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(bound="D: SerializeWith + DeserializeWith")]
|
#[serde(bound="D: SerializeWith + DeserializeWith")]
|
||||||
@@ -102,6 +161,7 @@ struct WithTraits1<D, E> {
|
|||||||
bound="E: SerializeWith + DeserializeWith")]
|
bound="E: SerializeWith + DeserializeWith")]
|
||||||
e: E,
|
e: E,
|
||||||
}
|
}
|
||||||
|
assert::<WithTraits1<X, X>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(bound(serialize="D: SerializeWith",
|
#[serde(bound(serialize="D: SerializeWith",
|
||||||
@@ -116,9 +176,14 @@ struct WithTraits2<D, E> {
|
|||||||
bound(deserialize="E: DeserializeWith"))]
|
bound(deserialize="E: DeserializeWith"))]
|
||||||
e: E,
|
e: E,
|
||||||
}
|
}
|
||||||
|
assert::<WithTraits2<X, X>>();
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
fn assert<T: Serialize + Deserialize>() {}
|
||||||
|
fn assert_ser<T: Serialize>() {}
|
||||||
|
|
||||||
trait SerializeWith {
|
trait SerializeWith {
|
||||||
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> Result<(), S::Error>;
|
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> Result<(), S::Error>;
|
||||||
}
|
}
|
||||||
@@ -129,6 +194,23 @@ trait DeserializeWith: Sized {
|
|||||||
|
|
||||||
// Implements neither Serialize nor Deserialize
|
// Implements neither Serialize nor Deserialize
|
||||||
struct X;
|
struct X;
|
||||||
fn ser_x<S: Serializer>(_: &X, _: &mut S) -> Result<(), S::Error> { panic!() }
|
|
||||||
fn de_x<D: Deserializer>(_: &mut D) -> Result<X, D::Error> { panic!() }
|
|
||||||
|
|
||||||
|
fn ser_x<S: Serializer>(_: &X, _: &mut S) -> Result<(), S::Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn de_x<D: Deserializer>(_: &mut D) -> Result<X, D::Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerializeWith for X {
|
||||||
|
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> Result<(), S::Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeserializeWith for X {
|
||||||
|
fn deserialize_with<D: Deserializer>(_: &mut D) -> Result<Self, D::Error> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,23 +39,19 @@ struct DeNamedMap<A, B, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
enum SerEnum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
enum SerEnum<'a, B: 'a, C: 'a, D> where D: 'a {
|
||||||
Unit,
|
Unit,
|
||||||
Seq(
|
Seq(
|
||||||
i8,
|
i8,
|
||||||
B,
|
B,
|
||||||
&'a C,
|
&'a C,
|
||||||
//C::Type,
|
|
||||||
&'a mut D,
|
&'a mut D,
|
||||||
//<D as Trait>::Type,
|
|
||||||
),
|
),
|
||||||
Map {
|
Map {
|
||||||
a: i8,
|
a: i8,
|
||||||
b: B,
|
b: B,
|
||||||
c: &'a C,
|
c: &'a C,
|
||||||
//d: C::Type,
|
d: &'a mut D,
|
||||||
e: &'a mut D,
|
|
||||||
//f: <D as Trait>::Type,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Make sure we can support more than one variant.
|
// Make sure we can support more than one variant.
|
||||||
@@ -64,38 +60,30 @@ enum SerEnum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
|||||||
i8,
|
i8,
|
||||||
B,
|
B,
|
||||||
&'a C,
|
&'a C,
|
||||||
//C::Type,
|
|
||||||
&'a mut D,
|
&'a mut D,
|
||||||
//<D as Trait>::Type,
|
|
||||||
),
|
),
|
||||||
_Map2 {
|
_Map2 {
|
||||||
a: i8,
|
a: i8,
|
||||||
b: B,
|
b: B,
|
||||||
c: &'a C,
|
c: &'a C,
|
||||||
//d: C::Type,
|
d: &'a mut D,
|
||||||
e: &'a mut D,
|
|
||||||
//f: <D as Trait>::Type,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
enum DeEnum<B, C: /* Trait */, D> /* where D: Trait */ {
|
enum DeEnum<B, C, D> {
|
||||||
Unit,
|
Unit,
|
||||||
Seq(
|
Seq(
|
||||||
i8,
|
i8,
|
||||||
B,
|
B,
|
||||||
C,
|
C,
|
||||||
//C::Type,
|
|
||||||
D,
|
D,
|
||||||
//<D as Trait>::Type,
|
|
||||||
),
|
),
|
||||||
Map {
|
Map {
|
||||||
a: i8,
|
a: i8,
|
||||||
b: B,
|
b: B,
|
||||||
c: C,
|
c: C,
|
||||||
//d: C::Type,
|
d: D,
|
||||||
e: D,
|
|
||||||
//f: <D as Trait>::Type,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Make sure we can support more than one variant.
|
// Make sure we can support more than one variant.
|
||||||
@@ -104,17 +92,13 @@ enum DeEnum<B, C: /* Trait */, D> /* where D: Trait */ {
|
|||||||
i8,
|
i8,
|
||||||
B,
|
B,
|
||||||
C,
|
C,
|
||||||
//C::Type,
|
|
||||||
D,
|
D,
|
||||||
//<D as Trait>::Type,
|
|
||||||
),
|
),
|
||||||
_Map2 {
|
_Map2 {
|
||||||
a: i8,
|
a: i8,
|
||||||
b: B,
|
b: B,
|
||||||
c: C,
|
c: C,
|
||||||
//d: C::Type,
|
d: D,
|
||||||
e: D,
|
|
||||||
//f: <D as Trait>::Type,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,18 +285,14 @@ fn test_ser_enum_seq() {
|
|||||||
let a = 1;
|
let a = 1;
|
||||||
let b = 2;
|
let b = 2;
|
||||||
let c = 3;
|
let c = 3;
|
||||||
//let d = 4;
|
let mut d = 4;
|
||||||
let mut e = 5;
|
|
||||||
//let f = 6;
|
|
||||||
|
|
||||||
assert_ser_tokens(
|
assert_ser_tokens(
|
||||||
&SerEnum::Seq(
|
&SerEnum::Seq(
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
&c,
|
&c,
|
||||||
//d,
|
&mut d,
|
||||||
&mut e,
|
|
||||||
//f,
|
|
||||||
),
|
),
|
||||||
&[
|
&[
|
||||||
Token::EnumSeqStart("SerEnum", "Seq", 4),
|
Token::EnumSeqStart("SerEnum", "Seq", 4),
|
||||||
@@ -327,7 +307,7 @@ fn test_ser_enum_seq() {
|
|||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
Token::EnumSeqSep,
|
Token::EnumSeqSep,
|
||||||
Token::I32(5),
|
Token::I32(4),
|
||||||
|
|
||||||
Token::EnumSeqEnd,
|
Token::EnumSeqEnd,
|
||||||
],
|
],
|
||||||
@@ -339,18 +319,14 @@ fn test_ser_enum_map() {
|
|||||||
let a = 1;
|
let a = 1;
|
||||||
let b = 2;
|
let b = 2;
|
||||||
let c = 3;
|
let c = 3;
|
||||||
//let d = 4;
|
let mut d = 4;
|
||||||
let mut e = 5;
|
|
||||||
//let f = 6;
|
|
||||||
|
|
||||||
assert_ser_tokens(
|
assert_ser_tokens(
|
||||||
&SerEnum::Map {
|
&SerEnum::Map {
|
||||||
a: a,
|
a: a,
|
||||||
b: b,
|
b: b,
|
||||||
c: &c,
|
c: &c,
|
||||||
//d: d,
|
d: &mut d,
|
||||||
e: &mut e,
|
|
||||||
//f: f,
|
|
||||||
},
|
},
|
||||||
&[
|
&[
|
||||||
Token::EnumMapStart("SerEnum", "Map", 4),
|
Token::EnumMapStart("SerEnum", "Map", 4),
|
||||||
@@ -368,8 +344,8 @@ fn test_ser_enum_map() {
|
|||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
Token::EnumMapSep,
|
Token::EnumMapSep,
|
||||||
Token::Str("e"),
|
Token::Str("d"),
|
||||||
Token::I32(5),
|
Token::I32(4),
|
||||||
|
|
||||||
Token::EnumMapEnd,
|
Token::EnumMapEnd,
|
||||||
],
|
],
|
||||||
@@ -391,18 +367,14 @@ fn test_de_enum_seq() {
|
|||||||
let a = 1;
|
let a = 1;
|
||||||
let b = 2;
|
let b = 2;
|
||||||
let c = 3;
|
let c = 3;
|
||||||
//let d = 4;
|
let d = 4;
|
||||||
let e = 5;
|
|
||||||
//let f = 6;
|
|
||||||
|
|
||||||
assert_tokens(
|
assert_tokens(
|
||||||
&DeEnum::Seq(
|
&DeEnum::Seq(
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
c,
|
c,
|
||||||
//d,
|
d,
|
||||||
e,
|
|
||||||
//f,
|
|
||||||
),
|
),
|
||||||
&[
|
&[
|
||||||
Token::EnumSeqStart("DeEnum", "Seq", 4),
|
Token::EnumSeqStart("DeEnum", "Seq", 4),
|
||||||
@@ -417,7 +389,7 @@ fn test_de_enum_seq() {
|
|||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
Token::EnumSeqSep,
|
Token::EnumSeqSep,
|
||||||
Token::I32(5),
|
Token::I32(4),
|
||||||
|
|
||||||
Token::EnumSeqEnd,
|
Token::EnumSeqEnd,
|
||||||
],
|
],
|
||||||
@@ -429,18 +401,14 @@ fn test_de_enum_map() {
|
|||||||
let a = 1;
|
let a = 1;
|
||||||
let b = 2;
|
let b = 2;
|
||||||
let c = 3;
|
let c = 3;
|
||||||
//let d = 4;
|
let d = 4;
|
||||||
let e = 5;
|
|
||||||
//let f = 6;
|
|
||||||
|
|
||||||
assert_tokens(
|
assert_tokens(
|
||||||
&DeEnum::Map {
|
&DeEnum::Map {
|
||||||
a: a,
|
a: a,
|
||||||
b: b,
|
b: b,
|
||||||
c: c,
|
c: c,
|
||||||
//d: d,
|
d: d,
|
||||||
e: e,
|
|
||||||
//f: f,
|
|
||||||
},
|
},
|
||||||
&[
|
&[
|
||||||
Token::EnumMapStart("DeEnum", "Map", 4),
|
Token::EnumMapStart("DeEnum", "Map", 4),
|
||||||
@@ -458,8 +426,8 @@ fn test_de_enum_map() {
|
|||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
Token::EnumMapSep,
|
Token::EnumMapSep,
|
||||||
Token::Str("e"),
|
Token::Str("d"),
|
||||||
Token::I32(5),
|
Token::I32(4),
|
||||||
|
|
||||||
Token::EnumMapEnd,
|
Token::EnumMapEnd,
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user