mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-25 01:07:56 +00:00
Compare commits
94 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b5c0406afe | |||
| 96cd910c92 | |||
| e0bd57d63c | |||
| 7c784f592e | |||
| 2c69ccdca4 | |||
| 66eddd4d9b | |||
| 1485f0a448 | |||
| 379c9e7148 | |||
| 6c2af4da7a | |||
| 2ff7d003ee | |||
| ea182e2561 | |||
| 938f42faf6 | |||
| cc115ca43a | |||
| f1b4072444 | |||
| 16d3e96b77 | |||
| 7d2423e856 | |||
| 9865ec23c7 | |||
| 76a321db5c | |||
| 004dcaec3b | |||
| 74eb2f52b8 | |||
| 9b7317fbb1 | |||
| 41142d41ee | |||
| fd328c2f2a | |||
| 3ad276944a | |||
| bb20796e9d | |||
| 709ac64dfc | |||
| a9a4b2d8e2 | |||
| 7374ac499d | |||
| 6596f77e91 | |||
| eeb4efc19c | |||
| 76b70455ec | |||
| f43c8a6267 | |||
| ae806af644 | |||
| 7aa0b6ce27 | |||
| efdbf5795f | |||
| 55355b6680 | |||
| f8a91e5176 | |||
| aa0cd9b3dc | |||
| 1f82cd6e3d | |||
| 3caac4e6f3 | |||
| f4414bfc14 | |||
| 8378267b9b | |||
| 9c0140968d | |||
| 5716e8c508 | |||
| 0e9d45da60 | |||
| 6e7a75c859 | |||
| 0ff91e4451 | |||
| 305fab7c16 | |||
| a959073a81 | |||
| 2f0fc6e6f1 | |||
| 7bd87feb62 | |||
| bff2301ac3 | |||
| fd3c15fb68 | |||
| 808b06940e | |||
| 8cce6ecf15 | |||
| ef97f87b96 | |||
| 93a7568ff6 | |||
| 0439bb9d02 | |||
| 886670134a | |||
| 65e36647f5 | |||
| 51fdb0e4ef | |||
| a4de662adb | |||
| ff02b0c741 | |||
| 6b3958d5fc | |||
| 84b289dd7b | |||
| dbba537b66 | |||
| bc2324fba7 | |||
| 9082b75e75 | |||
| 4b9f751d74 | |||
| 451700d3d2 | |||
| 1c5d83889c | |||
| f659fa8919 | |||
| 87393b61bb | |||
| a84b6aaedd | |||
| ac98a25291 | |||
| 48dd47b2b7 | |||
| 4af850431c | |||
| 95c5e8681e | |||
| c7c5b50f35 | |||
| a8509a1d03 | |||
| d9b6feef19 | |||
| b526404707 | |||
| 9785646246 | |||
| 332b51f58a | |||
| fb18a5cc56 | |||
| b1cab411d6 | |||
| b01fc032fd | |||
| f771eea6e6 | |||
| 6b127cb8d0 | |||
| d0a63b451c | |||
| 8296ff4bad | |||
| eaff73a541 | |||
| 19ec8bbdb9 | |||
| 4d4b85318f |
+25
-28
@@ -1,43 +1,40 @@
|
|||||||
language: rust
|
language: rust
|
||||||
sudo: false
|
|
||||||
|
|
||||||
rust:
|
rust:
|
||||||
- stable
|
- stable
|
||||||
- beta
|
- beta
|
||||||
- nightly
|
- nightly
|
||||||
|
- 1.5.0
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
packages:
|
packages:
|
||||||
- libcurl4-openssl-dev
|
- libcurl4-openssl-dev
|
||||||
- libelf-dev
|
- libelf-dev
|
||||||
- libdw-dev
|
- libdw-dev
|
||||||
|
- binutils-dev
|
||||||
before_script:
|
before_script:
|
||||||
- |
|
- pip install 'travis-cargo<0.2' --user
|
||||||
pip install 'travis-cargo<0.2' --user &&
|
- export PATH=$HOME/.local/bin:$PATH
|
||||||
export PATH=$HOME/.local/bin:$PATH
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- |
|
- (cd serde && travis-cargo build)
|
||||||
(cd serde && travis-cargo build) &&
|
- (cd serde && travis-cargo test)
|
||||||
(cd serde && travis-cargo test) &&
|
- (cd serde && travis-cargo --only nightly test -- --features nightly-testing)
|
||||||
(cd serde && travis-cargo --only nightly test -- --features nightly-testing) &&
|
- (cd serde && travis-cargo --skip 1.5.0 build -- --no-default-features)
|
||||||
(cd serde_tests && travis-cargo test) &&
|
- (cd serde && travis-cargo --only nightly build -- --no-default-features)
|
||||||
(cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing) &&
|
- (cd serde && travis-cargo --only nightly build -- --no-default-features --features alloc)
|
||||||
(cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing) &&
|
- (cd serde && travis-cargo --only nightly build -- --no-default-features --features collections)
|
||||||
(cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing) &&
|
- (cd serde_tests && travis-cargo test)
|
||||||
(cd serde && travis-cargo --only stable doc) &&
|
- (cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing)
|
||||||
(cd serde_codegen && travis-cargo --only stable doc)
|
- (cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing)
|
||||||
|
- (cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing)
|
||||||
|
- (cd serde && travis-cargo --only stable doc)
|
||||||
|
- (cd serde_codegen && travis-cargo --only stable doc)
|
||||||
|
- (cd serde_macros && travis-cargo --only nightly doc)
|
||||||
after_success:
|
after_success:
|
||||||
- (cd serde && travis-cargo --only stable doc-upload)
|
- (cd serde && travis-cargo --only stable doc-upload)
|
||||||
|
#- (cd serde_codegen && travis-cargo --only stable doc-upload)
|
||||||
|
#- (cd serde_macros && travis-cargo --only nightly doc-upload)
|
||||||
- (cd serde_tests && travis-cargo coveralls --no-sudo)
|
- (cd serde_tests && travis-cargo coveralls --no-sudo)
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
# override the default `--features unstable` used for the nightly branch (optional)
|
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
|
||||||
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
|
- secure: Jcd11Jy0xLyacBUB+oKOaxKBm9iZNInenRDtNBY8GKOtqF5fHUfEjgDf538hwRl5L0FP7DLr8oK0IHmzA7lPjJxlzoKVKV3IM7bRZEYzW5DMonf/lcliuGte7SH0NVFhifM87T8HI2hjGdAb+7+m34siBR7M3AY/XjLInrvUFvY=
|
||||||
|
|
||||||
# encrypted github token for doc upload (see `GH_TOKEN` link above)
|
|
||||||
- secure: HO41LMpMXkF2In9+1sxWVu7fgolL+y9+4Q5PI6wZX2L5pDwpPJCjxaQarQXCEnoIxED1PlP03JuF7ULNz0zw1ylYhAOfOSdkxFZRnE2wMZqq6qvXBHwyMiDrAociIzoPKSGv7JVrKPsjsnd+96K6xxueIodQZrmAdyq7N/M82Mc=
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
## 0.7.6
|
||||||
|
|
||||||
|
NOTES:
|
||||||
|
|
||||||
|
* Syncs `serde_codegen` and `serde_macros` with rustc 1.10.0-nightly (7bddce693 2016-05-27).
|
||||||
|
|
||||||
|
FEATURES:
|
||||||
|
|
||||||
|
* `#[serde(serialize_with=..., deserialize_with=...)]` now supports tuples. #335
|
||||||
|
* Serde now can be used in `#[no_std]` environments. #316
|
||||||
@@ -4,6 +4,7 @@ Serde Rust Serialization Framework
|
|||||||
[](https://travis-ci.org/serde-rs/serde)
|
[](https://travis-ci.org/serde-rs/serde)
|
||||||
[](https://coveralls.io/github/serde-rs/serde?branch=master)
|
[](https://coveralls.io/github/serde-rs/serde?branch=master)
|
||||||
[](https://crates.io/crates/serde)
|
[](https://crates.io/crates/serde)
|
||||||
|
[](http://clippy.bashy.io/github/serde-rs/serde/master/log)
|
||||||
|
|
||||||
Serde is a powerful framework that enables serialization libraries to
|
Serde is a powerful framework that enables serialization libraries to
|
||||||
generically serialize Rust data structures without the overhead of runtime type
|
generically serialize Rust data structures without the overhead of runtime type
|
||||||
@@ -13,8 +14,7 @@ the same speed as a hand written serializer for a specific type.
|
|||||||
|
|
||||||
Documentation is available at:
|
Documentation is available at:
|
||||||
|
|
||||||
* [serde](https://serde-rs.github.io/serde/serde/serde/serde/index.html)
|
* [serde](https://serde-rs.github.io/serde/serde/index.html)
|
||||||
* [serde\_codegen](https://serde-rs.github.io/serde/serde/serde_codegen/serde_codegen/index.html)
|
|
||||||
|
|
||||||
Using Serde with Nightly Rust and serde\_macros
|
Using Serde with Nightly Rust and serde\_macros
|
||||||
===============================================
|
===============================================
|
||||||
@@ -46,7 +46,6 @@ serde_macros = "*"
|
|||||||
#![feature(custom_derive, plugin)]
|
#![feature(custom_derive, plugin)]
|
||||||
#![plugin(serde_macros)]
|
#![plugin(serde_macros)]
|
||||||
|
|
||||||
extern crate serde;
|
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -689,34 +688,55 @@ how types are serialized. Here are the supported annotations:
|
|||||||
|
|
||||||
Container Annotations:
|
Container Annotations:
|
||||||
|
|
||||||
| Annotation | Function |
|
| Annotation | Function |
|
||||||
| ---------- | -------- |
|
| ---------- | -------- |
|
||||||
| `#[serde(rename="name")` | Serialize and deserialize this container with the given name |
|
| `#[serde(rename="name")]` | Serialize and deserialize this container with the given name |
|
||||||
| `#[serde(rename(serialize="name1"))` | Serialize this container with the given name |
|
| `#[serde(rename(serialize="name1"))]` | Serialize this container with the given name |
|
||||||
| `#[serde(rename(deserialize="name1"))` | Deserialize this container with the given name |
|
| `#[serde(rename(deserialize="name1"))]` | Deserialize this container with the given name |
|
||||||
| `#[serde(deny_unknown_fields)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
|
| `#[serde(deny_unknown_fields)]` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
|
||||||
|
|
||||||
Variant Annotations:
|
Variant Annotations:
|
||||||
|
|
||||||
| Annotation | Function |
|
| Annotation | Function |
|
||||||
| ---------- | -------- |
|
| ---------- | -------- |
|
||||||
| `#[serde(rename="name")` | Serialize and deserialize this variant with the given name |
|
| `#[serde(rename="name")]` | Serialize and deserialize this variant with the given name |
|
||||||
| `#[serde(rename(serialize="name1"))` | Serialize this variant with the given name |
|
| `#[serde(rename(serialize="name1"))]` | Serialize this variant with the given name |
|
||||||
| `#[serde(rename(deserialize="name1"))` | Deserialize this variant with the given name |
|
| `#[serde(rename(deserialize="name1"))]` | Deserialize this variant with the given name |
|
||||||
|
|
||||||
Field Annotations:
|
Field Annotations:
|
||||||
|
|
||||||
| Annotation | Function |
|
| Annotation | Function |
|
||||||
| ---------- | -------- |
|
| ---------- | -------- |
|
||||||
| `#[serde(rename="name")` | Serialize and deserialize this field with the given name |
|
| `#[serde(rename="name")]` | Serialize and deserialize this field with the given name |
|
||||||
| `#[serde(rename(serialize="name1"))` | Serialize this field with the given name |
|
| `#[serde(rename(serialize="name1"))]` | Serialize this field with the given name |
|
||||||
| `#[serde(rename(deserialize="name1"))` | Deserialize this field with the given name |
|
| `#[serde(rename(deserialize="name1"))]` | Deserialize this field with the given name |
|
||||||
| `#[serde(default)` | If the value is not specified, use the `Default::default()` |
|
| `#[serde(default)]` | If the value is not specified, use the `Default::default()` |
|
||||||
| `#[serde(default="$path")` | Call the path to a function `fn() -> T` to build the value |
|
| `#[serde(default="$path")]` | Call the path to a function `fn() -> T` to build the value |
|
||||||
| `#[serde(skip_serializing)` | Do not serialize this value |
|
| `#[serde(skip_serializing)]` | Do not serialize this value |
|
||||||
| `#[serde(skip_serializing_if="$path")` | Do not serialize this value if this function `fn(&T) -> bool` returns `false` |
|
| `#[serde(skip_deserializing)]` | Always use `Default::default()` or `#[serde(default="$path")]` instead of deserializing this value |
|
||||||
| `#[serde(serialize_with="$path")` | Call a function `fn<T, S>(&T, &mut S) -> Result<(), S::Error> where S: Serializer` to serialize this value |
|
| `#[serde(skip_serializing_if="$path")]` | Do not serialize this value if this function `fn(&T) -> bool` returns `true` |
|
||||||
| `#[serde(deserialize_with="$path")` | Call a function `fn<T, D>(&mut D) -> Result<T, D::Error> where D: Deserializer` to deserialize this value |
|
| `#[serde(serialize_with="$path")]` | Call a function `fn<S>(&T, &mut S) -> Result<(), S::Error> where S: Serializer` to serialize this value of type `T` |
|
||||||
|
| `#[serde(deserialize_with="$path")]` | Call a function `fn<D>(&mut D) -> Result<T, D::Error> where D: Deserializer` to deserialize this value of type `T` |
|
||||||
|
|
||||||
|
Using in `no_std` crates
|
||||||
|
========================
|
||||||
|
|
||||||
|
The core `serde` package defines a number of features to enable usage in a
|
||||||
|
variety of freestanding environments. Enable any or none of the following
|
||||||
|
features, and use `default-features = false` in your `Cargo.toml`:
|
||||||
|
|
||||||
|
- `alloc` (implies `nightly`)
|
||||||
|
- `collections` (implies `alloc` and `nightly`)
|
||||||
|
- `std` (default)
|
||||||
|
|
||||||
|
If you only use `default-features = false`, you will receive a stock `no_std`
|
||||||
|
serde with no support for any of the collection types.
|
||||||
|
|
||||||
|
Upgrading from Serde 0.6
|
||||||
|
========================
|
||||||
|
|
||||||
|
* `#[serde(skip_serializing_if_none)]` was replaced with `#[serde(skip_serializing_if="Option::is_none")]`.
|
||||||
|
* `#[serde(skip_serializing_if_empty)]` was replaced with `#[serde(skip_serializing_if="Vec::is_empty")]`.
|
||||||
|
|
||||||
Serialization Formats Using Serde
|
Serialization Formats Using Serde
|
||||||
=================================
|
=================================
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ default = ["serde_codegen"]
|
|||||||
nightly = ["serde_macros"]
|
nightly = ["serde_macros"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
serde_codegen = { version = "^0.6.4", optional = true }
|
serde_codegen = { version = "^0.7.5", optional = true }
|
||||||
syntex = "^0.22.0"
|
syntex = "^0.32.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = "^0.6.1"
|
serde = "^0.7.5"
|
||||||
serde_json = "^0.6.0"
|
serde_json = "^0.7.0"
|
||||||
serde_macros = { version = "^0.6.1", optional = true }
|
serde_macros = { version = "^0.7.5", optional = true }
|
||||||
|
|||||||
+9
-3
@@ -1,17 +1,23 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "0.7.0"
|
version = "0.7.7"
|
||||||
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"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
documentation = "https://serde-rs.github.io/serde/serde/serde/index.html"
|
documentation = "https://serde-rs.github.io/serde/serde/"
|
||||||
readme = "../README.md"
|
readme = "../README.md"
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
|
||||||
|
std = []
|
||||||
nightly = []
|
nightly = []
|
||||||
nightly-testing = ["clippy", "nightly"]
|
alloc = ["nightly"]
|
||||||
|
collections = ["alloc"]
|
||||||
|
nightly-testing = ["clippy", "nightly", "std"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
|
|||||||
+159
-135
@@ -1,11 +1,15 @@
|
|||||||
//! Helper module to enable serializing bytes more efficiently
|
//! Helper module to enable serializing bytes more efficiently
|
||||||
|
|
||||||
use std::ops;
|
use core::{ops, fmt, char, iter, slice};
|
||||||
use std::fmt;
|
use core::fmt::Write;
|
||||||
use std::ascii;
|
|
||||||
|
|
||||||
use ser;
|
use ser;
|
||||||
use de;
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
|
pub use self::bytebuf::{ByteBuf, ByteBufVisitor};
|
||||||
|
|
||||||
|
#[cfg(feature = "collections")]
|
||||||
|
use collections::Vec;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -17,7 +21,11 @@ pub struct Bytes<'a> {
|
|||||||
|
|
||||||
impl<'a> fmt::Debug for Bytes<'a> {
|
impl<'a> fmt::Debug for Bytes<'a> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "b\"{}\"", escape_bytestring(self.bytes))
|
try!(f.write_str("b\""));
|
||||||
|
for c in escape_bytestring(self.bytes) {
|
||||||
|
try!(f.write_char(c));
|
||||||
|
}
|
||||||
|
f.write_char('"')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,10 +37,11 @@ impl<'a> From<&'a [u8]> for Bytes<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
|
impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
|
||||||
fn from(bytes: &'a Vec<u8>) -> Self {
|
fn from(bytes: &'a Vec<u8>) -> Self {
|
||||||
Bytes {
|
Bytes {
|
||||||
bytes: &bytes,
|
bytes: bytes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,157 +69,172 @@ impl<'a> ser::Serialize for Bytes<'a> {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// `ByteBuf` wraps a `Vec<u8>` and serializes as a byte array.
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
mod bytebuf {
|
||||||
pub struct ByteBuf {
|
use core::ops;
|
||||||
bytes: Vec<u8>,
|
use core::fmt;
|
||||||
}
|
use core::fmt::Write;
|
||||||
|
|
||||||
impl ByteBuf {
|
use ser;
|
||||||
/// Construct a new, empty `ByteBuf`.
|
use de;
|
||||||
pub fn new() -> Self {
|
|
||||||
ByteBuf {
|
#[cfg(feature = "collections")]
|
||||||
bytes: Vec::new(),
|
use collections::Vec;
|
||||||
|
|
||||||
|
/// `ByteBuf` wraps a `Vec<u8>` and serializes as a byte array.
|
||||||
|
#[derive(Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||||
|
pub struct ByteBuf {
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ByteBuf {
|
||||||
|
/// Construct a new, empty `ByteBuf`.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
ByteBuf {
|
||||||
|
bytes: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct a new, empty `ByteBuf` with the specified capacity.
|
||||||
|
pub fn with_capacity(cap: usize) -> Self {
|
||||||
|
ByteBuf {
|
||||||
|
bytes: Vec::with_capacity(cap)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a new, empty `ByteBuf` with the specified capacity.
|
impl fmt::Debug for ByteBuf {
|
||||||
pub fn with_capacity(cap: usize) -> Self {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
ByteBuf {
|
try!(f.write_str("b\""));
|
||||||
bytes: Vec::with_capacity(cap)
|
for c in super::escape_bytestring(self.bytes.as_ref()) {
|
||||||
|
try!(f.write_char(c));
|
||||||
|
}
|
||||||
|
f.write_char('"')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for ByteBuf {
|
impl Into<Vec<u8>> for ByteBuf {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn into(self) -> Vec<u8> {
|
||||||
write!(f, "b\"{}\"", escape_bytestring(self.bytes.as_ref()))
|
self.bytes
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<Vec<u8>> for ByteBuf {
|
|
||||||
fn into(self) -> Vec<u8> {
|
|
||||||
self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Vec<u8>> for ByteBuf {
|
|
||||||
fn from(bytes: Vec<u8>) -> Self {
|
|
||||||
ByteBuf {
|
|
||||||
bytes: bytes,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl AsRef<Vec<u8>> for ByteBuf {
|
impl From<Vec<u8>> for ByteBuf {
|
||||||
fn as_ref(&self) -> &Vec<u8> {
|
fn from(bytes: Vec<u8>) -> Self {
|
||||||
&self.bytes
|
ByteBuf {
|
||||||
}
|
bytes: bytes,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
impl AsRef<[u8]> for ByteBuf {
|
|
||||||
fn as_ref(&self) -> &[u8] {
|
|
||||||
&self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AsMut<Vec<u8>> for ByteBuf {
|
|
||||||
fn as_mut(&mut self) -> &mut Vec<u8> {
|
|
||||||
&mut self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AsMut<[u8]> for ByteBuf {
|
|
||||||
fn as_mut(&mut self) -> &mut [u8] {
|
|
||||||
&mut self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::Deref for ByteBuf {
|
|
||||||
type Target = [u8];
|
|
||||||
|
|
||||||
fn deref(&self) -> &[u8] { &self.bytes[..] }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::DerefMut for ByteBuf {
|
|
||||||
fn deref_mut(&mut self) -> &mut [u8] { &mut self.bytes[..] }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ser::Serialize for ByteBuf {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
||||||
where S: ser::Serializer
|
|
||||||
{
|
|
||||||
serializer.serialize_bytes(&self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This type implements the `serde::de::Visitor` trait for a `ByteBuf`.
|
|
||||||
pub struct ByteBufVisitor;
|
|
||||||
|
|
||||||
impl de::Visitor for ByteBufVisitor {
|
|
||||||
type Value = ByteBuf;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_unit<E>(&mut self) -> Result<ByteBuf, E>
|
|
||||||
where E: de::Error,
|
|
||||||
{
|
|
||||||
Ok(ByteBuf {
|
|
||||||
bytes: Vec::new(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
impl AsRef<Vec<u8>> for ByteBuf {
|
||||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<ByteBuf, V::Error>
|
fn as_ref(&self) -> &Vec<u8> {
|
||||||
where V: de::SeqVisitor,
|
&self.bytes
|
||||||
{
|
}
|
||||||
let (len, _) = visitor.size_hint();
|
}
|
||||||
let mut values = Vec::with_capacity(len);
|
|
||||||
|
|
||||||
while let Some(value) = try!(visitor.visit()) {
|
impl AsRef<[u8]> for ByteBuf {
|
||||||
values.push(value);
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
&self.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsMut<Vec<u8>> for ByteBuf {
|
||||||
|
fn as_mut(&mut self) -> &mut Vec<u8> {
|
||||||
|
&mut self.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsMut<[u8]> for ByteBuf {
|
||||||
|
fn as_mut(&mut self) -> &mut [u8] {
|
||||||
|
&mut self.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::Deref for ByteBuf {
|
||||||
|
type Target = [u8];
|
||||||
|
|
||||||
|
fn deref(&self) -> &[u8] { &self.bytes[..] }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::DerefMut for ByteBuf {
|
||||||
|
fn deref_mut(&mut self) -> &mut [u8] { &mut self.bytes[..] }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ser::Serialize for ByteBuf {
|
||||||
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
|
where S: ser::Serializer
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This type implements the `serde::de::Visitor` trait for a `ByteBuf`.
|
||||||
|
pub struct ByteBufVisitor;
|
||||||
|
|
||||||
|
impl de::Visitor for ByteBufVisitor {
|
||||||
|
type Value = ByteBuf;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_unit<E>(&mut self) -> Result<ByteBuf, E>
|
||||||
|
where E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(ByteBuf {
|
||||||
|
bytes: Vec::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
try!(visitor.end());
|
#[inline]
|
||||||
|
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<ByteBuf, V::Error>
|
||||||
|
where V: de::SeqVisitor,
|
||||||
|
{
|
||||||
|
let (len, _) = visitor.size_hint();
|
||||||
|
let mut values = Vec::with_capacity(len);
|
||||||
|
|
||||||
Ok(ByteBuf {
|
while let Some(value) = try!(visitor.visit()) {
|
||||||
bytes: values,
|
values.push(value);
|
||||||
})
|
}
|
||||||
|
|
||||||
|
try!(visitor.end());
|
||||||
|
|
||||||
|
Ok(ByteBuf {
|
||||||
|
bytes: values,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<ByteBuf, E>
|
||||||
|
where E: de::Error,
|
||||||
|
{
|
||||||
|
self.visit_byte_buf(v.to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E>
|
||||||
|
where E: de::Error,
|
||||||
|
{
|
||||||
|
Ok(ByteBuf {
|
||||||
|
bytes: v,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
impl de::Deserialize for ByteBuf {
|
||||||
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<ByteBuf, E>
|
#[inline]
|
||||||
where E: de::Error,
|
fn deserialize<D>(deserializer: &mut D) -> Result<ByteBuf, D::Error>
|
||||||
{
|
where D: de::Deserializer
|
||||||
self.visit_byte_buf(v.to_vec())
|
{
|
||||||
}
|
deserializer.deserialize_bytes(ByteBufVisitor)
|
||||||
|
}
|
||||||
#[inline]
|
|
||||||
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E>
|
|
||||||
where E: de::Error,
|
|
||||||
{
|
|
||||||
Ok(ByteBuf {
|
|
||||||
bytes: v,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl de::Deserialize for ByteBuf {
|
|
||||||
#[inline]
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<ByteBuf, D::Error>
|
|
||||||
where D: de::Deserializer
|
|
||||||
{
|
|
||||||
deserializer.deserialize_bytes(ByteBufVisitor)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
fn escape_bytestring(bytes: &[u8]) -> String {
|
#[inline]
|
||||||
let mut result = String::new();
|
fn escape_bytestring<'a>(bytes: &'a [u8]) -> iter::FlatMap<slice::Iter<'a, u8>, char::EscapeDefault, fn(&u8) -> char::EscapeDefault> {
|
||||||
for &b in bytes {
|
fn f(b: &u8) -> char::EscapeDefault {
|
||||||
for esc in ascii::escape_default(b) {
|
char::from_u32(*b as u32).unwrap().escape_default()
|
||||||
result.push(esc as char);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result
|
bytes.iter().flat_map(f as fn(&u8) -> char::EscapeDefault)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,13 @@
|
|||||||
|
|
||||||
// Extracted from https://github.com/rust-num/num.
|
// Extracted from https://github.com/rust-num/num.
|
||||||
|
|
||||||
use std::{usize, u8, u16, u32, u64};
|
// Rust 1.5 is unhappy that this private module is undocumented.
|
||||||
use std::{isize, i8, i16, i32, i64};
|
#![allow(missing_docs)]
|
||||||
use std::{f32, f64};
|
|
||||||
use std::mem::size_of;
|
use core::{usize, u8, u16, u32, u64};
|
||||||
|
use core::{isize, i8, i16, i32, i64};
|
||||||
|
use core::{f32, f64};
|
||||||
|
use core::mem::size_of;
|
||||||
|
|
||||||
/// Numbers which have upper and lower bounds
|
/// Numbers which have upper and lower bounds
|
||||||
pub trait Bounded {
|
pub trait Bounded {
|
||||||
@@ -271,7 +274,7 @@ macro_rules! impl_to_primitive_float_to_float {
|
|||||||
Some($slf as $DstT)
|
Some($slf as $DstT)
|
||||||
} else {
|
} else {
|
||||||
let n = $slf as f64;
|
let n = $slf as f64;
|
||||||
let max_value: $SrcT = ::std::$SrcT::MAX;
|
let max_value: $SrcT = ::core::$SrcT::MAX;
|
||||||
if -max_value as f64 <= n && n <= max_value as f64 {
|
if -max_value as f64 <= n && n <= max_value as f64 {
|
||||||
Some($slf as $DstT)
|
Some($slf as $DstT)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+89
-11
@@ -1,30 +1,63 @@
|
|||||||
//! This module contains `Deserialize` and `Visitor` implementations.
|
//! This module contains `Deserialize` and `Visitor` implementations.
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::{
|
#[cfg(all(feature = "nightly", feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::borrow::Cow;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::{
|
||||||
BinaryHeap,
|
BinaryHeap,
|
||||||
BTreeMap,
|
BTreeMap,
|
||||||
BTreeSet,
|
BTreeSet,
|
||||||
LinkedList,
|
LinkedList,
|
||||||
|
VecDeque,
|
||||||
|
Vec,
|
||||||
|
String,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::collections::{
|
||||||
HashMap,
|
HashMap,
|
||||||
HashSet,
|
HashSet,
|
||||||
|
BinaryHeap,
|
||||||
|
BTreeMap,
|
||||||
|
BTreeSet,
|
||||||
|
LinkedList,
|
||||||
VecDeque,
|
VecDeque,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "nightly")]
|
|
||||||
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
use collections::enum_set::{CLike, EnumSet};
|
use collections::enum_set::{CLike, EnumSet};
|
||||||
use std::hash::Hash;
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
use std::marker::PhantomData;
|
use collections::borrow::ToOwned;
|
||||||
|
|
||||||
|
use core::hash::Hash;
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::net;
|
use std::net;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::path;
|
use std::path;
|
||||||
|
use core::str;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::str;
|
#[cfg(all(feature = "nightly", feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::rc::Rc;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
#[cfg(all(feature = "nightly", feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::arc::Arc;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "nightly", feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use core::nonzero::{NonZero, Zeroable};
|
use core::nonzero::{NonZero, Zeroable};
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use std::num::Zero;
|
use core::num::Zero;
|
||||||
|
|
||||||
use de::{
|
use de::{
|
||||||
Deserialize,
|
Deserialize,
|
||||||
@@ -85,7 +118,7 @@ impl Visitor for BoolVisitor {
|
|||||||
fn visit_str<E>(&mut self, s: &str) -> Result<bool, E>
|
fn visit_str<E>(&mut self, s: &str) -> Result<bool, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
match s.trim() {
|
match s.trim_matches(::utils::Pattern_White_Space) {
|
||||||
"true" => Ok(true),
|
"true" => Ok(true),
|
||||||
"false" => Ok(false),
|
"false" => Ok(false),
|
||||||
_ => Err(Error::invalid_type(Type::Bool)),
|
_ => Err(Error::invalid_type(Type::Bool)),
|
||||||
@@ -151,10 +184,10 @@ impl<T> Visitor for PrimitiveVisitor<T>
|
|||||||
impl_deserialize_num_method!(f64, visit_f64, from_f64, Type::F64);
|
impl_deserialize_num_method!(f64, visit_f64, from_f64, Type::F64);
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_str<E>(&mut self, v: &str) -> Result<T, E>
|
fn visit_str<E>(&mut self, s: &str) -> Result<T, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
str::FromStr::from_str(v.trim()).or_else(|_| {
|
str::FromStr::from_str(s.trim_matches(::utils::Pattern_White_Space)).or_else(|_| {
|
||||||
Err(Error::invalid_type(Type::Str))
|
Err(Error::invalid_type(Type::Str))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -228,8 +261,10 @@ impl Deserialize for char {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
struct StringVisitor;
|
struct StringVisitor;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl Visitor for StringVisitor {
|
impl Visitor for StringVisitor {
|
||||||
type Value = String;
|
type Value = String;
|
||||||
|
|
||||||
@@ -264,6 +299,7 @@ impl Visitor for StringVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl Deserialize for String {
|
impl Deserialize for String {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -406,6 +442,7 @@ macro_rules! seq_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
BinaryHeap<T>,
|
BinaryHeap<T>,
|
||||||
<Deserialize, Ord>,
|
<Deserialize, Ord>,
|
||||||
@@ -415,6 +452,7 @@ seq_impl!(
|
|||||||
BinaryHeap::with_capacity(visitor.size_hint().0),
|
BinaryHeap::with_capacity(visitor.size_hint().0),
|
||||||
BinaryHeap::push);
|
BinaryHeap::push);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
BTreeSet<T>,
|
BTreeSet<T>,
|
||||||
<Deserialize, Eq, Ord>,
|
<Deserialize, Eq, Ord>,
|
||||||
@@ -424,7 +462,7 @@ seq_impl!(
|
|||||||
BTreeSet::new(),
|
BTreeSet::new(),
|
||||||
BTreeSet::insert);
|
BTreeSet::insert);
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
EnumSet<T>,
|
EnumSet<T>,
|
||||||
<Deserialize, CLike>,
|
<Deserialize, CLike>,
|
||||||
@@ -434,6 +472,7 @@ seq_impl!(
|
|||||||
EnumSet::new(),
|
EnumSet::new(),
|
||||||
EnumSet::insert);
|
EnumSet::insert);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
LinkedList<T>,
|
LinkedList<T>,
|
||||||
<Deserialize>,
|
<Deserialize>,
|
||||||
@@ -443,6 +482,7 @@ seq_impl!(
|
|||||||
LinkedList::new(),
|
LinkedList::new(),
|
||||||
LinkedList::push_back);
|
LinkedList::push_back);
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
HashSet<T>,
|
HashSet<T>,
|
||||||
<Deserialize, Eq, Hash>,
|
<Deserialize, Eq, Hash>,
|
||||||
@@ -452,6 +492,7 @@ seq_impl!(
|
|||||||
HashSet::with_capacity(visitor.size_hint().0),
|
HashSet::with_capacity(visitor.size_hint().0),
|
||||||
HashSet::insert);
|
HashSet::insert);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
Vec<T>,
|
Vec<T>,
|
||||||
<Deserialize>,
|
<Deserialize>,
|
||||||
@@ -461,6 +502,7 @@ seq_impl!(
|
|||||||
Vec::with_capacity(visitor.size_hint().0),
|
Vec::with_capacity(visitor.size_hint().0),
|
||||||
Vec::push);
|
Vec::push);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
VecDeque<T>,
|
VecDeque<T>,
|
||||||
<Deserialize>,
|
<Deserialize>,
|
||||||
@@ -747,6 +789,7 @@ macro_rules! map_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
map_impl!(
|
map_impl!(
|
||||||
BTreeMap<K, V>,
|
BTreeMap<K, V>,
|
||||||
<Deserialize, Eq, Ord>,
|
<Deserialize, Eq, Ord>,
|
||||||
@@ -756,6 +799,7 @@ map_impl!(
|
|||||||
BTreeMap::new(),
|
BTreeMap::new(),
|
||||||
BTreeMap::insert);
|
BTreeMap::insert);
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
map_impl!(
|
map_impl!(
|
||||||
HashMap<K, V>,
|
HashMap<K, V>,
|
||||||
<Deserialize, Eq, Hash>,
|
<Deserialize, Eq, Hash>,
|
||||||
@@ -767,7 +811,7 @@ map_impl!(
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "nightly", feature = "std"))]
|
||||||
impl Deserialize for net::IpAddr {
|
impl Deserialize for net::IpAddr {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -780,6 +824,7 @@ impl Deserialize for net::IpAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::Ipv4Addr {
|
impl Deserialize for net::Ipv4Addr {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -792,6 +837,7 @@ impl Deserialize for net::Ipv4Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::Ipv6Addr {
|
impl Deserialize for net::Ipv6Addr {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -806,6 +852,7 @@ impl Deserialize for net::Ipv6Addr {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddr {
|
impl Deserialize for net::SocketAddr {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -818,6 +865,7 @@ impl Deserialize for net::SocketAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddrV4 {
|
impl Deserialize for net::SocketAddrV4 {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -830,6 +878,7 @@ impl Deserialize for net::SocketAddrV4 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for net::SocketAddrV6 {
|
impl Deserialize for net::SocketAddrV6 {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -844,8 +893,10 @@ impl Deserialize for net::SocketAddrV6 {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
struct PathBufVisitor;
|
struct PathBufVisitor;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Visitor for PathBufVisitor {
|
impl Visitor for PathBufVisitor {
|
||||||
type Value = path::PathBuf;
|
type Value = path::PathBuf;
|
||||||
|
|
||||||
@@ -862,6 +913,7 @@ impl Visitor for PathBufVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Deserialize for path::PathBuf {
|
impl Deserialize for path::PathBuf {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -872,6 +924,7 @@ impl Deserialize for path::PathBuf {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Box<T> {
|
impl<T: Deserialize> Deserialize for Box<T> {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Box<T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Box<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -881,6 +934,17 @@ impl<T: Deserialize> Deserialize for Box<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
|
impl<T: Deserialize> Deserialize for Box<[T]> {
|
||||||
|
fn deserialize<D>(deserializer: &mut D) -> Result<Box<[T]>, D::Error>
|
||||||
|
where D: Deserializer,
|
||||||
|
{
|
||||||
|
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
|
||||||
|
Ok(v.into_boxed_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -890,6 +954,7 @@ impl<T: Deserialize> Deserialize for Arc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: Deserialize> Deserialize for Rc<T> {
|
impl<T: Deserialize> Deserialize for Rc<T> {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Rc<T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Rc<T>, D::Error>
|
||||||
where D: Deserializer,
|
where D: Deserializer,
|
||||||
@@ -899,6 +964,7 @@ impl<T: Deserialize> Deserialize for Rc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
|
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Cow<'a, T>, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Cow<'a, T>, D::Error>
|
||||||
@@ -945,7 +1011,10 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
|||||||
impl ::de::Visitor for FieldVisitor {
|
impl ::de::Visitor for FieldVisitor {
|
||||||
type Value = Field;
|
type Value = Field;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
|
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
|
||||||
|
#[cfg(feature = "collections")]
|
||||||
|
use collections::string::ToString;
|
||||||
match value {
|
match value {
|
||||||
0 => Ok(Field::Ok),
|
0 => Ok(Field::Ok),
|
||||||
1 => Ok(Field::Err),
|
1 => Ok(Field::Err),
|
||||||
@@ -953,6 +1022,15 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
|
||||||
|
match value {
|
||||||
|
0 => Ok(Field::Ok),
|
||||||
|
1 => Ok(Field::Err),
|
||||||
|
_ => Err(Error::unknown_field("some number")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E> where E: Error {
|
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E> where E: Error {
|
||||||
match value {
|
match value {
|
||||||
"Ok" => Ok(Field::Ok),
|
"Ok" => Ok(Field::Ok),
|
||||||
|
|||||||
+23
-3
@@ -1,6 +1,12 @@
|
|||||||
//! Generic deserialization framework.
|
//! Generic deserialization framework.
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::error;
|
use std::error;
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
use error;
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), feature = "collections"))]
|
||||||
|
use collections::{String, Vec};
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
@@ -12,8 +18,13 @@ mod from_primitive;
|
|||||||
/// `Deserializer` error.
|
/// `Deserializer` 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 general error when deserializing a type.
|
||||||
|
#[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.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn custom<T: Into<&'static str>>(msg: T) -> Self;
|
||||||
|
|
||||||
/// Raised when a `Deserialize` type unexpectedly hit the end of the stream.
|
/// Raised when a `Deserialize` type unexpectedly hit the end of the stream.
|
||||||
fn end_of_stream() -> Self;
|
fn end_of_stream() -> Self;
|
||||||
|
|
||||||
@@ -28,6 +39,9 @@ pub trait Error: Sized + error::Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Raised when a fixed sized sequence or map was passed in the wrong amount of arguments.
|
/// Raised when a fixed sized sequence or map was passed in the wrong amount of arguments.
|
||||||
|
///
|
||||||
|
/// The parameter `len` is the number of arguments found in the serialization. The sequence
|
||||||
|
/// may either expect more arguments or less arguments.
|
||||||
fn invalid_length(len: usize) -> Self {
|
fn invalid_length(len: usize) -> Self {
|
||||||
Error::custom(format!("Invalid length: {}", len))
|
Error::custom(format!("Invalid length: {}", len))
|
||||||
}
|
}
|
||||||
@@ -46,6 +60,12 @@ pub trait Error: Sized + error::Error {
|
|||||||
fn missing_field(field: &'static str) -> Self {
|
fn missing_field(field: &'static str) -> Self {
|
||||||
Error::custom(format!("Missing field `{}`", field))
|
Error::custom(format!("Missing field `{}`", field))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Raised when a `Deserialize` struct type received more than one of the
|
||||||
|
/// same struct field.
|
||||||
|
fn duplicate_field(field: &'static str) -> Self {
|
||||||
|
Error::custom(format!("Duplicate field `{}`", field))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Type` represents all the primitive types that can be deserialized. This is used by
|
/// `Type` represents all the primitive types that can be deserialized. This is used by
|
||||||
@@ -558,9 +578,7 @@ pub trait Visitor {
|
|||||||
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
self.visit_str(::utils::encode_utf8(v).as_str())
|
||||||
// doesn't support encoding a `char` into a stack buffer.
|
|
||||||
self.visit_string(v.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_str` deserializes a `&str` into a `Value`.
|
/// `visit_str` deserializes a `&str` into a `Value`.
|
||||||
@@ -574,6 +592,7 @@ pub trait Visitor {
|
|||||||
/// a copy if it is deserializing a string from a `String` type. By default it passes a `&str`
|
/// a copy if it is deserializing a string from a `String` type. By default it passes a `&str`
|
||||||
/// to the `visit_str` method.
|
/// to the `visit_str` method.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn visit_string<E>(&mut self, v: String) -> Result<Self::Value, E>
|
fn visit_string<E>(&mut self, v: String) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
@@ -638,6 +657,7 @@ pub trait Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.
|
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<Self::Value, E>
|
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
|
|||||||
+72
-4
@@ -1,5 +1,6 @@
|
|||||||
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::collections::{
|
use std::collections::{
|
||||||
BTreeMap,
|
BTreeMap,
|
||||||
BTreeSet,
|
BTreeSet,
|
||||||
@@ -10,11 +11,31 @@ use std::collections::{
|
|||||||
hash_map,
|
hash_map,
|
||||||
hash_set,
|
hash_set,
|
||||||
};
|
};
|
||||||
use std::hash::Hash;
|
#[cfg(feature = "std")]
|
||||||
use std::error;
|
|
||||||
use std::fmt;
|
|
||||||
use std::vec;
|
use std::vec;
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::{
|
||||||
|
BTreeMap,
|
||||||
|
BTreeSet,
|
||||||
|
Vec,
|
||||||
|
String,
|
||||||
|
btree_map,
|
||||||
|
btree_set,
|
||||||
|
vec,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
|
use collections::borrow::ToOwned;
|
||||||
|
|
||||||
|
use core::hash::Hash;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::error;
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
use error;
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
use de;
|
use de;
|
||||||
use bytes;
|
use bytes;
|
||||||
@@ -25,7 +46,11 @@ use bytes;
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// The value had some custom error.
|
/// The value had some custom error.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
Custom(String),
|
Custom(String),
|
||||||
|
/// The value had some custom error.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
Custom(&'static str),
|
||||||
|
|
||||||
/// The value had an incorrect type.
|
/// The value had an incorrect type.
|
||||||
InvalidType(de::Type),
|
InvalidType(de::Type),
|
||||||
@@ -34,29 +59,60 @@ pub enum Error {
|
|||||||
InvalidLength(usize),
|
InvalidLength(usize),
|
||||||
|
|
||||||
/// The value is invalid and cannot be deserialized.
|
/// The value is invalid and cannot be deserialized.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
InvalidValue(String),
|
InvalidValue(String),
|
||||||
|
/// The value is invalid and cannot be deserialized.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
InvalidValue(&'static str),
|
||||||
|
|
||||||
/// EOF while deserializing a value.
|
/// EOF while deserializing a value.
|
||||||
EndOfStream,
|
EndOfStream,
|
||||||
|
|
||||||
/// Unknown variant in enum.
|
/// Unknown variant in enum.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
UnknownVariant(String),
|
UnknownVariant(String),
|
||||||
|
/// Unknown variant in enum.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
UnknownVariant(&'static str),
|
||||||
|
|
||||||
/// Unknown field in struct.
|
/// Unknown field in struct.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
UnknownField(String),
|
UnknownField(String),
|
||||||
|
/// Unknown field in struct.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
UnknownField(&'static str),
|
||||||
|
|
||||||
/// Struct is missing a field.
|
/// Struct is missing a field.
|
||||||
MissingField(&'static str),
|
MissingField(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl de::Error for Error {
|
impl de::Error for Error {
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn custom<T: Into<String>>(msg: T) -> Self { Error::Custom(msg.into()) }
|
fn custom<T: Into<String>>(msg: T) -> Self { Error::Custom(msg.into()) }
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn custom<T: Into<&'static str>>(msg: T) -> Self { Error::Custom(msg.into()) }
|
||||||
|
|
||||||
fn end_of_stream() -> Self { Error::EndOfStream }
|
fn end_of_stream() -> Self { Error::EndOfStream }
|
||||||
fn invalid_type(ty: de::Type) -> Self { Error::InvalidType(ty) }
|
fn invalid_type(ty: de::Type) -> Self { Error::InvalidType(ty) }
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn invalid_value(msg: &str) -> Self { Error::InvalidValue(msg.to_owned()) }
|
fn invalid_value(msg: &str) -> Self { Error::InvalidValue(msg.to_owned()) }
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn invalid_value(msg: &str) -> Self { Error::InvalidValue("invalid value") }
|
||||||
|
|
||||||
fn invalid_length(len: usize) -> Self { Error::InvalidLength(len) }
|
fn invalid_length(len: usize) -> Self { Error::InvalidLength(len) }
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn unknown_variant(variant: &str) -> Self { Error::UnknownVariant(String::from(variant)) }
|
fn unknown_variant(variant: &str) -> Self { Error::UnknownVariant(String::from(variant)) }
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn unknown_field(field: &str) -> Self { Error::UnknownField(String::from(field)) }
|
fn unknown_field(field: &str) -> Self { Error::UnknownField(String::from(field)) }
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn unknown_variant(variant: &str) -> Self { Error::UnknownVariant("unknown variant") }
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn unknown_field(field: &str) -> Self { Error::UnknownField("unknown field") }
|
||||||
fn missing_field(field: &'static str) -> Self { Error::MissingField(field) }
|
fn missing_field(field: &'static str) -> Self { Error::MissingField(field) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,8 +294,10 @@ impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E>
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A helper deserializer that deserializes a `String`.
|
/// A helper deserializer that deserializes a `String`.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
pub struct StringDeserializer<E>(Option<String>, PhantomData<E>);
|
pub struct StringDeserializer<E>(Option<String>, PhantomData<E>);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> ValueDeserializer<E> for String
|
impl<E> ValueDeserializer<E> for String
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
{
|
{
|
||||||
@@ -250,6 +308,7 @@ impl<E> ValueDeserializer<E> for String
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> de::Deserializer for StringDeserializer<E>
|
impl<E> de::Deserializer for StringDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
{
|
{
|
||||||
@@ -274,6 +333,7 @@ impl<E> de::Deserializer for StringDeserializer<E>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, E> de::VariantVisitor for StringDeserializer<E>
|
impl<'a, E> de::VariantVisitor for StringDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
{
|
{
|
||||||
@@ -361,6 +421,7 @@ impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T, E> ValueDeserializer<E> for Vec<T>
|
impl<T, E> ValueDeserializer<E> for Vec<T>
|
||||||
where T: ValueDeserializer<E>,
|
where T: ValueDeserializer<E>,
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -373,6 +434,7 @@ impl<T, E> ValueDeserializer<E> for Vec<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
||||||
where T: ValueDeserializer<E> + Eq + Ord,
|
where T: ValueDeserializer<E> + Eq + Ord,
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -385,6 +447,7 @@ impl<T, E> ValueDeserializer<E> for BTreeSet<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl<T, E> ValueDeserializer<E> for HashSet<T>
|
impl<T, E> ValueDeserializer<E> for HashSet<T>
|
||||||
where T: ValueDeserializer<E> + Eq + Hash,
|
where T: ValueDeserializer<E> + Eq + Hash,
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -527,6 +590,7 @@ impl<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
||||||
where K: ValueDeserializer<E> + Eq + Ord,
|
where K: ValueDeserializer<E> + Eq + Ord,
|
||||||
V: ValueDeserializer<E>,
|
V: ValueDeserializer<E>,
|
||||||
@@ -540,6 +604,7 @@ impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl<K, V, E> ValueDeserializer<E> for HashMap<K, V>
|
impl<K, V, E> ValueDeserializer<E> for HashMap<K, V>
|
||||||
where K: ValueDeserializer<E> + Eq + Hash,
|
where K: ValueDeserializer<E> + Eq + Hash,
|
||||||
V: ValueDeserializer<E>,
|
V: ValueDeserializer<E>,
|
||||||
@@ -618,6 +683,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> ValueDeserializer<E> for bytes::ByteBuf
|
impl<E> ValueDeserializer<E> for bytes::ByteBuf
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
{
|
{
|
||||||
@@ -629,8 +695,10 @@ impl<E> ValueDeserializer<E> for bytes::ByteBuf
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A helper deserializer that deserializes a `Vec<u8>`.
|
/// A helper deserializer that deserializes a `Vec<u8>`.
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
pub struct ByteBufDeserializer<E>(Option<Vec<u8>>, PhantomData<E>);
|
pub struct ByteBufDeserializer<E>(Option<Vec<u8>>, PhantomData<E>);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<E> de::Deserializer for ByteBufDeserializer<E>
|
impl<E> de::Deserializer for ByteBufDeserializer<E>
|
||||||
where E: de::Error,
|
where E: de::Error,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
//! A stand-in for `std::error`
|
||||||
|
use core::any::TypeId;
|
||||||
|
use core::fmt::{Debug, Display};
|
||||||
|
|
||||||
|
|
||||||
|
/// A stand-in for `std::error::Error`, which requires no allocation.
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
pub trait Error: Debug + Display + ::core::marker::Reflect {
|
||||||
|
/// A short description of the error.
|
||||||
|
///
|
||||||
|
/// The description should not contain newlines or sentence-ending
|
||||||
|
/// punctuation, to facilitate embedding in larger user-facing
|
||||||
|
/// strings.
|
||||||
|
fn description(&self) -> &str;
|
||||||
|
|
||||||
|
/// The lower-level cause of this error, if any.
|
||||||
|
fn cause(&self) -> Option<&Error> { None }
|
||||||
|
|
||||||
|
/// Get the `TypeId` of `self`
|
||||||
|
#[doc(hidden)]
|
||||||
|
fn type_id(&self) -> TypeId where Self: 'static {
|
||||||
|
TypeId::of::<Self>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A stand-in for `std::error::Error`, which requires no allocation.
|
||||||
|
#[cfg(not(feature = "nightly"))]
|
||||||
|
pub trait Error: Debug + Display {
|
||||||
|
/// A short description of the error.
|
||||||
|
///
|
||||||
|
/// The description should not contain newlines or sentence-ending
|
||||||
|
/// punctuation, to facilitate embedding in larger user-facing
|
||||||
|
/// strings.
|
||||||
|
fn description(&self) -> &str;
|
||||||
|
|
||||||
|
/// The lower-level cause of this error, if any.
|
||||||
|
fn cause(&self) -> Option<&Error> { None }
|
||||||
|
|
||||||
|
/// Stubbed! Returns type_id of `()`
|
||||||
|
#[doc(hidden)]
|
||||||
|
fn type_id(&self) -> TypeId where Self: 'static {
|
||||||
|
TypeId::of::<()>()
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
-5
@@ -10,23 +10,46 @@
|
|||||||
//! [github repository](https://github.com/serde-rs/serde)
|
//! [github repository](https://github.com/serde-rs/serde)
|
||||||
|
|
||||||
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
||||||
#![cfg_attr(feature = "nightly", feature(collections, enumset, nonzero, plugin, step_trait,
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
zero_one))]
|
#![cfg_attr(feature = "nightly", feature(reflect_marker, unicode, nonzero, plugin, step_trait, zero_one))]
|
||||||
|
#![cfg_attr(feature = "alloc", feature(alloc))]
|
||||||
|
#![cfg_attr(feature = "collections", feature(collections, enumset))]
|
||||||
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
||||||
#![cfg_attr(feature = "nightly-testing", allow(linkedlist))]
|
#![cfg_attr(feature = "nightly-testing", allow(linkedlist))]
|
||||||
|
|
||||||
|
#![cfg_attr(any(not(feature = "std"), feature = "nightly"), allow(unused_variables, unused_imports, unused_features, dead_code))]
|
||||||
|
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
extern crate collections;
|
extern crate collections;
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "nightly", feature = "alloc"))]
|
||||||
extern crate core;
|
extern crate alloc;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
mod core {
|
||||||
|
pub use std::{ops, hash, fmt, cmp, marker, mem, i8, i16, i32, i64, u8, u16, u32, u64, isize,
|
||||||
|
usize, f32, f64, char, str, num, slice, iter};
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
extern crate core;
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
pub use self::core::nonzero;
|
||||||
|
}
|
||||||
|
|
||||||
pub use ser::{Serialize, Serializer};
|
pub use ser::{Serialize, Serializer};
|
||||||
pub use de::{Deserialize, Deserializer, Error};
|
pub use de::{Deserialize, Deserializer, Error};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
macro_rules! format {
|
||||||
|
($s:expr, $($rest:tt)*) => ($s)
|
||||||
|
}
|
||||||
|
|
||||||
pub mod bytes;
|
pub mod bytes;
|
||||||
pub mod de;
|
pub mod de;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
pub mod error;
|
||||||
|
mod utils;
|
||||||
|
|||||||
+60
-8
@@ -1,6 +1,11 @@
|
|||||||
//! Implementations for all of Rust's builtin types.
|
//! Implementations for all of Rust's builtin types.
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::borrow::Cow;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::collections::{
|
use std::collections::{
|
||||||
BinaryHeap,
|
BinaryHeap,
|
||||||
BTreeMap,
|
BTreeMap,
|
||||||
@@ -10,20 +15,47 @@ use std::collections::{
|
|||||||
HashSet,
|
HashSet,
|
||||||
VecDeque,
|
VecDeque,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::{
|
||||||
|
BinaryHeap,
|
||||||
|
BTreeMap,
|
||||||
|
BTreeSet,
|
||||||
|
LinkedList,
|
||||||
|
VecDeque,
|
||||||
|
String,
|
||||||
|
Vec,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
use collections::enum_set::{CLike, EnumSet};
|
use collections::enum_set::{CLike, EnumSet};
|
||||||
use std::hash::Hash;
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
|
use collections::borrow::ToOwned;
|
||||||
|
|
||||||
|
use core::hash::Hash;
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use std::iter;
|
use core::iter;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::net;
|
use std::net;
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use std::num;
|
use core::num;
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use std::ops;
|
use core::ops;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::path;
|
use std::path;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::rc::Rc;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::marker::PhantomData;
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::arc::Arc;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
use core::nonzero::{NonZero, Zeroable};
|
use core::nonzero::{NonZero, Zeroable};
|
||||||
@@ -77,6 +109,7 @@ impl Serialize for str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl Serialize for String {
|
impl Serialize for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -260,6 +293,7 @@ array_impls!(32);
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for BinaryHeap<T>
|
impl<T> Serialize for BinaryHeap<T>
|
||||||
where T: Serialize + Ord
|
where T: Serialize + Ord
|
||||||
{
|
{
|
||||||
@@ -271,6 +305,7 @@ impl<T> Serialize for BinaryHeap<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for BTreeSet<T>
|
impl<T> Serialize for BTreeSet<T>
|
||||||
where T: Serialize + Ord,
|
where T: Serialize + Ord,
|
||||||
{
|
{
|
||||||
@@ -282,7 +317,7 @@ impl<T> Serialize for BTreeSet<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||||
impl<T> Serialize for EnumSet<T>
|
impl<T> Serialize for EnumSet<T>
|
||||||
where T: Serialize + CLike
|
where T: Serialize + CLike
|
||||||
{
|
{
|
||||||
@@ -294,6 +329,7 @@ impl<T> Serialize for EnumSet<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl<T> Serialize for HashSet<T>
|
impl<T> Serialize for HashSet<T>
|
||||||
where T: Serialize + Eq + Hash,
|
where T: Serialize + Eq + Hash,
|
||||||
{
|
{
|
||||||
@@ -305,6 +341,7 @@ impl<T> Serialize for HashSet<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for LinkedList<T>
|
impl<T> Serialize for LinkedList<T>
|
||||||
where T: Serialize,
|
where T: Serialize,
|
||||||
{
|
{
|
||||||
@@ -330,6 +367,7 @@ impl<A> Serialize for ops::Range<A>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for Vec<T> where T: Serialize {
|
impl<T> Serialize for Vec<T> where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -339,6 +377,7 @@ impl<T> Serialize for Vec<T> where T: Serialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for VecDeque<T> where T: Serialize {
|
impl<T> Serialize for VecDeque<T> where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -598,6 +637,7 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<K, V> Serialize for BTreeMap<K, V>
|
impl<K, V> Serialize for BTreeMap<K, V>
|
||||||
where K: Serialize + Ord,
|
where K: Serialize + Ord,
|
||||||
V: Serialize,
|
V: Serialize,
|
||||||
@@ -610,6 +650,7 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl<K, V> Serialize for HashMap<K, V>
|
impl<K, V> Serialize for HashMap<K, V>
|
||||||
where K: Serialize + Eq + Hash,
|
where K: Serialize + Eq + Hash,
|
||||||
V: Serialize,
|
V: Serialize,
|
||||||
@@ -642,6 +683,7 @@ impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: ?Sized> Serialize for Box<T> where T: Serialize {
|
impl<T: ?Sized> Serialize for Box<T> where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -651,6 +693,7 @@ impl<T: ?Sized> Serialize for Box<T> where T: Serialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T> Serialize for Rc<T> where T: Serialize, {
|
impl<T> Serialize for Rc<T> where T: Serialize, {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -660,6 +703,7 @@ impl<T> Serialize for Rc<T> where T: Serialize, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T> Serialize for Arc<T> where T: Serialize, {
|
impl<T> Serialize for Arc<T> where T: Serialize, {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -669,6 +713,7 @@ impl<T> Serialize for Arc<T> where T: Serialize, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
|
impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
@@ -695,7 +740,7 @@ impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(all(feature = "std", feature = "nightly"))]
|
||||||
impl Serialize for net::IpAddr {
|
impl Serialize for net::IpAddr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -707,6 +752,7 @@ impl Serialize for net::IpAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv4Addr {
|
impl Serialize for net::Ipv4Addr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -715,6 +761,7 @@ impl Serialize for net::Ipv4Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv6Addr {
|
impl Serialize for net::Ipv6Addr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -725,6 +772,7 @@ impl Serialize for net::Ipv6Addr {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddr {
|
impl Serialize for net::SocketAddr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -736,6 +784,7 @@ impl Serialize for net::SocketAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV4 {
|
impl Serialize for net::SocketAddrV4 {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -744,6 +793,7 @@ impl Serialize for net::SocketAddrV4 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV6 {
|
impl Serialize for net::SocketAddrV6 {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -754,6 +804,7 @@ impl Serialize for net::SocketAddrV6 {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::Path {
|
impl Serialize for path::Path {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
@@ -765,6 +816,7 @@ impl Serialize for path::Path {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::PathBuf {
|
impl Serialize for path::PathBuf {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
|
|||||||
+12
-3
@@ -1,6 +1,12 @@
|
|||||||
//! Generic serialization framework.
|
//! Generic serialization framework.
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::error;
|
use std::error;
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
use error;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::String;
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
|
|
||||||
@@ -10,8 +16,13 @@ pub mod impls;
|
|||||||
/// `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 general error when deserializing a type.
|
||||||
|
#[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.
|
||||||
|
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
|
||||||
|
fn custom<T: Into<&'static str>>(msg: T) -> Self;
|
||||||
|
|
||||||
/// Raised when a `Serialize` was passed an incorrect value.
|
/// Raised when a `Serialize` was passed an incorrect value.
|
||||||
fn invalid_value(msg: &str) -> Self {
|
fn invalid_value(msg: &str) -> Self {
|
||||||
Error::custom(format!("invalid value: {}", msg))
|
Error::custom(format!("invalid value: {}", msg))
|
||||||
@@ -115,9 +126,7 @@ pub trait Serializer {
|
|||||||
/// single character.
|
/// single character.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error> {
|
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error> {
|
||||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
self.serialize_str(::utils::encode_utf8(v).as_str())
|
||||||
// doesn't support encoding a `char` into a stack buffer.
|
|
||||||
self.serialize_str(&v.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes a `&str`.
|
/// Serializes a `&str`.
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
//! Private utility functions
|
||||||
|
|
||||||
|
const TAG_CONT: u8 = 0b1000_0000;
|
||||||
|
const TAG_TWO_B: u8 = 0b1100_0000;
|
||||||
|
const TAG_THREE_B: u8 = 0b1110_0000;
|
||||||
|
const TAG_FOUR_B: u8 = 0b1111_0000;
|
||||||
|
const MAX_ONE_B: u32 = 0x80;
|
||||||
|
const MAX_TWO_B: u32 = 0x800;
|
||||||
|
const MAX_THREE_B: u32 = 0x10000;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn encode_utf8(c: char) -> EncodeUtf8 {
|
||||||
|
let code = c as u32;
|
||||||
|
let mut buf = [0; 4];
|
||||||
|
let pos = if code < MAX_ONE_B {
|
||||||
|
buf[3] = code as u8;
|
||||||
|
3
|
||||||
|
} else if code < MAX_TWO_B {
|
||||||
|
buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
|
||||||
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
|
2
|
||||||
|
} else if code < MAX_THREE_B {
|
||||||
|
buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
|
||||||
|
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
||||||
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
|
||||||
|
buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
|
||||||
|
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
||||||
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
|
0
|
||||||
|
};
|
||||||
|
EncodeUtf8 { buf: buf, pos: pos }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EncodeUtf8 {
|
||||||
|
buf: [u8; 4],
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EncodeUtf8 {
|
||||||
|
// FIXME: use this from_utf8_unchecked, since we know it can never fail
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
::core::str::from_utf8(&self.buf[self.pos..]).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
const Pattern_White_Space_table: &'static [(char, char)] = &[
|
||||||
|
('\u{9}', '\u{d}'), ('\u{20}', '\u{20}'), ('\u{85}', '\u{85}'), ('\u{200e}', '\u{200f}'),
|
||||||
|
('\u{2028}', '\u{2029}')
|
||||||
|
];
|
||||||
|
|
||||||
|
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
|
||||||
|
use core::cmp::Ordering::{Equal, Less, Greater};
|
||||||
|
r.binary_search_by(|&(lo, hi)| {
|
||||||
|
if c < lo {
|
||||||
|
Greater
|
||||||
|
} else if hi < c {
|
||||||
|
Less
|
||||||
|
} else {
|
||||||
|
Equal
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
pub fn Pattern_White_Space(c: char) -> bool {
|
||||||
|
bsearch_range_table(c, Pattern_White_Space_table)
|
||||||
|
}
|
||||||
+11
-10
@@ -1,13 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen"
|
name = "serde_codegen"
|
||||||
version = "0.7.0"
|
version = "0.7.7"
|
||||||
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"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
documentation = "https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html"
|
documentation = "https://github.com/serde-rs/serde"
|
||||||
build = "build.rs"
|
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
|
build = "build.rs"
|
||||||
|
include = ["Cargo.toml", "build.rs", "src/**/*.rs"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["with-syntex"]
|
default = ["with-syntex"]
|
||||||
@@ -16,13 +17,13 @@ nightly-testing = ["clippy"]
|
|||||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
quasi_codegen = { version = "^0.7.0", optional = true }
|
quasi_codegen = { version = "^0.11.0", optional = true }
|
||||||
syntex = { version = "^0.29.0", optional = true }
|
syntex = { version = "^0.33.0", optional = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aster = { version = "^0.13.1", default-features = false }
|
aster = { version = "^0.17.0", default-features = false }
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
quasi = { version = "^0.7.0", default-features = false }
|
quasi = { version = "^0.11.0", default-features = false }
|
||||||
quasi_macros = { version = "^0.7.0", optional = true }
|
quasi_macros = { version = "^0.11.0", optional = true }
|
||||||
syntex = { version = "^0.29.0", optional = true }
|
syntex = { version = "^0.33.0", optional = true }
|
||||||
syntex_syntax = { version = "^0.29.0", optional = true }
|
syntex_syntax = { version = "^0.33.0", optional = true }
|
||||||
|
|||||||
+56
-204
@@ -4,7 +4,7 @@ use syntax::attr;
|
|||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::ExtCtxt;
|
use syntax::ext::base::ExtCtxt;
|
||||||
use syntax::fold::Folder;
|
use syntax::fold::Folder;
|
||||||
use syntax::parse::parser::PathParsingMode;
|
use syntax::parse::parser::PathStyle;
|
||||||
use syntax::parse::token::{self, InternedString};
|
use syntax::parse::token::{self, InternedString};
|
||||||
use syntax::parse;
|
use syntax::parse;
|
||||||
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
||||||
@@ -30,11 +30,6 @@ impl Name {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the string expression of the field ident.
|
|
||||||
pub fn ident_expr(&self) -> P<ast::Expr> {
|
|
||||||
AstBuilder::new().expr().str(self.ident)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the container name for the container when serializing.
|
/// Return the container name for the container when serializing.
|
||||||
pub fn serialize_name(&self) -> InternedString {
|
pub fn serialize_name(&self) -> InternedString {
|
||||||
match self.serialize_name {
|
match self.serialize_name {
|
||||||
@@ -181,36 +176,36 @@ impl VariantAttrs {
|
|||||||
pub struct FieldAttrs {
|
pub struct FieldAttrs {
|
||||||
name: Name,
|
name: Name,
|
||||||
skip_serializing_field: bool,
|
skip_serializing_field: bool,
|
||||||
skip_serializing_field_if: Option<P<ast::Expr>>,
|
skip_deserializing_field: bool,
|
||||||
|
skip_serializing_if: Option<ast::Path>,
|
||||||
default_expr_if_missing: Option<P<ast::Expr>>,
|
default_expr_if_missing: Option<P<ast::Expr>>,
|
||||||
serialize_with: Option<P<ast::Expr>>,
|
serialize_with: Option<ast::Path>,
|
||||||
deserialize_with: Option<P<ast::Expr>>,
|
deserialize_with: Option<ast::Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FieldAttrs {
|
impl FieldAttrs {
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
||||||
pub fn from_field(cx: &ExtCtxt,
|
pub fn from_field(cx: &ExtCtxt,
|
||||||
container_ty: &P<ast::Ty>,
|
index: usize,
|
||||||
generics: &ast::Generics,
|
field: &ast::StructField) -> Result<Self, Error> {
|
||||||
field: &ast::StructField,
|
|
||||||
is_enum: bool) -> Result<Self, Error> {
|
|
||||||
let builder = AstBuilder::new();
|
let builder = AstBuilder::new();
|
||||||
|
|
||||||
let field_ident = match field.node.ident() {
|
let field_ident = match field.ident {
|
||||||
Some(ident) => ident,
|
Some(ident) => ident,
|
||||||
None => { cx.span_bug(field.span, "struct field has no name?") }
|
None => builder.id(index.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut field_attrs = FieldAttrs {
|
let mut field_attrs = FieldAttrs {
|
||||||
name: Name::new(field_ident),
|
name: Name::new(field_ident),
|
||||||
skip_serializing_field: false,
|
skip_serializing_field: false,
|
||||||
skip_serializing_field_if: None,
|
skip_deserializing_field: false,
|
||||||
|
skip_serializing_if: None,
|
||||||
default_expr_if_missing: None,
|
default_expr_if_missing: None,
|
||||||
serialize_with: None,
|
serialize_with: None,
|
||||||
deserialize_with: None,
|
deserialize_with: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
for meta_items in field.node.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
match meta_item.node {
|
match meta_item.node {
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
@@ -249,41 +244,34 @@ impl FieldAttrs {
|
|||||||
field_attrs.skip_serializing_field = true;
|
field_attrs.skip_serializing_field = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse `#[serde(skip_deserializing)]`
|
||||||
|
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
||||||
|
field_attrs.skip_deserializing_field = true;
|
||||||
|
|
||||||
|
// Initialize field to Default::default() unless a different
|
||||||
|
// default is specified by `#[serde(default="...")]`
|
||||||
|
if field_attrs.default_expr_if_missing.is_none() {
|
||||||
|
let default_expr = builder.expr().default();
|
||||||
|
field_attrs.default_expr_if_missing = Some(default_expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(skip_serializing_if="...")]`
|
// Parse `#[serde(skip_serializing_if="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
||||||
let expr = wrap_skip_serializing(
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
||||||
field_ident,
|
field_attrs.skip_serializing_if = Some(path);
|
||||||
try!(parse_lit_into_path(cx, name, lit)),
|
|
||||||
is_enum,
|
|
||||||
);
|
|
||||||
|
|
||||||
field_attrs.skip_serializing_field_if = Some(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(serialize_with="...")]`
|
// Parse `#[serde(serialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
||||||
let expr = wrap_serialize_with(
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
||||||
cx,
|
field_attrs.serialize_with = Some(path);
|
||||||
container_ty,
|
|
||||||
generics,
|
|
||||||
field_ident,
|
|
||||||
try!(parse_lit_into_path(cx, name, lit)),
|
|
||||||
is_enum,
|
|
||||||
);
|
|
||||||
|
|
||||||
field_attrs.serialize_with = Some(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deserialize_with="...")]`
|
// Parse `#[serde(deserialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
||||||
let expr = wrap_deserialize_with(
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
||||||
cx,
|
field_attrs.deserialize_with = Some(path);
|
||||||
&field.node.ty,
|
|
||||||
generics,
|
|
||||||
try!(parse_lit_into_path(cx, name, lit)),
|
|
||||||
);
|
|
||||||
|
|
||||||
field_attrs.deserialize_with = Some(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
@@ -305,48 +293,43 @@ impl FieldAttrs {
|
|||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Predicate for using a field's default value
|
|
||||||
pub fn expr_is_missing(&self) -> P<ast::Expr> {
|
|
||||||
match self.default_expr_if_missing {
|
|
||||||
Some(ref expr) => expr.clone(),
|
|
||||||
None => {
|
|
||||||
let name = self.name.ident_expr();
|
|
||||||
AstBuilder::new().expr()
|
|
||||||
.try()
|
|
||||||
.method_call("missing_field").id("visitor")
|
|
||||||
.with_arg(name)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Predicate for ignoring a field when serializing a value
|
|
||||||
pub fn skip_serializing_field(&self) -> bool {
|
pub fn skip_serializing_field(&self) -> bool {
|
||||||
self.skip_serializing_field
|
self.skip_serializing_field
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_serializing_field_if(&self) -> Option<&P<ast::Expr>> {
|
pub fn skip_deserializing_field(&self) -> bool {
|
||||||
self.skip_serializing_field_if.as_ref()
|
self.skip_deserializing_field
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_with(&self) -> Option<&P<ast::Expr>> {
|
pub fn skip_serializing_if(&self) -> Option<&ast::Path> {
|
||||||
|
self.skip_serializing_if.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_expr_if_missing(&self) -> Option<&P<ast::Expr>> {
|
||||||
|
self.default_expr_if_missing.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialize_with(&self) -> Option<&ast::Path> {
|
||||||
self.serialize_with.as_ref()
|
self.serialize_with.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deserialize_with(&self) -> Option<&P<ast::Expr>> {
|
pub fn deserialize_with(&self) -> Option<&ast::Path> {
|
||||||
self.deserialize_with.as_ref()
|
self.deserialize_with.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Zip together fields and `#[serde(...)]` attributes on those fields.
|
||||||
pub fn get_struct_field_attrs(cx: &ExtCtxt,
|
pub fn fields_with_attrs<'a>(
|
||||||
container_ty: &P<ast::Ty>,
|
cx: &ExtCtxt,
|
||||||
generics: &ast::Generics,
|
fields: &'a [ast::StructField],
|
||||||
fields: &[ast::StructField],
|
) -> Result<Vec<(&'a ast::StructField, FieldAttrs)>, Error> {
|
||||||
is_enum: bool) -> Result<Vec<FieldAttrs>, Error> {
|
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.map(|field| FieldAttrs::from_field(cx, container_ty, generics, field, is_enum))
|
.enumerate()
|
||||||
|
.map(|(i, field)| {
|
||||||
|
let attrs = try!(FieldAttrs::from_field(cx, i, field));
|
||||||
|
Ok((field, attrs))
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -382,7 +365,7 @@ fn get_renames(cx: &ExtCtxt,
|
|||||||
Ok((ser_name, de_name))
|
Ok((ser_name, de_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
|
pub fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
|
||||||
match attr.node.value.node {
|
match attr.node.value.node {
|
||||||
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
||||||
attr::mark_used(&attr);
|
attr::mark_used(&attr);
|
||||||
@@ -462,18 +445,18 @@ fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::
|
|||||||
// error reporting, we'll first parse the string into a token tree. Then we'll update those
|
// error reporting, we'll first parse the string into a token tree. Then we'll update those
|
||||||
// spans to say they're coming from a macro context that originally came from the attribute,
|
// spans to say they're coming from a macro context that originally came from the attribute,
|
||||||
// and then finally parse them into an expression.
|
// and then finally parse them into an expression.
|
||||||
let tts = parse::parse_tts_from_source_str(
|
let tts = panictry!(parse::parse_tts_from_source_str(
|
||||||
format!("<serde {} expansion>", name),
|
format!("<serde {} expansion>", name),
|
||||||
(*source).to_owned(),
|
(*source).to_owned(),
|
||||||
cx.cfg(),
|
cx.cfg(),
|
||||||
cx.parse_sess());
|
cx.parse_sess()));
|
||||||
|
|
||||||
// Respan the spans to say they are all coming from this macro.
|
// Respan the spans to say they are all coming from this macro.
|
||||||
let tts = Respanner { cx: cx }.fold_tts(&tts);
|
let tts = Respanner { cx: cx }.fold_tts(&tts);
|
||||||
|
|
||||||
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
|
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
|
||||||
|
|
||||||
let path = match parser.parse_path(PathParsingMode::LifetimeAndTypesWithoutColons) {
|
let path = match parser.parse_path(PathStyle::Type) {
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(mut e) => {
|
Err(mut e) => {
|
||||||
e.emit();
|
e.emit();
|
||||||
@@ -500,134 +483,3 @@ fn wrap_default(path: ast::Path) -> P<ast::Expr> {
|
|||||||
.build_path(path)
|
.build_path(path)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(skip_serializing_if="...")]` in a trait to
|
|
||||||
/// prevent it from accessing the internal `Serialize` state.
|
|
||||||
fn wrap_skip_serializing(field_ident: ast::Ident,
|
|
||||||
path: ast::Path,
|
|
||||||
is_enum: bool) -> P<ast::Expr> {
|
|
||||||
let builder = AstBuilder::new();
|
|
||||||
|
|
||||||
let expr = builder.expr()
|
|
||||||
.field(field_ident)
|
|
||||||
.field("value")
|
|
||||||
.self_();
|
|
||||||
|
|
||||||
let expr = if is_enum {
|
|
||||||
expr
|
|
||||||
} else {
|
|
||||||
builder.expr().ref_().build(expr)
|
|
||||||
};
|
|
||||||
|
|
||||||
builder.expr().call()
|
|
||||||
.build_path(path)
|
|
||||||
.arg().build(expr)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(serialize_with="...")]` in a trait to
|
|
||||||
/// prevent it from accessing the internal `Serialize` state.
|
|
||||||
fn wrap_serialize_with(cx: &ExtCtxt,
|
|
||||||
container_ty: &P<ast::Ty>,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
field_ident: ast::Ident,
|
|
||||||
path: ast::Path,
|
|
||||||
is_enum: bool) -> P<ast::Expr> {
|
|
||||||
let builder = AstBuilder::new();
|
|
||||||
|
|
||||||
let expr = builder.expr()
|
|
||||||
.field(field_ident)
|
|
||||||
.self_();
|
|
||||||
|
|
||||||
let expr = if is_enum {
|
|
||||||
expr
|
|
||||||
} else {
|
|
||||||
builder.expr().ref_().build(expr)
|
|
||||||
};
|
|
||||||
|
|
||||||
let expr = builder.expr().call()
|
|
||||||
.build_path(path)
|
|
||||||
.arg().build(expr)
|
|
||||||
.arg()
|
|
||||||
.id("serializer")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let where_clause = &generics.where_clause;
|
|
||||||
|
|
||||||
quote_expr!(cx, {
|
|
||||||
trait __SerdeSerializeWith {
|
|
||||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
||||||
where S: ::serde::ser::Serializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> __SerdeSerializeWith for &'a T
|
|
||||||
where T: 'a + __SerdeSerializeWith,
|
|
||||||
{
|
|
||||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
||||||
where S: ::serde::ser::Serializer
|
|
||||||
{
|
|
||||||
(**self).__serde_serialize_with(serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $generics __SerdeSerializeWith for $container_ty $where_clause {
|
|
||||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
||||||
where S: ::serde::ser::Serializer
|
|
||||||
{
|
|
||||||
$expr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct __SerdeSerializeWithStruct<'a, T: 'a> {
|
|
||||||
value: &'a T,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> ::serde::ser::Serialize for __SerdeSerializeWithStruct<'a, T>
|
|
||||||
where T: 'a + __SerdeSerializeWith
|
|
||||||
{
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
||||||
where S: ::serde::ser::Serializer
|
|
||||||
{
|
|
||||||
self.value.__serde_serialize_with(serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
__SerdeSerializeWithStruct {
|
|
||||||
value: &self.value,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in a trait to prevent
|
|
||||||
/// it from accessing the internal `Deserialize` state.
|
|
||||||
fn wrap_deserialize_with(cx: &ExtCtxt,
|
|
||||||
field_ty: &P<ast::Ty>,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
path: ast::Path) -> P<ast::Expr> {
|
|
||||||
// Quasi-quoting doesn't do a great job of expanding generics into paths, so manually build it.
|
|
||||||
let ty_path = AstBuilder::new().path()
|
|
||||||
.segment("__SerdeDeserializeWithStruct")
|
|
||||||
.with_generics(generics.clone())
|
|
||||||
.build()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let where_clause = &generics.where_clause;
|
|
||||||
|
|
||||||
quote_expr!(cx, {
|
|
||||||
struct __SerdeDeserializeWithStruct $generics $where_clause {
|
|
||||||
value: $field_ty,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $generics ::serde::de::Deserialize for $ty_path $where_clause {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
|
||||||
where D: ::serde::de::Deserializer
|
|
||||||
{
|
|
||||||
let value = try!($path(deserializer));
|
|
||||||
Ok(__SerdeDeserializeWithStruct { value: value })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let value: $ty_path = try!(visitor.visit_value());
|
|
||||||
Ok(value.value)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use aster::AstBuilder;
|
||||||
|
|
||||||
|
use syntax::ast;
|
||||||
|
use syntax::ext::base::ExtCtxt;
|
||||||
|
use syntax::ptr::P;
|
||||||
|
use syntax::visit;
|
||||||
|
|
||||||
|
// Remove the default from every type parameter because in the generated impls
|
||||||
|
// they look like associated types: "error: associated type bindings are not
|
||||||
|
// allowed here".
|
||||||
|
pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
||||||
|
ast::Generics {
|
||||||
|
ty_params: generics.ty_params.iter().map(|ty_param| {
|
||||||
|
ast::TyParam {
|
||||||
|
default: None,
|
||||||
|
.. ty_param.clone()
|
||||||
|
}}).collect(),
|
||||||
|
.. generics.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_bound(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &AstBuilder,
|
||||||
|
item: &ast::Item,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
filter: &Fn(&ast::StructField) -> bool,
|
||||||
|
bound: &ast::Path,
|
||||||
|
) -> ast::Generics {
|
||||||
|
builder.from_generics(generics.clone())
|
||||||
|
.with_predicates(
|
||||||
|
all_variants(cx, item).iter()
|
||||||
|
.flat_map(|variant_data| all_struct_fields(variant_data))
|
||||||
|
.filter(|field| filter(field))
|
||||||
|
.map(|field| &field.ty)
|
||||||
|
// TODO this filter can be removed later, see comment on function
|
||||||
|
.filter(|ty| contains_generic(ty, generics))
|
||||||
|
.map(|ty| strip_reference(ty))
|
||||||
|
.map(|ty| builder.where_predicate()
|
||||||
|
// the type that is being bounded e.g. T
|
||||||
|
.bound().build(ty.clone())
|
||||||
|
// the bound e.g. Serialize
|
||||||
|
.bound().trait_(bound.clone()).build()
|
||||||
|
.build()))
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_variants<'a>(cx: &ExtCtxt, item: &'a ast::Item) -> Vec<&'a ast::VariantData> {
|
||||||
|
match item.node {
|
||||||
|
ast::ItemKind::Struct(ref variant_data, _) => {
|
||||||
|
vec![variant_data]
|
||||||
|
}
|
||||||
|
ast::ItemKind::Enum(ref enum_def, _) => {
|
||||||
|
enum_def.variants.iter()
|
||||||
|
.map(|variant| &variant.node.data)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
cx.span_bug(item.span, "expected Item to be Struct or Enum");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_struct_fields(variant_data: &ast::VariantData) -> &[ast::StructField] {
|
||||||
|
match *variant_data {
|
||||||
|
ast::VariantData::Struct(ref fields, _) |
|
||||||
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
|
fields
|
||||||
|
}
|
||||||
|
ast::VariantData::Unit(_) => {
|
||||||
|
&[]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rust <1.7 enforces that `where` clauses involve generic type parameters. The
|
||||||
|
// corresponding compiler error is E0193. It is no longer enforced in Rust >=1.7
|
||||||
|
// so this filtering can be removed in the future when we stop supporting <1.7.
|
||||||
|
//
|
||||||
|
// E0193 means we must not generate a `where` clause like `i32: Serialize`
|
||||||
|
// because even though i32 implements Serialize, i32 is not a generic type
|
||||||
|
// parameter. Clauses like `T: Serialize` and `Option<T>: Serialize` are okay.
|
||||||
|
// This function decides whether a given type references any of the generic type
|
||||||
|
// parameters in the input `Generics`.
|
||||||
|
fn contains_generic(ty: &ast::Ty, generics: &ast::Generics) -> bool {
|
||||||
|
struct FindGeneric<'a> {
|
||||||
|
generic_names: &'a HashSet<ast::Name>,
|
||||||
|
found_generic: bool,
|
||||||
|
}
|
||||||
|
impl<'a, 'v> visit::Visitor<'v> for FindGeneric<'a> {
|
||||||
|
fn visit_path(&mut self, path: &'v ast::Path, _id: ast::NodeId) {
|
||||||
|
if !path.global
|
||||||
|
&& path.segments.len() == 1
|
||||||
|
&& self.generic_names.contains(&path.segments[0].identifier.name) {
|
||||||
|
self.found_generic = true;
|
||||||
|
} else {
|
||||||
|
visit::walk_path(self, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let generic_names: HashSet<_> = generics.ty_params.iter()
|
||||||
|
.map(|ty_param| ty_param.ident.name)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut visitor = FindGeneric {
|
||||||
|
generic_names: &generic_names,
|
||||||
|
found_generic: false,
|
||||||
|
};
|
||||||
|
visit::walk_ty(&mut visitor, ty);
|
||||||
|
visitor.found_generic
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
+488
-381
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
|||||||
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
||||||
#![cfg_attr(feature = "nightly-testing", feature(plugin))]
|
#![cfg_attr(feature = "nightly-testing", feature(plugin))]
|
||||||
|
#![cfg_attr(feature = "nightly-testing", allow(too_many_arguments))]
|
||||||
#![cfg_attr(feature = "nightly-testing", allow(used_underscore_binding))]
|
#![cfg_attr(feature = "nightly-testing", allow(used_underscore_binding))]
|
||||||
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
||||||
#![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))]
|
#![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))]
|
||||||
@@ -11,9 +12,11 @@ extern crate quasi;
|
|||||||
extern crate syntex;
|
extern crate syntex;
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
#[cfg(feature = "with-syntex")]
|
||||||
|
#[macro_use]
|
||||||
extern crate syntex_syntax as syntax;
|
extern crate syntex_syntax as syntax;
|
||||||
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
#[cfg(not(feature = "with-syntex"))]
|
||||||
|
#[macro_use]
|
||||||
extern crate syntax;
|
extern crate syntax;
|
||||||
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
#[cfg(not(feature = "with-syntex"))]
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod attr;
|
mod attr;
|
||||||
|
mod bound;
|
||||||
mod de;
|
mod de;
|
||||||
mod error;
|
mod error;
|
||||||
mod ser;
|
mod ser;
|
||||||
|
|||||||
+246
-110
@@ -8,10 +8,10 @@ use syntax::ast::{
|
|||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
use syntax::ext::build::AstBuilder;
|
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
use attr;
|
use attr;
|
||||||
|
use bound;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
||||||
pub fn expand_derive_serialize(
|
pub fn expand_derive_serialize(
|
||||||
@@ -60,11 +60,7 @@ fn serialize_item(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let impl_generics = builder.from_generics(generics.clone())
|
let impl_generics = build_impl_generics(cx, builder, item, generics);
|
||||||
.add_ty_param_bound(
|
|
||||||
builder.path().global().ids(&["serde", "ser", "Serialize"]).build()
|
|
||||||
)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
let ty = builder.ty().path()
|
||||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||||
@@ -78,15 +74,57 @@ fn serialize_item(
|
|||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
|
let dummy_const = builder.id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
||||||
|
|
||||||
Ok(quote_item!(cx,
|
Ok(quote_item!(cx,
|
||||||
impl $impl_generics ::serde::ser::Serialize for $ty $where_clause {
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||||
fn serialize<__S>(&self, serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
const $dummy_const: () = {
|
||||||
where __S: ::serde::ser::Serializer,
|
extern crate serde as _serde;
|
||||||
{
|
#[automatically_derived]
|
||||||
$body
|
impl $impl_generics _serde::ser::Serialize for $ty $where_clause {
|
||||||
|
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||||
|
where __S: _serde::ser::Serializer,
|
||||||
|
{
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
||||||
|
// field type that will be serialized by us.
|
||||||
|
fn build_impl_generics(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
item: &Item,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
) -> ast::Generics {
|
||||||
|
let generics = bound::without_defaults(generics);
|
||||||
|
let generics = bound::with_bound(cx, builder, item, &generics,
|
||||||
|
&serialized_by_us,
|
||||||
|
&builder.path().ids(&["_serde", "ser", "Serialize"]).build());
|
||||||
|
generics
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields with a `skip_serializing` or `serialize_with` attribute are not
|
||||||
|
// serialized by us. All other fields may need a `T: Serialize` bound where T is
|
||||||
|
// the type of the field.
|
||||||
|
fn serialized_by_us(field: &ast::StructField) -> bool {
|
||||||
|
for meta_items in field.attrs.iter().filter_map(attr::get_serde_meta_items) {
|
||||||
|
for meta_item in meta_items {
|
||||||
|
match meta_item.node {
|
||||||
|
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
ast::MetaItemKind::NameValue(ref name, _) if name == &"serialize_with" => {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
).unwrap())
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_body(
|
fn serialize_body(
|
||||||
@@ -147,11 +185,15 @@ fn serialize_item_struct(
|
|||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
serialize_newtype_struct(
|
serialize_newtype_struct(
|
||||||
cx,
|
cx,
|
||||||
|
&builder,
|
||||||
|
impl_generics,
|
||||||
|
ty,
|
||||||
|
&fields[0],
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
if fields.iter().any(|field| field.ident.is_some()) {
|
||||||
cx.span_bug(span, "tuple struct has named fields")
|
cx.span_bug(span, "tuple struct has named fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,12 +202,12 @@ fn serialize_item_struct(
|
|||||||
&builder,
|
&builder,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields.len(),
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
cx.span_bug(span, "struct has unnamed fields")
|
cx.span_bug(span, "struct has unnamed fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,18 +230,30 @@ fn serialize_unit_struct(
|
|||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
Ok(quote_expr!(cx,
|
||||||
serializer.serialize_unit_struct($type_name)
|
_serializer.serialize_unit_struct($type_name)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct(
|
fn serialize_newtype_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
impl_generics: &ast::Generics,
|
||||||
|
container_ty: P<ast::Ty>,
|
||||||
|
field: &ast::StructField,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
|
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
||||||
|
|
||||||
|
let mut field_expr = quote_expr!(cx, &self.0);
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&container_ty, impl_generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
Ok(quote_expr!(cx,
|
||||||
serializer.serialize_newtype_struct($type_name, &self.0)
|
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,10 +262,10 @@ fn serialize_tuple_struct(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: usize,
|
fields: &[ast::StructField],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
@@ -222,14 +276,15 @@ fn serialize_tuple_struct(
|
|||||||
builder.id("serialize_tuple_struct_elt"),
|
builder.id("serialize_tuple_struct_elt"),
|
||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
);
|
false,
|
||||||
|
));
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
serializer.serialize_tuple_struct($type_name, Visitor {
|
_serializer.serialize_tuple_struct($type_name, Visitor {
|
||||||
value: self,
|
value: self,
|
||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||||
@@ -264,7 +319,7 @@ fn serialize_struct(
|
|||||||
Ok(quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
serializer.serialize_struct($type_name, Visitor {
|
_serializer.serialize_struct($type_name, Visitor {
|
||||||
value: self,
|
value: self,
|
||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||||
@@ -324,14 +379,10 @@ fn serialize_variant(
|
|||||||
|
|
||||||
match variant.node.data {
|
match variant.node.data {
|
||||||
ast::VariantData::Unit(_) => {
|
ast::VariantData::Unit(_) => {
|
||||||
let pat = builder.pat().path()
|
|
||||||
.id(type_ident).id(variant_ident)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => {
|
$type_ident::$variant_ident => {
|
||||||
::serde::ser::Serializer::serialize_unit_variant(
|
_serde::ser::Serializer::serialize_unit_variant(
|
||||||
serializer,
|
_serializer,
|
||||||
$type_name,
|
$type_name,
|
||||||
$variant_index,
|
$variant_index,
|
||||||
$variant_name,
|
$variant_name,
|
||||||
@@ -340,23 +391,19 @@ fn serialize_variant(
|
|||||||
))
|
))
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
let field = builder.id("__simple_value");
|
let expr = try!(serialize_newtype_variant(
|
||||||
let field = builder.pat().ref_id(field);
|
cx,
|
||||||
let pat = builder.pat().enum_()
|
builder,
|
||||||
.id(type_ident).id(variant_ident).build()
|
type_name,
|
||||||
.with_pats(Some(field).into_iter())
|
variant_index,
|
||||||
.build();
|
variant_name,
|
||||||
|
ty,
|
||||||
|
generics,
|
||||||
|
&fields[0],
|
||||||
|
));
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => {
|
$type_ident::$variant_ident(ref __simple_value) => { $expr }
|
||||||
::serde::ser::Serializer::serialize_newtype_variant(
|
|
||||||
serializer,
|
|
||||||
$type_name,
|
|
||||||
$variant_index,
|
|
||||||
$variant_name,
|
|
||||||
__simple_value,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
@@ -372,7 +419,7 @@ fn serialize_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = serialize_tuple_variant(
|
let expr = try!(serialize_tuple_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_name,
|
type_name,
|
||||||
@@ -382,7 +429,7 @@ fn serialize_variant(
|
|||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
field_names,
|
field_names,
|
||||||
);
|
));
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => { $expr }
|
$pat => { $expr }
|
||||||
@@ -399,9 +446,9 @@ fn serialize_variant(
|
|||||||
field_names.iter()
|
field_names.iter()
|
||||||
.zip(fields.iter())
|
.zip(fields.iter())
|
||||||
.map(|(id, field)| {
|
.map(|(id, field)| {
|
||||||
let name = match field.node.kind {
|
let name = match field.ident {
|
||||||
ast::NamedField(name, _) => name,
|
Some(name) => name,
|
||||||
ast::UnnamedField(_) => {
|
None => {
|
||||||
cx.span_bug(field.span, "struct variant has unnamed fields")
|
cx.span_bug(field.span, "struct variant has unnamed fields")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -430,6 +477,35 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_newtype_variant(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
type_name: P<ast::Expr>,
|
||||||
|
variant_index: usize,
|
||||||
|
variant_name: P<ast::Expr>,
|
||||||
|
container_ty: P<ast::Ty>,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
field: &ast::StructField,
|
||||||
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
|
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
||||||
|
|
||||||
|
let mut field_expr = quote_expr!(cx, __simple_value);
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&container_ty, generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(quote_expr!(cx,
|
||||||
|
_serde::ser::Serializer::serialize_newtype_variant(
|
||||||
|
_serializer,
|
||||||
|
$type_name,
|
||||||
|
$variant_index,
|
||||||
|
$variant_name,
|
||||||
|
$field_expr,
|
||||||
|
)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
@@ -440,27 +516,28 @@ fn serialize_tuple_variant(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[ast::StructField],
|
||||||
field_names: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
) -> P<ast::Expr> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let variant_ty = builder.ty().tuple()
|
let variant_ty = builder.ty().tuple()
|
||||||
.with_tys(
|
.with_tys(
|
||||||
fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
builder.ty()
|
builder.ty()
|
||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__a")
|
.lifetime("'__a")
|
||||||
.build_ty(field.node.ty.clone())
|
.build_ty(field.ty.clone())
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
structure_ty.clone(),
|
structure_ty.clone(),
|
||||||
variant_ty,
|
variant_ty,
|
||||||
builder.id("serialize_tuple_variant_elt"),
|
builder.id("serialize_tuple_variant_elt"),
|
||||||
fields.len(),
|
fields,
|
||||||
generics,
|
generics,
|
||||||
);
|
true,
|
||||||
|
));
|
||||||
|
|
||||||
let value_expr = builder.expr().tuple()
|
let value_expr = builder.expr().tuple()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
@@ -470,15 +547,15 @@ fn serialize_tuple_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||||
value: $value_expr,
|
value: $value_expr,
|
||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||||
})
|
})
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(
|
||||||
@@ -502,12 +579,12 @@ fn serialize_struct_variant(
|
|||||||
.with_generics(variant_generics.clone())
|
.with_generics(variant_generics.clone())
|
||||||
.with_fields(
|
.with_fields(
|
||||||
fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
builder.struct_field(field.node.ident().expect("struct has unnamed fields"))
|
builder.struct_field(field.ident.expect("struct has unnamed fields"))
|
||||||
.with_attrs(field.node.attrs.iter().cloned())
|
.with_attrs(field.attrs.iter().cloned())
|
||||||
.ty()
|
.ty()
|
||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__serde_variant")
|
.lifetime("'__serde_variant")
|
||||||
.build_ty(field.node.ty.clone())
|
.build_ty(field.ty.clone())
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.field("__serde_container_ty")
|
.field("__serde_container_ty")
|
||||||
@@ -520,7 +597,7 @@ fn serialize_struct_variant(
|
|||||||
.zip(field_names.iter())
|
.zip(field_names.iter())
|
||||||
.map(|(field, field_name)| {
|
.map(|(field, field_name)| {
|
||||||
(
|
(
|
||||||
field.node.ident().expect("struct has unnamed fields"),
|
field.ident.expect("struct has unnamed fields"),
|
||||||
builder.expr().id(field_name),
|
builder.expr().id(field_name),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -557,7 +634,7 @@ fn serialize_struct_variant(
|
|||||||
$variant_struct
|
$variant_struct
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
serializer.serialize_struct_variant(
|
_serializer.serialize_struct_variant(
|
||||||
$container_name,
|
$container_name,
|
||||||
$variant_index,
|
$variant_index,
|
||||||
$variant_name,
|
$variant_name,
|
||||||
@@ -576,20 +653,33 @@ fn serialize_tuple_struct_visitor(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
variant_ty: P<ast::Ty>,
|
variant_ty: P<ast::Ty>,
|
||||||
serializer_method: ast::Ident,
|
serializer_method: ast::Ident,
|
||||||
fields: usize,
|
fields: &[ast::StructField],
|
||||||
generics: &ast::Generics
|
generics: &ast::Generics,
|
||||||
) -> (P<ast::Item>, P<ast::Item>) {
|
is_enum: bool,
|
||||||
let arms: Vec<ast::Arm> = (0 .. fields)
|
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
||||||
.map(|i| {
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
let expr = builder.expr().method_call(serializer_method)
|
|
||||||
.id("serializer")
|
let arms: Vec<_> = fields_with_attrs.iter()
|
||||||
.arg().ref_().tup_field(i).field("value").self_()
|
.enumerate()
|
||||||
.build();
|
.map(|(i, &(field, ref attrs))| {
|
||||||
|
let mut field_expr = builder.expr().tup_field(i).field("value").self_();
|
||||||
|
if !is_enum {
|
||||||
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
let continue_if_skip = attrs.skip_serializing_if()
|
||||||
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
|
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&structure_ty, generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$i => {
|
$i => {
|
||||||
self.state += 1;
|
self.state += 1;
|
||||||
Ok(Some(try!($expr)))
|
$continue_if_skip
|
||||||
|
Ok(Some(try!(_serializer.$serializer_method($field_expr))))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -606,7 +696,9 @@ fn serialize_tuple_struct_visitor(
|
|||||||
.strip_bounds()
|
.strip_bounds()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
(
|
let nfields = fields.len();
|
||||||
|
|
||||||
|
Ok((
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
struct Visitor $visitor_impl_generics $where_clause {
|
struct Visitor $visitor_impl_generics $where_clause {
|
||||||
state: usize,
|
state: usize,
|
||||||
@@ -616,12 +708,12 @@ fn serialize_tuple_struct_visitor(
|
|||||||
).unwrap(),
|
).unwrap(),
|
||||||
|
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
impl $visitor_impl_generics ::serde::ser::SeqVisitor
|
impl $visitor_impl_generics _serde::ser::SeqVisitor
|
||||||
for Visitor $visitor_generics
|
for Visitor $visitor_generics
|
||||||
$where_clause {
|
$where_clause {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
fn visit<__S>(&mut self, _serializer: &mut __S) -> ::std::result::Result<Option<()>, __S::Error>
|
||||||
where S: ::serde::ser::Serializer
|
where __S: _serde::ser::Serializer
|
||||||
{
|
{
|
||||||
match self.state {
|
match self.state {
|
||||||
$arms
|
$arms
|
||||||
@@ -631,11 +723,11 @@ fn serialize_tuple_struct_visitor(
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn len(&self) -> Option<usize> {
|
fn len(&self) -> Option<usize> {
|
||||||
Some($fields)
|
Some($nfields)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_visitor(
|
fn serialize_struct_visitor(
|
||||||
@@ -648,37 +740,36 @@ fn serialize_struct_visitor(
|
|||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
||||||
let field_attrs = try!(
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
attr::get_struct_field_attrs(cx, &structure_ty, generics, fields, is_enum)
|
|
||||||
);
|
|
||||||
|
|
||||||
let arms: Vec<ast::Arm> = fields.iter().zip(field_attrs.iter())
|
let arms: Vec<ast::Arm> = fields_with_attrs.iter()
|
||||||
.filter(|&(_, ref field_attr)| !field_attr.skip_serializing_field())
|
.filter(|&&(_, ref attrs)| !attrs.skip_serializing_field())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, (ref field, ref field_attr))| {
|
.map(|(i, &(field, ref attrs))| {
|
||||||
let name = field.node.ident().expect("struct has unnamed field");
|
let ident = field.ident.expect("struct has unnamed field");
|
||||||
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
|
if !is_enum {
|
||||||
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
let key_expr = field_attr.name().serialize_name_expr();
|
let key_expr = attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
let stmt = if let Some(expr) = field_attr.skip_serializing_field_if() {
|
let continue_if_skip = attrs.skip_serializing_if()
|
||||||
Some(quote_stmt!(cx, if $expr { continue; }))
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let field_expr = match field_attr.serialize_with() {
|
if let Some(path) = attrs.serialize_with() {
|
||||||
Some(expr) => expr.clone(),
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
None => quote_expr!(cx, &self.value.$name),
|
&structure_ty, generics, &field.ty, path, field_expr)
|
||||||
};
|
}
|
||||||
|
|
||||||
let expr = quote_expr!(cx,
|
let expr = quote_expr!(cx,
|
||||||
serializer.$serializer_method($key_expr, $field_expr)
|
_serializer.$serializer_method($key_expr, $field_expr)
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$i => {
|
$i => {
|
||||||
self.state += 1;
|
self.state += 1;
|
||||||
$stmt
|
$continue_if_skip
|
||||||
return Ok(Some(try!($expr)));
|
return Ok(Some(try!($expr)));
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -696,16 +787,18 @@ fn serialize_struct_visitor(
|
|||||||
.strip_bounds()
|
.strip_bounds()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let len = field_attrs.iter()
|
let len = fields_with_attrs.iter()
|
||||||
.filter(|field_attr| !field_attr.skip_serializing_field())
|
.filter(|&&(_, ref attrs)| !attrs.skip_serializing_field())
|
||||||
.map(|field_attr| {
|
.map(|&(field, ref attrs)| {
|
||||||
match field_attr.skip_serializing_field_if() {
|
let ident = field.ident.expect("struct has unnamed fields");
|
||||||
Some(expr) => {
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
quote_expr!(cx, if $expr { 0 } else { 1 })
|
if !is_enum {
|
||||||
}
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
None => {
|
}
|
||||||
quote_expr!(cx, 1)
|
|
||||||
}
|
match attrs.skip_serializing_if() {
|
||||||
|
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
||||||
|
None => quote_expr!(cx, 1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||||
@@ -721,12 +814,12 @@ fn serialize_struct_visitor(
|
|||||||
|
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
impl $visitor_impl_generics
|
impl $visitor_impl_generics
|
||||||
::serde::ser::MapVisitor
|
_serde::ser::MapVisitor
|
||||||
for Visitor $visitor_generics
|
for Visitor $visitor_generics
|
||||||
$where_clause {
|
$where_clause {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
fn visit<__S>(&mut self, _serializer: &mut __S) -> ::std::result::Result<Option<()>, __S::Error>
|
||||||
where S: ::serde::ser::Serializer,
|
where __S: _serde::ser::Serializer,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match self.state {
|
match self.state {
|
||||||
@@ -744,3 +837,46 @@ fn serialize_struct_visitor(
|
|||||||
).unwrap(),
|
).unwrap(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wrap_serialize_with(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
container_ty: &P<ast::Ty>,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
field_ty: &P<ast::Ty>,
|
||||||
|
path: &ast::Path,
|
||||||
|
value: P<ast::Expr>,
|
||||||
|
) -> P<ast::Expr> {
|
||||||
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
|
let wrapper_generics = builder.from_generics(generics.clone())
|
||||||
|
.add_lifetime_bound("'__a")
|
||||||
|
.lifetime_name("'__a")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let wrapper_ty = builder.path()
|
||||||
|
.segment("__SerializeWith")
|
||||||
|
.with_generics(wrapper_generics.clone())
|
||||||
|
.build()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
quote_expr!(cx, {
|
||||||
|
struct __SerializeWith $wrapper_generics $where_clause {
|
||||||
|
value: &'__a $field_ty,
|
||||||
|
phantom: ::std::marker::PhantomData<$container_ty>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl $wrapper_generics _serde::ser::Serialize for $wrapper_ty $where_clause {
|
||||||
|
fn serialize<__S>(&self, __s: &mut __S) -> Result<(), __S::Error>
|
||||||
|
where __S: _serde::ser::Serializer
|
||||||
|
{
|
||||||
|
$path(self.value, __s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__SerializeWith {
|
||||||
|
value: $value,
|
||||||
|
phantom: ::std::marker::PhantomData::<$container_ty>,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
version = "0.7.0"
|
version = "0.7.7"
|
||||||
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"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
documentation = "https://github.com/serde-rs/serde"
|
documentation = "https://github.com/serde-rs/serde"
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
@@ -17,12 +18,12 @@ nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-tes
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
serde_codegen = { version = "^0.7.0", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
serde_codegen = { version = "^0.7.7", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
compiletest_rs = "^0.0.11"
|
compiletest_rs = "^0.1.1"
|
||||||
rustc-serialize = "^0.3.16"
|
rustc-serialize = "^0.3.16"
|
||||||
serde = { version = "^0.7.0", path = "../serde" }
|
serde = { version = "^0.7.7", path = "../serde" }
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "test"
|
name = "test"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_tests"
|
name = "serde_tests"
|
||||||
version = "0.6.3"
|
version = "0.7.6"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
@@ -14,14 +14,14 @@ build = "build.rs"
|
|||||||
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
|
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
syntex = { version = "^0.29.0" }
|
syntex = { version = "^0.33.0" }
|
||||||
syntex_syntax = { version = "^0.29.0" }
|
syntex_syntax = { version = "^0.33.0" }
|
||||||
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rustc-serialize = "^0.3.16"
|
rustc-serialize = "^0.3.16"
|
||||||
serde = { version = "*", path = "../serde" }
|
serde = { version = "*", path = "../serde" }
|
||||||
syntex = "^0.29.0"
|
syntex = "^0.33.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use rustc_serialize::{Decoder, Decodable};
|
use rustc_serialize::Decodable;
|
||||||
use serde;
|
use serde;
|
||||||
use serde::de::{Deserializer, Deserialize};
|
use serde::de::Deserialize;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ use test::Bencher;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
use rustc_serialize::{Decoder, Decodable};
|
use rustc_serialize::Decodable;
|
||||||
|
|
||||||
use serde;
|
use serde;
|
||||||
use serde::de::{Deserializer, Deserialize};
|
use serde::de::Deserialize;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#![cfg_attr(feature = "nightly", feature(plugin))]
|
#![cfg_attr(feature = "nightly", feature(plugin))]
|
||||||
#![cfg_attr(feature = "nightly", plugin(clippy))]
|
#![cfg_attr(feature = "nightly", plugin(clippy))]
|
||||||
|
|
||||||
extern crate serde;
|
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||||
|
|||||||
@@ -6,5 +6,6 @@ mod token;
|
|||||||
mod test_annotations;
|
mod test_annotations;
|
||||||
mod test_bytes;
|
mod test_bytes;
|
||||||
mod test_de;
|
mod test_de;
|
||||||
|
mod test_gen;
|
||||||
mod test_macros;
|
mod test_macros;
|
||||||
mod test_ser;
|
mod test_ser;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::default::Default;
|
extern crate serde;
|
||||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
use self::serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
|
|
||||||
use token::{
|
use token::{
|
||||||
Error,
|
Error,
|
||||||
@@ -10,23 +10,33 @@ use token::{
|
|||||||
assert_de_tokens_error
|
assert_de_tokens_error
|
||||||
};
|
};
|
||||||
|
|
||||||
trait Trait: Sized {
|
trait MyDefault: Sized {
|
||||||
fn my_default() -> Self;
|
fn my_default() -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ShouldSkip: Sized {
|
||||||
fn should_skip(&self) -> bool;
|
fn should_skip(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait SerializeWith: Sized {
|
||||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait DeserializeWith: Sized {
|
||||||
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer;
|
where D: Deserializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Trait for i32 {
|
impl MyDefault for i32 {
|
||||||
fn my_default() -> Self { 123 }
|
fn my_default() -> Self { 123 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShouldSkip for i32 {
|
||||||
fn should_skip(&self) -> bool { *self == 123 }
|
fn should_skip(&self) -> bool { *self == 123 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerializeWith for i32 {
|
||||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
@@ -36,7 +46,9 @@ impl Trait for i32 {
|
|||||||
false.serialize(ser)
|
false.serialize(ser)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeserializeWith for i32 {
|
||||||
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
||||||
where D: Deserializer
|
where D: Deserializer
|
||||||
{
|
{
|
||||||
@@ -49,18 +61,25 @@ impl Trait for i32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
struct DefaultStruct<A, B: Default, C> where C: Trait {
|
struct DefaultStruct<A, B, C, D, E>
|
||||||
|
where C: MyDefault,
|
||||||
|
E: MyDefault,
|
||||||
|
{
|
||||||
a1: A,
|
a1: A,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
a2: B,
|
a2: B,
|
||||||
#[serde(default="Trait::my_default")]
|
#[serde(default="MyDefault::my_default")]
|
||||||
a3: C,
|
a3: C,
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
a4: D,
|
||||||
|
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||||
|
a5: E,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_struct() {
|
fn test_default_struct() {
|
||||||
assert_de_tokens(
|
assert_de_tokens(
|
||||||
&DefaultStruct { a1: 1, a2: 2, a3: 3 },
|
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||||
vec![
|
vec![
|
||||||
Token::StructStart("DefaultStruct", Some(3)),
|
Token::StructStart("DefaultStruct", Some(3)),
|
||||||
|
|
||||||
@@ -76,12 +95,20 @@ fn test_default_struct() {
|
|||||||
Token::Str("a3"),
|
Token::Str("a3"),
|
||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a4"),
|
||||||
|
Token::I32(4),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a5"),
|
||||||
|
Token::I32(5),
|
||||||
|
|
||||||
Token::StructEnd,
|
Token::StructEnd,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_de_tokens(
|
assert_de_tokens(
|
||||||
&DefaultStruct { a1: 1, a2: 0, a3: 123 },
|
&DefaultStruct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||||
vec![
|
vec![
|
||||||
Token::StructStart("DefaultStruct", Some(1)),
|
Token::StructStart("DefaultStruct", Some(1)),
|
||||||
|
|
||||||
@@ -95,22 +122,29 @@ fn test_default_struct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
enum DefaultEnum<A, B: Default, C> where C: Trait {
|
enum DefaultEnum<A, B, C, D, E>
|
||||||
|
where C: MyDefault,
|
||||||
|
E: MyDefault
|
||||||
|
{
|
||||||
Struct {
|
Struct {
|
||||||
a1: A,
|
a1: A,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
a2: B,
|
a2: B,
|
||||||
#[serde(default="Trait::my_default")]
|
#[serde(default="MyDefault::my_default")]
|
||||||
a3: C,
|
a3: C,
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
a4: D,
|
||||||
|
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||||
|
a5: E,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_enum() {
|
fn test_default_enum() {
|
||||||
assert_de_tokens(
|
assert_de_tokens(
|
||||||
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3 },
|
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||||
vec![
|
vec![
|
||||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(3)),
|
Token::EnumMapStart("DefaultEnum", "Struct", Some(5)),
|
||||||
|
|
||||||
Token::EnumMapSep,
|
Token::EnumMapSep,
|
||||||
Token::Str("a1"),
|
Token::Str("a1"),
|
||||||
@@ -124,14 +158,22 @@ fn test_default_enum() {
|
|||||||
Token::Str("a3"),
|
Token::Str("a3"),
|
||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("a4"),
|
||||||
|
Token::I32(4),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("a5"),
|
||||||
|
Token::I32(5),
|
||||||
|
|
||||||
Token::EnumMapEnd,
|
Token::EnumMapEnd,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_de_tokens(
|
assert_de_tokens(
|
||||||
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123 },
|
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||||
vec![
|
vec![
|
||||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(3)),
|
Token::EnumMapStart("DefaultEnum", "Struct", Some(5)),
|
||||||
|
|
||||||
Token::EnumMapSep,
|
Token::EnumMapSep,
|
||||||
Token::Str("a1"),
|
Token::Str("a1"),
|
||||||
@@ -142,6 +184,108 @@ fn test_default_enum() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Does not implement std::default::Default.
|
||||||
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
|
struct NoStdDefault(i8);
|
||||||
|
|
||||||
|
impl MyDefault for NoStdDefault {
|
||||||
|
fn my_default() -> Self {
|
||||||
|
NoStdDefault(123)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
|
struct ContainsNoStdDefault<A: MyDefault> {
|
||||||
|
#[serde(default="MyDefault::my_default")]
|
||||||
|
a: A,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that a struct field does not need to implement std::default::Default if
|
||||||
|
// it is annotated with `default=...`.
|
||||||
|
#[test]
|
||||||
|
fn test_no_std_default() {
|
||||||
|
assert_de_tokens(
|
||||||
|
&ContainsNoStdDefault { a: NoStdDefault(123) },
|
||||||
|
vec![
|
||||||
|
Token::StructStart("ContainsNoStdDefault", Some(1)),
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_de_tokens(
|
||||||
|
&ContainsNoStdDefault { a: NoStdDefault(8) },
|
||||||
|
vec![
|
||||||
|
Token::StructStart("ContainsNoStdDefault", Some(1)),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::StructNewType("NoStdDefault"),
|
||||||
|
Token::I8(8),
|
||||||
|
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Does not implement Deserialize.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct NotDeserializeStruct(i8);
|
||||||
|
|
||||||
|
impl Default for NotDeserializeStruct {
|
||||||
|
fn default() -> Self {
|
||||||
|
NotDeserializeStruct(123)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeserializeWith for NotDeserializeStruct {
|
||||||
|
fn deserialize_with<D>(_: &mut D) -> Result<Self, D::Error>
|
||||||
|
where D: Deserializer
|
||||||
|
{
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Does not implement Deserialize.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
enum NotDeserializeEnum { Trouble }
|
||||||
|
|
||||||
|
impl MyDefault for NotDeserializeEnum {
|
||||||
|
fn my_default() -> Self {
|
||||||
|
NotDeserializeEnum::Trouble
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
|
struct ContainsNotDeserialize<A, B, C: DeserializeWith, E: MyDefault> {
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
a: A,
|
||||||
|
#[serde(skip_deserializing, default)]
|
||||||
|
b: B,
|
||||||
|
#[serde(deserialize_with="DeserializeWith::deserialize_with", default)]
|
||||||
|
c: C,
|
||||||
|
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||||
|
e: E,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that a struct field does not need to implement Deserialize if it is
|
||||||
|
// annotated with skip_deserializing, whether using the std Default or a
|
||||||
|
// custom default.
|
||||||
|
#[test]
|
||||||
|
fn test_elt_not_deserialize() {
|
||||||
|
assert_de_tokens(
|
||||||
|
&ContainsNotDeserialize {
|
||||||
|
a: NotDeserializeStruct(123),
|
||||||
|
b: NotDeserializeStruct(123),
|
||||||
|
c: NotDeserializeStruct(123),
|
||||||
|
e: NotDeserializeEnum::Trouble,
|
||||||
|
},
|
||||||
|
vec![
|
||||||
|
Token::StructStart("ContainsNotDeserialize", Some(3)),
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
struct DenyUnknown {
|
struct DenyUnknown {
|
||||||
@@ -152,7 +296,7 @@ struct DenyUnknown {
|
|||||||
fn test_ignore_unknown() {
|
fn test_ignore_unknown() {
|
||||||
// 'Default' allows unknown. Basic smoke test of ignore...
|
// 'Default' allows unknown. Basic smoke test of ignore...
|
||||||
assert_de_tokens(
|
assert_de_tokens(
|
||||||
&DefaultStruct { a1: 1, a2: 2, a3: 3 },
|
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||||
vec![
|
vec![
|
||||||
Token::StructStart("DefaultStruct", Some(5)),
|
Token::StructStart("DefaultStruct", Some(5)),
|
||||||
|
|
||||||
@@ -389,11 +533,11 @@ fn test_rename_enum() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
struct SkipSerializingStruct<'a, B, C> where C: Trait {
|
struct SkipSerializingStruct<'a, B, C> where C: ShouldSkip {
|
||||||
a: &'a i8,
|
a: &'a i8,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
b: B,
|
b: B,
|
||||||
#[serde(skip_serializing_if="Trait::should_skip")]
|
#[serde(skip_serializing_if="ShouldSkip::should_skip")]
|
||||||
c: C,
|
c: C,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,12 +584,12 @@ fn test_skip_serializing_struct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
enum SkipSerializingEnum<'a, B, C> where C: Trait {
|
enum SkipSerializingEnum<'a, B, C> where C: ShouldSkip {
|
||||||
Struct {
|
Struct {
|
||||||
a: &'a i8,
|
a: &'a i8,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
_b: B,
|
_b: B,
|
||||||
#[serde(skip_serializing_if="Trait::should_skip")]
|
#[serde(skip_serializing_if="ShouldSkip::should_skip")]
|
||||||
c: C,
|
c: C,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -492,10 +636,62 @@ fn test_skip_serializing_enum() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct NotSerializeStruct(i8);
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
enum NotSerializeEnum { Trouble }
|
||||||
|
|
||||||
|
impl SerializeWith for NotSerializeEnum {
|
||||||
|
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
|
"trouble".serialize(ser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
struct SerializeWithStruct<'a, B> where B: Trait {
|
struct ContainsNotSerialize<'a, B, C, D> where B: 'a, D: SerializeWith {
|
||||||
|
a: &'a Option<i8>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
b: &'a B,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
c: Option<C>,
|
||||||
|
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||||
|
d: D,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_elt_not_serialize() {
|
||||||
|
let a = 1;
|
||||||
|
assert_ser_tokens(
|
||||||
|
&ContainsNotSerialize {
|
||||||
|
a: &Some(a),
|
||||||
|
b: &NotSerializeStruct(2),
|
||||||
|
c: Some(NotSerializeEnum::Trouble),
|
||||||
|
d: NotSerializeEnum::Trouble,
|
||||||
|
},
|
||||||
|
&[
|
||||||
|
Token::StructStart("ContainsNotSerialize", Some(2)),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::Option(true),
|
||||||
|
Token::I8(1),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("d"),
|
||||||
|
Token::Str("trouble"),
|
||||||
|
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
|
struct SerializeWithStruct<'a, B> where B: SerializeWith {
|
||||||
a: &'a i8,
|
a: &'a i8,
|
||||||
#[serde(serialize_with="Trait::serialize_with")]
|
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||||
b: B,
|
b: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -544,10 +740,10 @@ fn test_serialize_with_struct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize)]
|
||||||
enum SerializeWithEnum<'a, B> where B: Trait {
|
enum SerializeWithEnum<'a, B> where B: SerializeWith {
|
||||||
Struct {
|
Struct {
|
||||||
a: &'a i8,
|
a: &'a i8,
|
||||||
#[serde(serialize_with="Trait::serialize_with")]
|
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||||
b: B,
|
b: B,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -597,9 +793,9 @@ fn test_serialize_with_enum() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
struct DeserializeWithStruct<B> where B: Trait {
|
struct DeserializeWithStruct<B> where B: DeserializeWith {
|
||||||
a: i8,
|
a: i8,
|
||||||
#[serde(deserialize_with="Trait::deserialize_with")]
|
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||||
b: B,
|
b: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -647,10 +843,10 @@ fn test_deserialize_with_struct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
enum DeserializeWithEnum<B> where B: Trait {
|
enum DeserializeWithEnum<B> where B: DeserializeWith {
|
||||||
Struct {
|
Struct {
|
||||||
a: i8,
|
a: i8,
|
||||||
#[serde(deserialize_with="Trait::deserialize_with")]
|
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||||
b: B,
|
b: B,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -697,3 +893,57 @@ fn test_deserialize_with_enum() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_missing_renamed_field_struct() {
|
||||||
|
assert_de_tokens_error::<RenameStruct>(
|
||||||
|
vec![
|
||||||
|
Token::StructStart("Superhero", Some(2)),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a1"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
Error::MissingFieldError("a3"),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_de_tokens_error::<RenameStructSerializeDeserialize>(
|
||||||
|
vec![
|
||||||
|
Token::StructStart("SuperheroDe", Some(2)),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a1"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
Error::MissingFieldError("a5"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_missing_renamed_field_enum() {
|
||||||
|
assert_de_tokens_error::<RenameEnum>(
|
||||||
|
vec![
|
||||||
|
Token::EnumMapStart("Superhero", "barry_allan", Some(1)),
|
||||||
|
|
||||||
|
Token::EnumMapEnd,
|
||||||
|
],
|
||||||
|
Error::MissingFieldError("b"),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_de_tokens_error::<RenameEnumSerializeDeserialize<i8>>(
|
||||||
|
vec![
|
||||||
|
Token::EnumMapStart("SuperheroDe", "jason_todd", Some(2)),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I8(0),
|
||||||
|
|
||||||
|
Token::EnumMapEnd,
|
||||||
|
],
|
||||||
|
Error::MissingFieldError("d"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use serde;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error;
|
use std::error;
|
||||||
use serde::Serialize;
|
|
||||||
use serde::bytes::{ByteBuf, Bytes};
|
extern crate serde;
|
||||||
|
use self::serde::Serialize;
|
||||||
|
use self::serde::bytes::{ByteBuf, Bytes};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
|||||||
use std::net;
|
use std::net;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use serde::de::{Deserializer, Visitor};
|
|
||||||
|
|
||||||
use token::{
|
use token::{
|
||||||
Error,
|
Error,
|
||||||
Token,
|
Token,
|
||||||
@@ -24,6 +22,7 @@ struct TupleStruct(i32, i32, i32);
|
|||||||
struct Struct {
|
struct Struct {
|
||||||
a: i32,
|
a: i32,
|
||||||
b: i32,
|
b: i32,
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
c: i32,
|
c: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +59,17 @@ macro_rules! declare_tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! declare_error_tests {
|
||||||
|
($($name:ident<$target:ident> { $tokens:expr, $expected:expr, })+) => {
|
||||||
|
$(
|
||||||
|
#[test]
|
||||||
|
fn $name() {
|
||||||
|
assert_de_tokens_error::<$target>($tokens, $expected);
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
declare_tests! {
|
declare_tests! {
|
||||||
@@ -524,7 +534,40 @@ declare_tests! {
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
test_struct {
|
test_struct {
|
||||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||||
|
Token::MapStart(Some(3)),
|
||||||
|
Token::MapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::MapSep,
|
||||||
|
Token::Str("b"),
|
||||||
|
Token::I32(2),
|
||||||
|
Token::MapEnd,
|
||||||
|
],
|
||||||
|
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||||
|
Token::StructStart("Struct", Some(3)),
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("b"),
|
||||||
|
Token::I32(2),
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||||
|
Token::SeqStart(Some(3)),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(2),
|
||||||
|
Token::SeqEnd,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
test_struct_with_skip {
|
||||||
|
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||||
Token::MapStart(Some(3)),
|
Token::MapStart(Some(3)),
|
||||||
Token::MapSep,
|
Token::MapSep,
|
||||||
Token::Str("a"),
|
Token::Str("a"),
|
||||||
@@ -537,9 +580,13 @@ declare_tests! {
|
|||||||
Token::MapSep,
|
Token::MapSep,
|
||||||
Token::Str("c"),
|
Token::Str("c"),
|
||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
|
Token::MapSep,
|
||||||
|
Token::Str("d"),
|
||||||
|
Token::I32(4),
|
||||||
Token::MapEnd,
|
Token::MapEnd,
|
||||||
],
|
],
|
||||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||||
Token::StructStart("Struct", Some(3)),
|
Token::StructStart("Struct", Some(3)),
|
||||||
Token::StructSep,
|
Token::StructSep,
|
||||||
Token::Str("a"),
|
Token::Str("a"),
|
||||||
@@ -552,6 +599,10 @@ declare_tests! {
|
|||||||
Token::StructSep,
|
Token::StructSep,
|
||||||
Token::Str("c"),
|
Token::Str("c"),
|
||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("d"),
|
||||||
|
Token::I32(4),
|
||||||
Token::StructEnd,
|
Token::StructEnd,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
@@ -611,6 +662,21 @@ declare_tests! {
|
|||||||
Token::Unit,
|
Token::Unit,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_box {
|
||||||
|
Box::new(0i32) => vec![Token::I32(0)],
|
||||||
|
}
|
||||||
|
test_boxed_slice {
|
||||||
|
Box::new([0, 1, 2]) => vec![
|
||||||
|
Token::SeqStart(Some(3)),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(0),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(1),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(2),
|
||||||
|
Token::SeqEnd,
|
||||||
|
],
|
||||||
|
}
|
||||||
test_net_ipv4addr {
|
test_net_ipv4addr {
|
||||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => vec![Token::Str("1.2.3.4")],
|
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => vec![Token::Str("1.2.3.4")],
|
||||||
}
|
}
|
||||||
@@ -638,12 +704,50 @@ fn test_net_ipaddr() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
declare_error_tests! {
|
||||||
fn test_enum_error() {
|
test_unknown_variant<Enum> {
|
||||||
assert_de_tokens_error::<Enum>(
|
|
||||||
vec![
|
vec![
|
||||||
Token::EnumUnit("Enum", "Foo"),
|
Token::EnumUnit("Enum", "Foo"),
|
||||||
],
|
],
|
||||||
Error::UnknownVariantError("Foo".to_owned()),
|
Error::UnknownVariantError("Foo".to_owned()),
|
||||||
)
|
}
|
||||||
|
test_struct_seq_too_long<Struct> {
|
||||||
|
vec![
|
||||||
|
Token::SeqStart(Some(4)),
|
||||||
|
Token::SeqSep, Token::I32(1),
|
||||||
|
Token::SeqSep, Token::I32(2),
|
||||||
|
Token::SeqSep, Token::I32(3),
|
||||||
|
Token::SeqSep, Token::I32(4),
|
||||||
|
Token::SeqEnd,
|
||||||
|
],
|
||||||
|
Error::UnexpectedToken(Token::SeqSep),
|
||||||
|
}
|
||||||
|
test_duplicate_field_struct<Struct> {
|
||||||
|
vec![
|
||||||
|
Token::MapStart(Some(3)),
|
||||||
|
Token::MapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::MapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(3),
|
||||||
|
Token::MapEnd,
|
||||||
|
],
|
||||||
|
Error::DuplicateFieldError("a"),
|
||||||
|
}
|
||||||
|
test_duplicate_field_enum<Enum> {
|
||||||
|
vec![
|
||||||
|
Token::EnumMapStart("Enum", "Map", Some(3)),
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(1),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(3),
|
||||||
|
Token::EnumMapEnd,
|
||||||
|
],
|
||||||
|
Error::DuplicateFieldError("a"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
// These just test that serde_codegen is able to produce code that compiles
|
||||||
|
// successfully when there are a variety of generics and non-(de)serializable
|
||||||
|
// types involved.
|
||||||
|
|
||||||
|
extern crate serde;
|
||||||
|
use self::serde::ser::{Serialize, Serializer};
|
||||||
|
use self::serde::de::{Deserialize, Deserializer};
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct With<T> {
|
||||||
|
t: T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
x: X,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct WithRef<'a, T: 'a> {
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
t: Option<&'a T>,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
x: X,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Bounds<T: Serialize + Deserialize> {
|
||||||
|
t: T,
|
||||||
|
option: Option<T>,
|
||||||
|
boxed: Box<T>,
|
||||||
|
option_boxed: Option<Box<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct NoBounds<T> {
|
||||||
|
t: T,
|
||||||
|
option: Option<T>,
|
||||||
|
boxed: Box<T>,
|
||||||
|
option_boxed: Option<Box<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
enum EnumWith<T> {
|
||||||
|
Unit,
|
||||||
|
Newtype(
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X),
|
||||||
|
Tuple(
|
||||||
|
T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X),
|
||||||
|
Struct {
|
||||||
|
t: T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
x: X },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
||||||
|
t: T,
|
||||||
|
rrrt: &'a &'b &'c T,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Newtype(
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X
|
||||||
|
);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Tuple<T>(
|
||||||
|
T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X,
|
||||||
|
);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Implements neither Serialize nor Deserialize
|
||||||
|
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!() }
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens};
|
use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -6,6 +7,13 @@ trait Trait {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// That tests that the derived Serialize implementation doesn't trigger
|
||||||
|
// any warning about `serializer` not being used, in case of empty enums.
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[deny(unused_variables)]
|
||||||
|
enum Void {}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
struct NamedUnit;
|
struct NamedUnit;
|
||||||
|
|
||||||
@@ -136,6 +144,19 @@ pub enum GenericEnum<T, U> {
|
|||||||
Map { x: T, y: U },
|
Map { x: T, y: U },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait AssociatedType {
|
||||||
|
type X;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AssociatedType for i32 {
|
||||||
|
type X = i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
struct DefaultTyParam<T: AssociatedType<X=i32> = i32> {
|
||||||
|
phantom: PhantomData<T>
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_named_unit() {
|
fn test_named_unit() {
|
||||||
assert_tokens(
|
assert_tokens(
|
||||||
@@ -594,3 +615,19 @@ fn test_generic_enum_map() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_default_ty_param() {
|
||||||
|
assert_tokens(
|
||||||
|
&DefaultTyParam::<i32> { phantom: PhantomData },
|
||||||
|
vec![
|
||||||
|
Token::StructStart("DefaultTyParam", Some(1)),
|
||||||
|
|
||||||
|
Token::StructSep,
|
||||||
|
Token::Str("phantom"),
|
||||||
|
Token::UnitStruct("PhantomData"),
|
||||||
|
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -262,6 +262,21 @@ declare_ser_tests! {
|
|||||||
Token::EnumMapEnd,
|
Token::EnumMapEnd,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_box {
|
||||||
|
Box::new(0i32) => &[Token::I32(0)],
|
||||||
|
}
|
||||||
|
test_boxed_slice {
|
||||||
|
Box::new([0, 1, 2]) => &[
|
||||||
|
Token::SeqArrayStart(3),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(0),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(1),
|
||||||
|
Token::SeqSep,
|
||||||
|
Token::I32(2),
|
||||||
|
Token::SeqEnd,
|
||||||
|
],
|
||||||
|
}
|
||||||
test_net_ipv4addr {
|
test_net_ipv4addr {
|
||||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ use std::fmt;
|
|||||||
use std::iter;
|
use std::iter;
|
||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
use serde::ser::{self, Serialize};
|
extern crate serde;
|
||||||
use serde::de;
|
use self::serde::ser::{self, Serialize};
|
||||||
use serde::de::value::{self, ValueDeserializer};
|
use self::serde::de;
|
||||||
|
use self::serde::de::value::{self, ValueDeserializer};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub enum Token<'a> {
|
pub enum Token<'a> {
|
||||||
@@ -409,6 +410,7 @@ pub enum Error {
|
|||||||
UnknownFieldError(String),
|
UnknownFieldError(String),
|
||||||
UnknownVariantError(String),
|
UnknownVariantError(String),
|
||||||
MissingFieldError(&'static str),
|
MissingFieldError(&'static str),
|
||||||
|
DuplicateFieldError(&'static str),
|
||||||
InvalidName(&'static str),
|
InvalidName(&'static str),
|
||||||
InvalidValue(String),
|
InvalidValue(String),
|
||||||
UnexpectedToken(Token<'static>),
|
UnexpectedToken(Token<'static>),
|
||||||
@@ -428,6 +430,10 @@ impl de::Error for Error {
|
|||||||
|
|
||||||
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
||||||
|
|
||||||
|
fn invalid_value(msg: &str) -> Error {
|
||||||
|
Error::InvalidValue(msg.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
fn unknown_field(field: &str) -> Error {
|
fn unknown_field(field: &str) -> Error {
|
||||||
Error::UnknownFieldError(field.to_owned())
|
Error::UnknownFieldError(field.to_owned())
|
||||||
}
|
}
|
||||||
@@ -439,6 +445,10 @@ impl de::Error for Error {
|
|||||||
fn missing_field(field: &'static str) -> Error {
|
fn missing_field(field: &'static str) -> Error {
|
||||||
Error::MissingFieldError(field)
|
Error::MissingFieldError(field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn duplicate_field(field: &'static str) -> Error {
|
||||||
|
Error::DuplicateFieldError(field)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
|
|||||||
Reference in New Issue
Block a user