Compare commits

...

107 Commits

Author SHA1 Message Date
David Tolnay d4d2061a2e Release 0.9.14 2017-04-17 07:33:45 -07:00
David Tolnay c202ce13dc Merge pull request #882 from jethrogb/feature/interior_mut_types
Add Serialize/Deserialize for std types that provide interior mutability
2017-04-17 07:32:13 -07:00
Jethro Beekman 1c9478bfa6 Add Serialize/Deserialize for std types that provide interior mutability
Fixes #179
2017-04-16 22:59:17 -07:00
David Tolnay 1232cfd194 Add playground link to readme 2017-04-13 10:19:22 -07:00
David Tolnay 2c49f9aad3 Recommend 'extern crate serde' 2017-04-08 13:53:13 -07:00
David Tolnay 1b763da529 Release 0.9.13 2017-04-05 15:03:37 -07:00
David Tolnay b7d6c5d9f7 Remove no_std special case in forward_to_deserialize 2017-04-05 15:01:55 -07:00
David Tolnay bfabaf3789 Merge pull request #838 from serde-rs/winstr
Deserialize OsString on Windows
2017-04-04 11:33:42 -07:00
David Tolnay cf6c4ab7ec Deserialize OsString on Windows 2017-04-04 11:19:24 -07:00
David Tolnay 8eb50186e0 Merge pull request #831 from serde-rs/derivepath
Use the live serde_derive for serde doc tests
2017-03-30 22:55:33 -07:00
David Tolnay 7d985ff3fd Use the live serde_derive for serde doc tests
This is required as both are updated to Deserialize<'de> together.
2017-03-30 22:45:15 -07:00
David Tolnay 59b3961ad4 Release 0.9.12 2017-03-27 15:45:14 -07:00
David Tolnay dbc9a60c5f Merge pull request #824 from alexcrichton/osstr-osstring
Implement Serialize/Deserialize for OsStr/OsString
2017-03-27 15:39:07 -07:00
Alex Crichton ce687431f3 Implement Serialize/Deserialize for OsStr/OsString
This commit implements the two serde traits for the libstd `OsStr` and
`OsString` types. This came up as a use case during implementing sccache where
we're basically just doing IPC to communicate paths around. Additionally the
`Path` and `PathBuf` implementations have been updated to delegate to the os
string ones.

These types are platform-specific, however, so the serialization/deserialization
isn't trivial. Currently this "fakes" a newtype variant for Unix/Windows to
prevent cross-platform serialization/deserialization. This means if you're doing
IPC within the same OS (e.g. Windows to Windows) then serialization should be
infallible. If you're doing IPC across platforms (e.g.  Unix to Windows) then
using `OsString` is guaranteed to fail as bytes from one OS won't deserialize on
the other (even if they're unicode).
2017-03-27 11:44:50 -07:00
David Tolnay 71ccc5753b Merge pull request #820 from serde-rs/reexport
Re-export derives from serde
2017-03-26 20:37:12 -07:00
David Tolnay 13886435a6 Re-export derives from serde 2017-03-21 16:54:31 -07:00
David Tolnay 08e2182042 UFCS for Option::map and Result::map 2017-03-18 13:31:03 -07:00
David Tolnay d60595cc27 Use format_args! to simplify de::Error 2017-03-18 10:48:47 -07:00
David Tolnay 8c7396c35a Be consistent with UFCS and double underscores 2017-03-18 10:33:22 -07:00
David Tolnay 42b2fd8eec Improve error message parsing from and into 2017-03-18 09:22:44 -07:00
John Baublitz bc946e4fd7 Macro attributes to specify From and Into trait types for structs and enums (#817)
* serde macro support for type conversions through From and Into trait

* Revisions requested by dtolnay

* Additional changes requested by dtolnay
2017-03-18 09:22:27 -07:00
David Tolnay c488cec641 Remove RangeInclusive impl for now
The old impl is inconsistent with the new ops::Range impl.
2017-03-13 11:09:56 -07:00
David Tolnay fcc3c69a49 Range impl is defined when building with std 2017-03-09 12:04:02 -08:00
David Tolnay 350c2e835b Remove duplicate ops::Range serialize impl 2017-03-09 11:57:36 -08:00
David Tolnay a0a031677e Merge pull request #813 from rocallahan/support-range
Implement `Serialize`/`Deserialize` for `Range`
2017-03-09 11:55:21 -08:00
Robert O'Callahan cc0d045f5c Add Deserialize impl for std::ops::Range
Resolves #796
2017-03-10 08:39:34 +13:00
Robert O'Callahan aebe1fddab Add Serialize impl for std::ops::Range 2017-03-10 00:35:04 +13:00
David Tolnay 77ee306b57 Format serde with rustfmt 0.8.0 2017-03-08 21:11:17 -08:00
David Tolnay 6750fdaae1 Merge pull request #811 from jonhoo/deserialize-cstr
Add Deserialize impl for CStr
2017-03-08 21:06:09 -08:00
David Tolnay dd6989d551 Actually run unstable tests 2017-03-08 21:02:15 -08:00
Jon Gjengset 0c2fed49e2 Add Deserialize impl for CStr
Fixes #810
2017-03-08 23:57:26 -05:00
Oliver Schneider a4ee9bd045 Merge pull request #808 from serde-rs/u32
Implement deserialize_enum for U32Deserializer
2017-03-07 18:58:29 +01:00
David Tolnay 6d9397d573 Implement deserialize_enum for U32Deserializer 2017-03-07 09:05:33 -08:00
David Tolnay dc9445f873 Release 0.9.11 2017-03-05 16:51:44 -08:00
David Tolnay 17bc40ec1c Merge pull request #789 from nox/display
Introduce Serializer::collect_str (fixes #786)
2017-03-05 16:47:22 -08:00
David Tolnay 2184fef82f Add format_args example to collect_str 2017-03-05 16:43:31 -08:00
David Tolnay 7e1b5c6ce4 Neater collect_str variable name for rustdoc 2017-03-05 16:26:05 -08:00
David Tolnay 36da8a5cee Error message geared toward serializer users
The previous message was targeted toward Serializer implementors, which is not
the group that will be seeing this message most often.
2017-03-05 16:24:48 -08:00
David Tolnay fbe85f399d Force no_std formats to implement collect_str in the future 2017-03-05 16:22:02 -08:00
David Tolnay 880b27b19e Identical signature for std and no_std collect_str 2017-03-05 16:17:03 -08:00
David Tolnay cc06f070d1 Use the existing CString error message 2017-03-05 13:59:18 -08:00
David Tolnay 2f988aa5e6 Merge pull request #801 from jonhoo/ffi-strings
impls for null-terminated FFI string types
2017-03-05 13:51:09 -08:00
Jon Gjengset d294a10e83 Only include ByteBuf when ser/de is on for std 2017-03-03 23:48:00 -05:00
Jon Gjengset defcbef7ab Use a non-stupid path for bytes::ByteBuf 2017-03-03 23:28:35 -05:00
Jon Gjengset d90eecd4a2 Add tests for CStr(ing) ser/de 2017-03-03 18:06:04 -05:00
Jon Gjengset 0d6d077e6a Serialize and deserialize CString through [u8] 2017-03-03 18:05:08 -05:00
Jon Gjengset be09fc9bbb Remove unsafe Deserialize impl for CStr
See also https://github.com/rust-lang/rust/issues/40248
2017-03-03 17:34:01 -05:00
Jon Gjengset fc9d78e26b Use serialize_bytes for speed 2017-03-03 17:14:39 -05:00
Jon Gjengset 9f83164c40 Don't serialize trailing NULL 2017-03-03 17:14:28 -05:00
Jon Gjengset 857974ab8a impls for null-terminated FFI string types
Fixes #800.
2017-03-03 16:09:41 -05:00
David Tolnay d70636f4d4 Merge pull request #794 from clarcharr/master
Documentation for serde_test.
2017-03-02 11:53:05 -08:00
Clar Charr 09e467cc4c Documentation for serde_test. 2017-02-28 18:20:27 -05:00
David Tolnay 51ed9c2a40 Release 0.9.10 2017-02-28 12:45:39 -08:00
Anthony Ramine a9a05350a9 Introduce Serializer::collect_str (fixes #786)
The default implementation collects the Display value into a String
and then passes that to Serializer::serialize_str when the std or collections
features are enabled, otherwise it unconditionally returns an error.
2017-02-28 12:11:47 +01:00
David Tolnay fe9ea3b4b4 Fix needless_pass_by_value lint 2017-02-27 19:43:42 -08:00
David Tolnay f944b453c4 Merge pull request #792 from elliottslaughter/fix_serialize_tuple_docs
Fix documentation of serialize_tuple and SerializeTuple
2017-02-27 14:05:15 -08:00
Elliott Slaughter a993630cf9 Fix documentation of serialize_tuple and SerializeTuple. 2017-02-27 14:00:21 -08:00
David Tolnay abc081ce9c Test rename_all attribute 2017-02-25 11:58:34 -08:00
David Tolnay 207940046b Merge pull request #788 from 46bit/issue-140
`rename_all` container attribute to have all children fit a naming convention
2017-02-25 11:38:33 -08:00
David Tolnay 47efbc6d75 Don't need to pass back the input here 2017-02-25 11:32:27 -08:00
David Tolnay 17279e8a4f Simplify case conversion implementation 2017-02-25 11:32:22 -08:00
David Tolnay 06c631db05 Bring in the tests 2017-02-25 11:02:51 -08:00
David Tolnay 7952bad41f Move case conversion to its own file 2017-02-25 11:02:25 -08:00
Michael Mokrysz 3308f81c3a Saving progress on naming convention conversion code to try new Inflector changes. #788 2017-02-25 13:24:49 +00:00
David Tolnay 75e6da02d3 Split up test suite so it compiles in parallel 2017-02-24 15:39:18 -08:00
David Tolnay 59ec931d8f Release 0.9.9 2017-02-24 13:57:52 -08:00
David Tolnay 4b3c5ea99e Fix clippy lint in content serializer 2017-02-24 13:56:44 -08:00
David Tolnay d4ea4a7eef Add test for enums containing enums
Fixes #775.
2017-02-24 13:50:47 -08:00
David Tolnay 5fbdadefb2 Deserializing enums in serde_test 2017-02-24 13:47:04 -08:00
David Tolnay 4da77e4200 Serialize to Content 2017-02-24 12:59:35 -08:00
David Tolnay d797504407 Deserialize enums from &Content 2017-02-24 09:32:14 -08:00
Michael Mokrysz 84915268ee Minor fixes for pull request. Cheers @dtolnay. 2017-02-24 01:55:31 +00:00
Michael Mokrysz 3b59d47e07 Heavily refactored rename_all and switched to SCREAMING_SNAKE_CASE. 2017-02-23 23:22:54 +00:00
Michael Mokrysz fc94c5399a Implementing rename_all container attribute using Inflector trait. #140 2017-02-23 18:58:41 +00:00
David Tolnay 8e5f472e27 Merge pull request #787 from 46bit/issue-775
Fix internally tagged enums by implementing ContentDeserializer::deserialize_enum
2017-02-22 18:23:18 -08:00
David Tolnay 5e7883945b Fix clippy lint in Content::unexpected 2017-02-22 18:16:51 -08:00
David Tolnay a4bd6b9a96 Fix content's SeqDeserializer on no_std 2017-02-22 18:14:56 -08:00
Michael Mokrysz 4c253748c2 Compiles and fixes internally tagged testcases. 2017-02-23 01:05:44 +00:00
Michael Mokrysz 8f6e1fb5b3 Resolved error typing. Needs to adapt to Content::Map with non-String keys.
`EnumDeserializer` is being adapted from:
  `serde_json::Value::Map<Vec<(String, Value)>>`
serde has a Map variant that allows non-String keys:
  `serde::de::Content::Map<Vec<(Content, Content)>>`

There's a lot of assumptions in `EnumDeserializer` about `String` keys and I'm not sure what the adaptation should be.
2017-02-22 23:59:47 +00:00
Michael Mokrysz 4e665a71bd Attempt to port serde_json::Value::deserialize_enum for ContentDeserializer is being derailed by what error types should be used. 2017-02-22 22:55:18 +00:00
David Tolnay 017e6d304f Release 0.9.8 2017-02-21 10:59:42 -08:00
David Tolnay 89cbb81673 Include readme and licenses in crates.io archive
Fixes #772.
2017-02-20 21:11:57 -08:00
David Tolnay c36743fa05 Merge pull request #782 from serde-rs/adjacent
Deserialization of Haskell style enums
2017-02-20 21:04:17 -08:00
David Tolnay 9a3c1243f4 Deserialization of Haskell style enums 2017-02-20 20:05:06 -08:00
David Tolnay 599a1b6607 Fragments that keep track of type of syntax
Fixes #777.
2017-02-20 17:12:35 -08:00
David Tolnay 3d6e086d3e Support custom paths in container attribute serde(default="...") 2017-02-20 17:06:00 -08:00
David Tolnay ad480d2b04 Merge pull request #780 from Thomasdezeeuw/default_container
Add support for `#[serde(default)]` on structs
2017-02-20 16:10:28 -08:00
Thomas de Zeeuw 7bee779514 Only allow #[serde(default)]` on structs 2017-02-21 00:15:00 +01:00
David Tolnay dff919a926 Rename __simple_value to __field0 to be consistent with tuples 2017-02-20 14:16:54 -08:00
David Tolnay bac593573c Drop aster dependency 2017-02-20 13:50:17 -08:00
Thomas de Zeeuw 9444db5f19 Add support for #[serde(default)] on structs
This allows structs to use the default value for each field defined in
the struct’s `std::default::Default` implementation, rather then the
default value for the field’s type.

```
struct StructDefault {
    a: i32,
    b: String,
}

impl Default for StructDefault {
    fn default() -> StructDefault {
        StructDefault{
            a: 100,
            b: "default".to_string(),
        }
    }
}
```

The code above will now return `100` for field `a` and `”default”` for
`b`, rather then `0` and `””` respectively.
2017-02-20 22:35:31 +01:00
David Tolnay 3bf8cda994 Update generics handling to use split_for_impl 2017-02-20 13:04:40 -08:00
David Tolnay f98e7f6ba0 Merge pull request #778 from SimonSapin/serialize_display_bounded_length
Avoid some string allocations.
2017-02-20 11:09:32 -08:00
Simon Sapin 772b22b427 Avoid some string allocations. 2017-02-20 18:36:38 +01:00
David Tolnay 535ab1e04b Merge pull request #771 from elliottslaughter:internally_content_tagged 2017-02-19 16:08:49 -08:00
David Tolnay a3fe03c323 Serialization of Haskell style enums 2017-02-19 16:04:39 -08:00
David Tolnay 7dad6426da Merge pull request #774 from nox/limit-hints
Clamp hints coming from untrusted input to 4096
2017-02-19 13:51:21 -08:00
David Tolnay 792a5f7502 Also clamp the collection impls 2017-02-19 13:47:05 -08:00
Anthony Ramine a4c738a9f3 Clamp hints coming from untrusted input to 4096 2017-02-19 14:29:41 +01:00
Elliott Slaughter 4538143d00 Initial work to support serialization of adjacently tagged enums. 2017-02-18 20:46:37 -08:00
David Tolnay 7f08894a32 Remove dependency on unneeded syn feature 2017-02-17 21:01:42 -08:00
David Tolnay adf7b086b5 Remove outdated comment 2017-02-17 20:38:50 -08:00
David Tolnay c590df13b9 Add compile-fail test for the with attribute 2017-02-16 18:43:04 -08:00
Oliver Schneider afa6dfbbe2 Merge pull request #765 from serde-rs/with
Pair serialize_with and deserialize_with into one attribute
2017-02-15 08:33:14 +01:00
David Tolnay f500db6e91 Pair serialize_with and deserialize_with into one attribute 2017-02-14 17:36:50 -08:00
David Tolnay 090c8a7049 Merge pull request #762 from serde-rs/rustfmt
Use rustfmt
2017-02-13 01:29:25 -08:00
David Tolnay e8651a52e7 Rustfmt 2017-02-12 22:15:59 -08:00
68 changed files with 5485 additions and 2165 deletions
+5
View File
@@ -15,10 +15,15 @@ You may be looking for:
## Serde in action
<a href="http://play.integer32.com/?gist=9003c5b88c1f4989941925d7190c6eec" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>
```rust
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
+17
View File
@@ -0,0 +1,17 @@
environment:
matrix:
- APPVEYOR_RUST_CHANNEL: stable
- APPVEYOR_RUST_CHANNEL: nightly
install:
# Install rust, x86_64-pc-windows-msvc host
- appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
- rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain %APPVEYOR_RUST_CHANNEL%
- set PATH=C:\msys64\usr\bin;%PATH%;C:\Users\appveyor\.cargo\bin
- rustc -vV
- cargo -vV
build: false
test_script:
- sh -c 'PATH=`rustc --print sysroot`/bin:$PATH ./travis.sh'
+6 -5
View File
@@ -1,22 +1,23 @@
[package]
name = "serde"
version = "0.9.7"
version = "0.9.14"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://docs.serde.rs/serde/"
readme = "../README.md"
keywords = ["serde", "serialization", "no_std"]
categories = ["encoding"]
include = ["Cargo.toml", "src/**/*.rs"]
readme = "../README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
[badges]
travis-ci = { repository = "serde-rs/serde" }
[features]
default = ["std"]
derive = ["serde_derive"]
std = []
unstable = []
@@ -28,7 +29,7 @@ unstable-testing = ["unstable", "std"]
playground = ["serde_derive"]
[dependencies]
serde_derive = { version = "0.9", optional = true }
serde_derive = { version = "0.9", optional = true, path = "../serde_derive" }
[dev-dependencies]
serde_derive = "0.9"
serde_derive = { version = "0.9", path = "../serde_derive" }
+1
View File
@@ -0,0 +1 @@
../LICENSE-APACHE
+1
View File
@@ -0,0 +1 @@
../LICENSE-MIT
+1
View File
@@ -0,0 +1 @@
../README.md
+22 -17
View File
@@ -60,9 +60,7 @@ pub struct Bytes<'a> {
impl<'a> Bytes<'a> {
/// Wrap an existing `&[u8]`.
pub fn new(bytes: &'a [u8]) -> Self {
Bytes {
bytes: bytes,
}
Bytes { bytes: bytes }
}
}
@@ -98,7 +96,9 @@ impl<'a> Into<&'a [u8]> for Bytes<'a> {
impl<'a> ops::Deref for Bytes<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] { self.bytes }
fn deref(&self) -> &[u8] {
self.bytes
}
}
impl<'a> ser::Serialize for Bytes<'a> {
@@ -114,6 +114,7 @@ impl<'a> ser::Serialize for Bytes<'a> {
#[cfg(any(feature = "std", feature = "collections"))]
mod bytebuf {
use core::cmp;
use core::ops;
use core::fmt;
use core::fmt::Write;
@@ -161,9 +162,7 @@ mod bytebuf {
/// Wrap existing bytes in a `ByteBuf`.
pub fn from<T: Into<Vec<u8>>>(bytes: T) -> Self {
ByteBuf {
bytes: bytes.into(),
}
ByteBuf { bytes: bytes.into() }
}
}
@@ -216,11 +215,15 @@ mod bytebuf {
impl ops::Deref for ByteBuf {
type Target = [u8];
fn deref(&self) -> &[u8] { &self.bytes[..] }
fn deref(&self) -> &[u8] {
&self.bytes[..]
}
}
impl ops::DerefMut for ByteBuf {
fn deref_mut(&mut self) -> &mut [u8] { &mut self.bytes[..] }
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.bytes[..]
}
}
impl ser::Serialize for ByteBuf {
@@ -243,16 +246,16 @@ mod bytebuf {
#[inline]
fn visit_unit<E>(self) -> Result<ByteBuf, E>
where E: de::Error,
where E: de::Error
{
Ok(ByteBuf::new())
}
#[inline]
fn visit_seq<V>(self, mut visitor: V) -> Result<ByteBuf, V::Error>
where V: de::SeqVisitor,
where V: de::SeqVisitor
{
let (len, _) = visitor.size_hint();
let len = cmp::min(visitor.size_hint().0, 4096);
let mut values = Vec::with_capacity(len);
while let Some(value) = try!(visitor.visit()) {
@@ -264,26 +267,26 @@ mod bytebuf {
#[inline]
fn visit_bytes<E>(self, v: &[u8]) -> Result<ByteBuf, E>
where E: de::Error,
where E: de::Error
{
Ok(ByteBuf::from(v))
}
#[inline]
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteBuf, E>
where E: de::Error,
where E: de::Error
{
Ok(ByteBuf::from(v))
}
fn visit_str<E>(self, v: &str) -> Result<ByteBuf, E>
where E: de::Error,
where E: de::Error
{
Ok(ByteBuf::from(v))
}
fn visit_string<E>(self, v: String) -> Result<ByteBuf, E>
where E: de::Error,
where E: de::Error
{
Ok(ByteBuf::from(v))
}
@@ -302,7 +305,9 @@ mod bytebuf {
///////////////////////////////////////////////////////////////////////////////
#[inline]
fn escape_bytestring<'a>(bytes: &'a [u8]) -> iter::FlatMap<slice::Iter<'a, u8>, char::EscapeDefault, fn(&u8) -> char::EscapeDefault> {
fn escape_bytestring<'a>
(bytes: &'a [u8])
-> iter::FlatMap<slice::Iter<'a, u8>, char::EscapeDefault, fn(&u8) -> char::EscapeDefault> {
fn f(b: &u8) -> char::EscapeDefault {
char::from_u32(*b as u32).unwrap().escape_default()
}
+613 -36
View File
@@ -10,6 +10,7 @@
#![doc(hidden)]
use core::cmp;
use core::fmt;
use core::marker::PhantomData;
@@ -19,16 +20,8 @@ use collections::{String, Vec};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
use de::{
self,
Deserialize,
DeserializeSeed,
Deserializer,
Visitor,
SeqVisitor,
MapVisitor,
EnumVisitor,
};
use de::{self, Deserialize, DeserializeSeed, Deserializer, Visitor, SeqVisitor, MapVisitor,
EnumVisitor, Unexpected};
/// Used from generated code to buffer the contents of the Deserializer when
/// deserializing untagged enums and internally tagged enums.
@@ -36,7 +29,6 @@ use de::{
/// Not public API. Use serde-value instead.
#[derive(Debug)]
pub enum Content {
// Don't mind the PhantomData, just need to use E somewhere.
Bool(bool),
U8(u8),
@@ -65,6 +57,32 @@ pub enum Content {
Map(Vec<(Content, Content)>),
}
impl Content {
fn unexpected(&self) -> Unexpected {
match *self {
Content::Bool(b) => Unexpected::Bool(b),
Content::U8(n) => Unexpected::Unsigned(n as u64),
Content::U16(n) => Unexpected::Unsigned(n as u64),
Content::U32(n) => Unexpected::Unsigned(n as u64),
Content::U64(n) => Unexpected::Unsigned(n),
Content::I8(n) => Unexpected::Signed(n as i64),
Content::I16(n) => Unexpected::Signed(n as i64),
Content::I32(n) => Unexpected::Signed(n as i64),
Content::I64(n) => Unexpected::Signed(n),
Content::F32(f) => Unexpected::Float(f as f64),
Content::F64(f) => Unexpected::Float(f),
Content::Char(c) => Unexpected::Char(c),
Content::String(ref s) => Unexpected::Str(s),
Content::Bytes(ref b) => Unexpected::Bytes(b),
Content::None | Content::Some(_) => Unexpected::Option,
Content::Unit => Unexpected::Unit,
Content::Newtype(_) => Unexpected::NewtypeStruct,
Content::Seq(_) => Unexpected::Seq,
Content::Map(_) => Unexpected::Map,
}
}
}
impl Deserialize for Content {
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error> {
// Untagged and internally tagged enums are only supported in
@@ -205,7 +223,7 @@ impl Visitor for ContentVisitor {
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor
{
let mut vec = Vec::with_capacity(visitor.size_hint().0);
let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096));
while let Some(e) = try!(visitor.visit()) {
vec.push(e);
}
@@ -215,7 +233,7 @@ impl Visitor for ContentVisitor {
fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor
{
let mut vec = Vec::with_capacity(visitor.size_hint().0);
let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096));
while let Some(kv) = try!(visitor.visit()) {
vec.push(kv);
}
@@ -243,9 +261,7 @@ struct TagOrContentVisitor {
impl TagOrContentVisitor {
fn new(name: &'static str) -> Self {
TagOrContentVisitor {
name: name,
}
TagOrContentVisitor { name: name }
}
}
@@ -475,7 +491,7 @@ impl<T> Visitor for TaggedContentVisitor<T>
where V: MapVisitor
{
let mut tag = None;
let mut vec = Vec::with_capacity(visitor.size_hint().0);
let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096));
while let Some(k) = try!(visitor.visit_key_seed(TagOrContentVisitor::new(self.tag_name))) {
match k {
TagOrContent::Tag => {
@@ -491,19 +507,61 @@ impl<T> Visitor for TaggedContentVisitor<T>
}
}
match tag {
None => {
Err(de::Error::missing_field(self.tag_name))
}
None => Err(de::Error::missing_field(self.tag_name)),
Some(tag) => {
Ok(TaggedContent {
tag: tag,
content: Content::Map(vec),
})
tag: tag,
content: Content::Map(vec),
})
}
}
}
}
/// Used by generated code to deserialize an adjacently tagged enum.
///
/// Not public API.
pub enum TagOrContentField {
Tag,
Content,
}
/// Not public API.
pub struct TagOrContentFieldVisitor {
pub tag: &'static str,
pub content: &'static str,
}
impl DeserializeSeed for TagOrContentFieldVisitor {
type Value = TagOrContentField;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
deserializer.deserialize_str(self)
}
}
impl Visitor for TagOrContentFieldVisitor {
type Value = TagOrContentField;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{:?} or {:?}", self.tag, self.content)
}
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
where E: de::Error
{
if field == self.tag {
Ok(TagOrContentField::Tag)
} else if field == self.content {
Ok(TagOrContentField::Content)
} else {
Err(de::Error::invalid_value(Unexpected::Str(field), &self))
}
}
}
/// Not public API
pub struct ContentDeserializer<E> {
content: Content,
@@ -544,14 +602,17 @@ impl<E> Deserializer for ContentDeserializer<E>
let value = try!(visitor.visit_seq(&mut seq_visitor));
try!(seq_visitor.end());
Ok(value)
},
}
Content::Map(v) => {
let map = v.into_iter().map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
let map = v.into_iter().map(|(k, v)| {
(ContentDeserializer::new(k),
ContentDeserializer::new(v))
});
let mut map_visitor = de::value::MapDeserializer::new(map);
let value = try!(visitor.visit_map(&mut map_visitor));
try!(map_visitor.end());
Ok(value)
},
}
Content::Bytes(v) => visitor.visit_byte_buf(v),
}
}
@@ -563,7 +624,7 @@ impl<E> Deserializer for ContentDeserializer<E>
Content::None => visitor.visit_none(),
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
_ => visitor.visit_some(self),
}
}
@@ -573,10 +634,47 @@ impl<E> Deserializer for ContentDeserializer<E>
visitor.visit_newtype_struct(self)
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor
{
let (variant, value) = match self.content {
Content::Map(value) => {
let mut iter = value.into_iter();
let (variant, value) = match iter.next() {
Some(v) => v,
None => {
return Err(de::Error::invalid_value(de::Unexpected::Map,
&"map with a single key"));
}
};
// enums are encoded in json as maps with a single key:value pair
if iter.next().is_some() {
return Err(de::Error::invalid_value(de::Unexpected::Map,
&"map with a single key"));
}
(variant, Some(value))
}
Content::String(variant) => (Content::String(variant), None),
other => {
return Err(de::Error::invalid_type(other.unexpected(), &"string or map"));
}
};
visitor.visit_enum(EnumDeserializer {
variant: variant,
value: value,
err: PhantomData,
})
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
seq_fixed_size bytes byte_buf map unit_struct tuple_struct struct
struct_field tuple enum ignored_any
struct_field tuple ignored_any
}
}
@@ -590,6 +688,227 @@ impl<E> ContentDeserializer<E> {
}
}
struct EnumDeserializer<E>
where E: de::Error
{
variant: Content,
value: Option<Content>,
err: PhantomData<E>,
}
impl<E> de::EnumVisitor for EnumDeserializer<E>
where E: de::Error
{
type Error = E;
type Variant = VariantDeserializer<Self::Error>;
fn visit_variant_seed<V>(self,
seed: V)
-> Result<(V::Value, VariantDeserializer<E>), Self::Error>
where V: de::DeserializeSeed
{
let visitor = VariantDeserializer {
value: self.value,
err: PhantomData,
};
seed.deserialize(ContentDeserializer::new(self.variant)).map(|v| (v, visitor))
}
}
struct VariantDeserializer<E>
where E: de::Error
{
value: Option<Content>,
err: PhantomData<E>,
}
impl<E> de::VariantVisitor for VariantDeserializer<E>
where E: de::Error
{
type Error = E;
fn visit_unit(self) -> Result<(), E> {
match self.value {
Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)),
None => Ok(()),
}
}
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, E>
where T: de::DeserializeSeed
{
match self.value {
Some(value) => seed.deserialize(ContentDeserializer::new(value)),
None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"newtype variant")),
}
}
fn visit_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
match self.value {
Some(Content::Seq(v)) => {
de::Deserializer::deserialize(SeqDeserializer::new(v), visitor)
}
Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"tuple variant")),
None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"tuple variant")),
}
}
fn visit_struct<V>(self,
_fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
match self.value {
Some(Content::Map(v)) => {
de::Deserializer::deserialize(MapDeserializer::new(v), visitor)
}
Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"struct variant")),
_ => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"struct variant")),
}
}
}
struct SeqDeserializer<E>
where E: de::Error
{
iter: <Vec<Content> as IntoIterator>::IntoIter,
err: PhantomData<E>,
}
impl<E> SeqDeserializer<E>
where E: de::Error
{
fn new(vec: Vec<Content>) -> Self {
SeqDeserializer {
iter: vec.into_iter(),
err: PhantomData,
}
}
}
impl<E> de::Deserializer for SeqDeserializer<E>
where E: de::Error
{
type Error = E;
#[inline]
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
let len = self.iter.len();
if len == 0 {
visitor.visit_unit()
} else {
let ret = try!(visitor.visit_seq(&mut self));
let remaining = self.iter.len();
if remaining == 0 {
Ok(ret)
} else {
Err(de::Error::invalid_length(len, &"fewer elements in array"))
}
}
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
tuple_struct struct struct_field tuple enum ignored_any
}
}
impl<E> de::SeqVisitor for SeqDeserializer<E>
where E: de::Error
{
type Error = E;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed
{
match self.iter.next() {
Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some),
None => Ok(None),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
struct MapDeserializer<E>
where E: de::Error
{
iter: <Vec<(Content, Content)> as IntoIterator>::IntoIter,
value: Option<Content>,
err: PhantomData<E>,
}
impl<E> MapDeserializer<E>
where E: de::Error
{
fn new(map: Vec<(Content, Content)>) -> Self {
MapDeserializer {
iter: map.into_iter(),
value: None,
err: PhantomData,
}
}
}
impl<E> de::MapVisitor for MapDeserializer<E>
where E: de::Error
{
type Error = E;
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed
{
match self.iter.next() {
Some((key, value)) => {
self.value = Some(value);
seed.deserialize(ContentDeserializer::new(key)).map(Some)
}
None => Ok(None),
}
}
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
where T: de::DeserializeSeed
{
match self.value.take() {
Some(value) => seed.deserialize(ContentDeserializer::new(value)),
None => Err(de::Error::custom("value is missing")),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<E> de::Deserializer for MapDeserializer<E>
where E: de::Error
{
type Error = E;
#[inline]
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_map(self)
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
tuple_struct struct struct_field tuple enum ignored_any
}
}
/// Not public API.
pub struct ContentRefDeserializer<'a, E> {
content: &'a Content,
@@ -603,7 +922,7 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
{
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, E>
where V: Visitor
{
match *self.content {
@@ -630,39 +949,79 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
let value = try!(visitor.visit_seq(&mut seq_visitor));
try!(seq_visitor.end());
Ok(value)
},
}
Content::Map(ref v) => {
let map = v.into_iter().map(|&(ref k, ref v)| (ContentRefDeserializer::new(k), ContentRefDeserializer::new(v)));
let map = v.into_iter().map(|&(ref k, ref v)| {
(ContentRefDeserializer::new(k),
ContentRefDeserializer::new(v))
});
let mut map_visitor = de::value::MapDeserializer::new(map);
let value = try!(visitor.visit_map(&mut map_visitor));
try!(map_visitor.end());
Ok(value)
},
}
Content::Bytes(ref v) => visitor.visit_bytes(v),
}
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
where V: Visitor
{
match *self.content {
Content::None => visitor.visit_none(),
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
_ => visitor.visit_some(self),
}
}
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
where V: Visitor
{
visitor.visit_newtype_struct(self)
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor
{
let (variant, value) = match *self.content {
Content::Map(ref value) => {
let mut iter = value.into_iter();
let &(ref variant, ref value) = match iter.next() {
Some(v) => v,
None => {
return Err(de::Error::invalid_value(de::Unexpected::Map,
&"map with a single key"));
}
};
// enums are encoded in json as maps with a single key:value pair
if iter.next().is_some() {
return Err(de::Error::invalid_value(de::Unexpected::Map,
&"map with a single key"));
}
(variant, Some(value))
}
ref s @ Content::String(_) => (s, None),
ref other => {
return Err(de::Error::invalid_type(other.unexpected(), &"string or map"));
}
};
visitor.visit_enum(EnumRefDeserializer {
variant: variant,
value: value,
err: PhantomData,
})
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
seq_fixed_size bytes byte_buf map unit_struct tuple_struct struct
struct_field tuple enum ignored_any
struct_field tuple ignored_any
}
}
@@ -676,6 +1035,224 @@ impl<'a, E> ContentRefDeserializer<'a, E> {
}
}
struct EnumRefDeserializer<'a, E>
where E: de::Error
{
variant: &'a Content,
value: Option<&'a Content>,
err: PhantomData<E>,
}
impl<'a, E> de::EnumVisitor for EnumRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
type Variant = VariantRefDeserializer<'a, Self::Error>;
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where V: de::DeserializeSeed
{
let visitor = VariantRefDeserializer {
value: self.value,
err: PhantomData,
};
seed.deserialize(ContentRefDeserializer::new(self.variant)).map(|v| (v, visitor))
}
}
struct VariantRefDeserializer<'a, E>
where E: de::Error
{
value: Option<&'a Content>,
err: PhantomData<E>,
}
impl<'a, E> de::VariantVisitor for VariantRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
fn visit_unit(self) -> Result<(), E> {
match self.value {
Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)),
None => Ok(()),
}
}
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, E>
where T: de::DeserializeSeed
{
match self.value {
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"newtype variant")),
}
}
fn visit_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
match self.value {
Some(&Content::Seq(ref v)) => {
de::Deserializer::deserialize(SeqRefDeserializer::new(v), visitor)
}
Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"tuple variant")),
None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"tuple variant")),
}
}
fn visit_struct<V>(self,
_fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
match self.value {
Some(&Content::Map(ref v)) => {
de::Deserializer::deserialize(MapRefDeserializer::new(v), visitor)
}
Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"struct variant")),
_ => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"struct variant")),
}
}
}
struct SeqRefDeserializer<'a, E>
where E: de::Error
{
iter: <&'a [Content] as IntoIterator>::IntoIter,
err: PhantomData<E>,
}
impl<'a, E> SeqRefDeserializer<'a, E>
where E: de::Error
{
fn new(vec: &'a [Content]) -> Self {
SeqRefDeserializer {
iter: vec.into_iter(),
err: PhantomData,
}
}
}
impl<'a, E> de::Deserializer for SeqRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
#[inline]
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
let len = self.iter.len();
if len == 0 {
visitor.visit_unit()
} else {
let ret = try!(visitor.visit_seq(&mut self));
let remaining = self.iter.len();
if remaining == 0 {
Ok(ret)
} else {
Err(de::Error::invalid_length(len, &"fewer elements in array"))
}
}
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
tuple_struct struct struct_field tuple enum ignored_any
}
}
impl<'a, E> de::SeqVisitor for SeqRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed
{
match self.iter.next() {
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)).map(Some),
None => Ok(None),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
struct MapRefDeserializer<'a, E>
where E: de::Error
{
iter: <&'a [(Content, Content)] as IntoIterator>::IntoIter,
value: Option<&'a Content>,
err: PhantomData<E>,
}
impl<'a, E> MapRefDeserializer<'a, E>
where E: de::Error
{
fn new(map: &'a [(Content, Content)]) -> Self {
MapRefDeserializer {
iter: map.into_iter(),
value: None,
err: PhantomData,
}
}
}
impl<'a, E> de::MapVisitor for MapRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed
{
match self.iter.next() {
Some(&(ref key, ref value)) => {
self.value = Some(value);
seed.deserialize(ContentRefDeserializer::new(key)).map(Some)
}
None => Ok(None),
}
}
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
where T: de::DeserializeSeed
{
match self.value.take() {
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
None => Err(de::Error::custom("value is missing")),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, E> de::Deserializer for MapRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
#[inline]
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_map(self)
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
tuple_struct struct struct_field tuple enum ignored_any
}
}
impl<E> de::value::ValueDeserializer<E> for ContentDeserializer<E>
where E: de::Error
{
+437 -111
View File
@@ -6,30 +6,16 @@ use std::borrow::Cow;
use collections::borrow::Cow;
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::{
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
VecDeque,
Vec,
String,
};
use collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque, Vec, String};
#[cfg(feature = "std")]
use std::collections::{
HashMap,
HashSet,
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
VecDeque,
};
use std::collections::{HashMap, HashSet, BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque};
#[cfg(feature = "collections")]
use collections::borrow::ToOwned;
#[cfg(any(feature = "std", feature = "collections"))]
use core::cmp;
use core::fmt;
#[cfg(feature = "std")]
use core::hash::{Hash, BuildHasher};
@@ -39,6 +25,10 @@ use std::net;
#[cfg(feature = "std")]
use std::path;
use core::str;
#[cfg(feature = "std")]
use std::ffi::{CString, OsString};
#[cfg(all(feature = "std", feature="unstable"))]
use std::ffi::CStr;
#[cfg(feature = "std")]
use std::rc::Rc;
@@ -53,9 +43,17 @@ use alloc::arc::Arc;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
use core::cell::{Cell, RefCell};
#[cfg(feature = "std")]
use std::sync::{Mutex, RwLock};
#[cfg(feature = "std")]
use std::time::Duration;
#[cfg(feature = "std")]
use std;
#[cfg(feature = "unstable")]
use core::nonzero::{NonZero, Zeroable};
@@ -63,19 +61,13 @@ use core::nonzero::{NonZero, Zeroable};
#[allow(deprecated)] // required for impl Deserialize for NonZero<T>
use core::num::Zero;
use de::{
Deserialize,
Deserializer,
EnumVisitor,
Error,
MapVisitor,
SeqVisitor,
Unexpected,
VariantVisitor,
Visitor,
};
use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor, Unexpected,
VariantVisitor, Visitor};
use de::from_primitive::FromPrimitive;
#[cfg(feature = "std")]
use bytes::ByteBuf;
///////////////////////////////////////////////////////////////////////////////
/// A visitor that produces a `()`.
@@ -89,13 +81,13 @@ impl Visitor for UnitVisitor {
}
fn visit_unit<E>(self) -> Result<(), E>
where E: Error,
where E: Error
{
Ok(())
}
fn visit_seq<V>(self, _: V) -> Result<(), V::Error>
where V: SeqVisitor,
where V: SeqVisitor
{
Ok(())
}
@@ -103,7 +95,7 @@ impl Visitor for UnitVisitor {
impl Deserialize for () {
fn deserialize<D>(deserializer: D) -> Result<(), D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_unit(UnitVisitor)
}
@@ -122,13 +114,13 @@ impl Visitor for BoolVisitor {
}
fn visit_bool<E>(self, v: bool) -> Result<bool, E>
where E: Error,
where E: Error
{
Ok(v)
}
fn visit_str<E>(self, s: &str) -> Result<bool, E>
where E: Error,
where E: Error
{
match s.trim_matches(::utils::Pattern_White_Space) {
"true" => Ok(true),
@@ -140,7 +132,7 @@ impl Visitor for BoolVisitor {
impl Deserialize for bool {
fn deserialize<D>(deserializer: D) -> Result<bool, D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_bool(BoolVisitor)
}
@@ -231,14 +223,14 @@ impl Visitor for CharVisitor {
#[inline]
fn visit_char<E>(self, v: char) -> Result<char, E>
where E: Error,
where E: Error
{
Ok(v)
}
#[inline]
fn visit_str<E>(self, v: &str) -> Result<char, E>
where E: Error,
where E: Error
{
let mut iter = v.chars();
match (iter.next(), iter.next()) {
@@ -251,7 +243,7 @@ impl Visitor for CharVisitor {
impl Deserialize for char {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<char, D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_char(CharVisitor)
}
@@ -271,25 +263,25 @@ impl Visitor for StringVisitor {
}
fn visit_str<E>(self, v: &str) -> Result<String, E>
where E: Error,
where E: Error
{
Ok(v.to_owned())
}
fn visit_string<E>(self, v: String) -> Result<String, E>
where E: Error,
where E: Error
{
Ok(v)
}
fn visit_unit<E>(self) -> Result<String, E>
where E: Error,
where E: Error
{
Ok(String::new())
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<String, E>
where E: Error,
where E: Error
{
match str::from_utf8(v) {
Ok(s) => Ok(s.to_owned()),
@@ -298,7 +290,7 @@ impl Visitor for StringVisitor {
}
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<String, E>
where E: Error,
where E: Error
{
match String::from_utf8(v) {
Ok(s) => Ok(s),
@@ -310,7 +302,7 @@ impl Visitor for StringVisitor {
#[cfg(any(feature = "std", feature = "collections"))]
impl Deserialize for String {
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_string(StringVisitor)
}
@@ -318,13 +310,33 @@ impl Deserialize for String {
///////////////////////////////////////////////////////////////////////////////
#[cfg(all(feature = "std", feature="unstable"))]
impl Deserialize for Box<CStr> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer
{
let s = try!(CString::deserialize(deserializer));
Ok(s.into_boxed_c_str())
}
}
#[cfg(feature = "std")]
impl Deserialize for CString {
fn deserialize<D>(deserializer: D) -> Result<CString, D::Error>
where D: Deserializer
{
let bytes = try!(ByteBuf::deserialize(deserializer));
CString::new(bytes).map_err(Error::custom)
}
}
///////////////////////////////////////////////////////////////////////////////
struct OptionVisitor<T> {
marker: PhantomData<T>,
}
impl<
T: Deserialize,
> Visitor for OptionVisitor<T> {
impl<T: Deserialize> Visitor for OptionVisitor<T> {
type Value = Option<T>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@@ -333,29 +345,31 @@ impl<
#[inline]
fn visit_unit<E>(self) -> Result<Option<T>, E>
where E: Error,
where E: Error
{
Ok(None)
}
#[inline]
fn visit_none<E>(self) -> Result<Option<T>, E>
where E: Error,
where E: Error
{
Ok(None)
}
#[inline]
fn visit_some<D>(self, deserializer: D) -> Result<Option<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
Ok(Some(try!(Deserialize::deserialize(deserializer))))
}
}
impl<T> Deserialize for Option<T> where T: Deserialize {
impl<T> Deserialize for Option<T>
where T: Deserialize
{
fn deserialize<D>(deserializer: D) -> Result<Option<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_option(OptionVisitor { marker: PhantomData })
}
@@ -377,7 +391,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
#[inline]
fn visit_unit<E>(self) -> Result<PhantomData<T>, E>
where E: Error,
where E: Error
{
Ok(PhantomData)
}
@@ -385,7 +399,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
impl<T> Deserialize for PhantomData<T> {
fn deserialize<D>(deserializer: D) -> Result<PhantomData<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let visitor = PhantomDataVisitor { marker: PhantomData };
deserializer.deserialize_unit_struct("PhantomData", visitor)
@@ -467,7 +481,7 @@ seq_impl!(
BinaryHeapVisitor<T: Deserialize + Ord>,
visitor,
BinaryHeap::new(),
BinaryHeap::with_capacity(visitor.size_hint().0),
BinaryHeap::with_capacity(cmp::min(visitor.size_hint().0, 4096)),
BinaryHeap::push);
#[cfg(any(feature = "std", feature = "collections"))]
@@ -495,7 +509,7 @@ seq_impl!(
S: BuildHasher + Default>,
visitor,
HashSet::with_hasher(S::default()),
HashSet::with_capacity_and_hasher(visitor.size_hint().0, S::default()),
HashSet::with_capacity_and_hasher(cmp::min(visitor.size_hint().0, 4096), S::default()),
HashSet::insert);
#[cfg(any(feature = "std", feature = "collections"))]
@@ -504,7 +518,7 @@ seq_impl!(
VecVisitor<T: Deserialize>,
visitor,
Vec::new(),
Vec::with_capacity(visitor.size_hint().0),
Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)),
Vec::push);
#[cfg(any(feature = "std", feature = "collections"))]
@@ -513,7 +527,7 @@ seq_impl!(
VecDequeVisitor<T: Deserialize>,
visitor,
VecDeque::new(),
VecDeque::with_capacity(visitor.size_hint().0),
VecDeque::with_capacity(cmp::min(visitor.size_hint().0, 4096)),
VecDeque::push_back);
///////////////////////////////////////////////////////////////////////////////
@@ -524,13 +538,13 @@ struct ArrayVisitor<A> {
impl<A> ArrayVisitor<A> {
pub fn new() -> Self {
ArrayVisitor {
marker: PhantomData,
}
ArrayVisitor { marker: PhantomData }
}
}
impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
impl<T> Visitor for ArrayVisitor<[T; 0]>
where T: Deserialize
{
type Value = [T; 0];
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@@ -539,14 +553,14 @@ impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
#[inline]
fn visit_unit<E>(self) -> Result<[T; 0], E>
where E: Error,
where E: Error
{
Ok([])
}
#[inline]
fn visit_seq<V>(self, _: V) -> Result<[T; 0], V::Error>
where V: SeqVisitor,
where V: SeqVisitor
{
Ok([])
}
@@ -556,7 +570,7 @@ impl<T> Deserialize for [T; 0]
where T: Deserialize
{
fn deserialize<D>(deserializer: D) -> Result<[T; 0], D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new())
}
@@ -791,14 +805,14 @@ map_impl!(
S: BuildHasher + Default>,
visitor,
HashMap::with_hasher(S::default()),
HashMap::with_capacity_and_hasher(visitor.size_hint().0, S::default()));
HashMap::with_capacity_and_hasher(cmp::min(visitor.size_hint().0, 4096), S::default()));
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
impl Deserialize for net::IpAddr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -811,7 +825,7 @@ impl Deserialize for net::IpAddr {
#[cfg(feature = "std")]
impl Deserialize for net::Ipv4Addr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -824,7 +838,7 @@ impl Deserialize for net::Ipv4Addr {
#[cfg(feature = "std")]
impl Deserialize for net::Ipv6Addr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -839,7 +853,7 @@ impl Deserialize for net::Ipv6Addr {
#[cfg(feature = "std")]
impl Deserialize for net::SocketAddr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -852,7 +866,7 @@ impl Deserialize for net::SocketAddr {
#[cfg(feature = "std")]
impl Deserialize for net::SocketAddrV4 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -865,7 +879,7 @@ impl Deserialize for net::SocketAddrV4 {
#[cfg(feature = "std")]
impl Deserialize for net::SocketAddrV6 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
match s.parse() {
@@ -889,22 +903,23 @@ impl Visitor for PathBufVisitor {
}
fn visit_str<E>(self, v: &str) -> Result<path::PathBuf, E>
where E: Error,
where E: Error
{
Ok(From::from(v))
}
fn visit_string<E>(self, v: String) -> Result<path::PathBuf, E>
where E: Error,
where E: Error
{
Ok(From::from(v))
}
}
#[cfg(feature = "std")]
impl Deserialize for path::PathBuf {
fn deserialize<D>(deserializer: D) -> Result<path::PathBuf, D::Error>
where D: Deserializer,
where D: Deserializer
{
deserializer.deserialize_string(PathBufVisitor)
}
@@ -912,10 +927,130 @@ impl Deserialize for path::PathBuf {
///////////////////////////////////////////////////////////////////////////////
#[cfg(all(feature = "std", any(unix, windows)))]
enum OsStringKind {
Unix,
Windows,
}
#[cfg(all(feature = "std", any(unix, windows)))]
static OSSTR_VARIANTS: &'static [&'static str] = &["Unix", "Windows"];
#[cfg(all(feature = "std", any(unix, windows)))]
impl Deserialize for OsStringKind {
fn deserialize<D>(deserializer: D) -> Result<OsStringKind, D::Error>
where D: Deserializer
{
struct KindVisitor;
impl Visitor for KindVisitor {
type Value = OsStringKind;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`Unix` or `Windows`")
}
fn visit_u32<E>(self, value: u32) -> Result<OsStringKind, E>
where E: Error,
{
match value {
0 => Ok(OsStringKind::Unix),
1 => Ok(OsStringKind::Windows),
_ => Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self))
}
}
fn visit_str<E>(self, value: &str) -> Result<OsStringKind, E>
where E: Error,
{
match value {
"Unix" => Ok(OsStringKind::Unix),
"Windows" => Ok(OsStringKind::Windows),
_ => Err(Error::unknown_variant(value, OSSTR_VARIANTS)),
}
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<OsStringKind, E>
where E: Error,
{
match value {
b"Unix" => Ok(OsStringKind::Unix),
b"Windows" => Ok(OsStringKind::Windows),
_ => {
match str::from_utf8(value) {
Ok(value) => Err(Error::unknown_variant(value, OSSTR_VARIANTS)),
Err(_) => {
Err(Error::invalid_value(Unexpected::Bytes(value), &self))
}
}
}
}
}
}
deserializer.deserialize(KindVisitor)
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
struct OsStringVisitor;
#[cfg(all(feature = "std", any(unix, windows)))]
impl Visitor for OsStringVisitor {
type Value = OsString;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("os string")
}
#[cfg(unix)]
fn visit_enum<V>(self, visitor: V) -> Result<OsString, V::Error>
where V: EnumVisitor,
{
use std::os::unix::ffi::OsStringExt;
match try!(visitor.visit_variant()) {
(OsStringKind::Unix, variant) => {
variant.visit_newtype().map(OsString::from_vec)
}
(OsStringKind::Windows, _) => {
Err(Error::custom("cannot deserialize Windows OS string on Unix"))
}
}
}
#[cfg(windows)]
fn visit_enum<V>(self, visitor: V) -> Result<OsString, V::Error>
where V: EnumVisitor,
{
use std::os::windows::ffi::OsStringExt;
match try!(visitor.visit_variant()) {
(OsStringKind::Windows, variant) => {
variant.visit_newtype::<Vec<u16>>().map(|vec| OsString::from_wide(&vec))
}
(OsStringKind::Unix, _) => {
Err(Error::custom("cannot deserialize Unix OS string on Windows"))
}
}
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
impl Deserialize for OsString {
fn deserialize<D>(deserializer: D) -> Result<OsString, D::Error>
where D: Deserializer
{
deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor)
}
}
///////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Box<T> {
fn deserialize<D>(deserializer: D) -> Result<Box<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Box::new(val))
@@ -925,7 +1060,7 @@ impl<T: Deserialize> Deserialize for Box<T> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<T: Deserialize> Deserialize for Box<[T]> {
fn deserialize<D>(deserializer: D) -> Result<Box<[T]>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
Ok(v.into_boxed_slice())
@@ -945,7 +1080,7 @@ impl Deserialize for Box<str> {
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Arc<T> {
fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Arc::new(val))
@@ -955,7 +1090,7 @@ impl<T: Deserialize> Deserialize for Arc<T> {
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Rc<T> {
fn deserialize<D>(deserializer: D) -> Result<Rc<T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Rc::new(val))
@@ -963,16 +1098,57 @@ 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]
fn deserialize<D>(deserializer: D) -> Result<Cow<'a, T>, D::Error>
where D: Deserializer,
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Cow::Owned(val))
}
}
impl<T: Deserialize + Copy> Deserialize for Cell<T> {
fn deserialize<D>(deserializer: D) -> Result<Cell<T>, D::Error>
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Cell::new(val))
}
}
impl<T: Deserialize> Deserialize for RefCell<T> {
fn deserialize<D>(deserializer: D) -> Result<RefCell<T>, D::Error>
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(RefCell::new(val))
}
}
#[cfg(feature = "std")]
impl<T: Deserialize> Deserialize for Mutex<T> {
fn deserialize<D>(deserializer: D) -> Result<Mutex<T>, D::Error>
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Mutex::new(val))
}
}
#[cfg(feature = "std")]
impl<T: Deserialize> Deserialize for RwLock<T> {
fn deserialize<D>(deserializer: D) -> Result<RwLock<T>, D::Error>
where D: Deserializer
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(RwLock::new(val))
}
}
///////////////////////////////////////////////////////////////////////////////
// This is a cleaned-up version of the impl generated by:
@@ -986,13 +1162,16 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deser
#[cfg(feature = "std")]
impl Deserialize for Duration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
where D: Deserializer
{
enum Field { Secs, Nanos };
enum Field {
Secs,
Nanos,
};
impl Deserialize for Field {
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
where D: Deserializer,
where D: Deserializer
{
struct FieldVisitor;
@@ -1004,7 +1183,7 @@ impl Deserialize for Duration {
}
fn visit_str<E>(self, value: &str) -> Result<Field, E>
where E: Error,
where E: Error
{
match value {
"secs" => Ok(Field::Secs),
@@ -1014,7 +1193,7 @@ impl Deserialize for Duration {
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
where E: Error,
where E: Error
{
match value {
b"secs" => Ok(Field::Secs),
@@ -1041,7 +1220,7 @@ impl Deserialize for Duration {
}
fn visit_seq<V>(self, mut visitor: V) -> Result<Duration, V::Error>
where V: SeqVisitor,
where V: SeqVisitor
{
let secs: u64 = match try!(visitor.visit()) {
Some(value) => value,
@@ -1059,7 +1238,7 @@ impl Deserialize for Duration {
}
fn visit_map<V>(self, mut visitor: V) -> Result<Duration, V::Error>
where V: MapVisitor,
where V: MapVisitor
{
let mut secs: Option<u64> = None;
let mut nanos: Option<u32> = None;
@@ -1098,26 +1277,163 @@ impl Deserialize for Duration {
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "unstable")]
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
let value = try!(Deserialize::deserialize(deserializer));
if value == Zero::zero() {
return Err(Error::custom("expected a non-zero value"))
// Similar to:
//
// #[derive(Deserialize)]
// #[serde(deny_unknown_fields)]
// struct Range {
// start: u64,
// end: u32,
// }
#[cfg(feature = "std")]
impl<Idx: Deserialize> Deserialize for std::ops::Range<Idx> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer
{
enum Field {
Start,
End,
};
impl Deserialize for Field {
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
where D: Deserializer
{
struct FieldVisitor;
impl Visitor for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`start` or `end`")
}
fn visit_str<E>(self, value: &str) -> Result<Field, E>
where E: Error
{
match value {
"start" => Ok(Field::Start),
"end" => Ok(Field::End),
_ => Err(Error::unknown_field(value, FIELDS)),
}
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
where E: Error
{
match value {
b"start" => Ok(Field::Start),
b"end" => Ok(Field::End),
_ => {
let value = String::from_utf8_lossy(value);
Err(Error::unknown_field(&value, FIELDS))
}
}
}
}
deserializer.deserialize_struct_field(FieldVisitor)
}
}
unsafe {
Ok(NonZero::new(value))
struct RangeVisitor<Idx> {
phantom: PhantomData<Idx>,
}
impl<Idx: Deserialize> Visitor for RangeVisitor<Idx> {
type Value = std::ops::Range<Idx>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct Range")
}
fn visit_seq<V>(self, mut visitor: V) -> Result<std::ops::Range<Idx>, V::Error>
where V: SeqVisitor
{
let start: Idx = match try!(visitor.visit()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
let end: Idx = match try!(visitor.visit()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(1, &self));
}
};
Ok(start..end)
}
fn visit_map<V>(self, mut visitor: V) -> Result<std::ops::Range<Idx>, V::Error>
where V: MapVisitor
{
let mut start: Option<Idx> = None;
let mut end: Option<Idx> = None;
while let Some(key) = try!(visitor.visit_key::<Field>()) {
match key {
Field::Start => {
if start.is_some() {
return Err(<V::Error as Error>::duplicate_field("start"));
}
start = Some(try!(visitor.visit_value()));
}
Field::End => {
if end.is_some() {
return Err(<V::Error as Error>::duplicate_field("end"));
}
end = Some(try!(visitor.visit_value()));
}
}
}
let start = match start {
Some(start) => start,
None => return Err(<V::Error as Error>::missing_field("start")),
};
let end = match end {
Some(end) => end,
None => return Err(<V::Error as Error>::missing_field("end")),
};
Ok(start..end)
}
}
const FIELDS: &'static [&'static str] = &["start", "end"];
deserializer.deserialize_struct("Range", FIELDS, RangeVisitor { phantom: PhantomData })
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "unstable")]
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
impl<T> Deserialize for NonZero<T>
where T: Deserialize + PartialEq + Zeroable + Zero
{
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error>
where D: Deserializer
{
let value = try!(Deserialize::deserialize(deserializer));
if value == Zero::zero() {
return Err(Error::custom("expected a non-zero value"));
}
unsafe { Ok(NonZero::new(value)) }
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T, E> Deserialize for Result<T, E>
where T: Deserialize,
E: Deserialize
{
fn deserialize<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
where D: Deserializer {
where D: Deserializer
{
enum Field {
Ok,
Err,
@@ -1137,15 +1453,21 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
formatter.write_str("`Ok` or `Err`")
}
fn visit_u32<E>(self, value: u32) -> Result<Field, E> where E: Error {
fn visit_u32<E>(self, value: u32) -> Result<Field, E>
where E: Error
{
match value {
0 => Ok(Field::Ok),
1 => Ok(Field::Err),
_ => Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self)),
_ => {
Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self))
}
}
}
fn visit_str<E>(self, value: &str) -> Result<Field, E> where E: Error {
fn visit_str<E>(self, value: &str) -> Result<Field, E>
where E: Error
{
match value {
"Ok" => Ok(Field::Ok),
"Err" => Ok(Field::Err),
@@ -1153,14 +1475,18 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
}
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E> where E: Error {
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
where E: Error
{
match value {
b"Ok" => Ok(Field::Ok),
b"Err" => Ok(Field::Err),
_ => {
match str::from_utf8(value) {
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)),
Err(_) => {
Err(Error::invalid_value(Unexpected::Bytes(value), &self))
}
}
}
}
@@ -1208,7 +1534,7 @@ pub struct IgnoredAny;
impl Deserialize for IgnoredAny {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
where D: Deserializer
{
struct IgnoredAnyVisitor;
@@ -1241,7 +1567,7 @@ impl Deserialize for IgnoredAny {
#[inline]
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
where E: Error,
where E: Error
{
Ok(IgnoredAny)
}
@@ -1253,14 +1579,14 @@ impl Deserialize for IgnoredAny {
#[inline]
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
where D: Deserializer
{
Ok(IgnoredAny)
}
#[inline]
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
where D: Deserializer
{
Ok(IgnoredAny)
}
@@ -1272,7 +1598,7 @@ impl Deserialize for IgnoredAny {
#[inline]
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
where V: SeqVisitor,
where V: SeqVisitor
{
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
// Gobble
@@ -1282,7 +1608,7 @@ impl Deserialize for IgnoredAny {
#[inline]
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
where V: MapVisitor,
where V: MapVisitor
{
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
// Gobble
@@ -1292,7 +1618,7 @@ impl Deserialize for IgnoredAny {
#[inline]
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
where E: Error,
where E: Error
{
Ok(IgnoredAny)
}
+105 -172
View File
@@ -64,6 +64,10 @@
//! - Rc\<T\>
//! - Arc\<T\>
//! - Cow\<'a, T\>
//! - Cell\<T\>
//! - RefCell\<T\>
//! - Mutex\<T\>
//! - RwLock\<T\>
//! - **Collection types**:
//! - BTreeMap\<K, V\>
//! - BTreeSet\<T\>
@@ -172,16 +176,7 @@ pub trait Error: Sized + error::Error {
/// containing an integer, the unexpected type is the integer and the
/// expected type is the string.
fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
struct InvalidType<'a> {
unexp: Unexpected<'a>,
exp: &'a Expected,
}
impl<'a> Display for InvalidType<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid type: {}, expected {}", self.unexp, self.exp)
}
}
Error::custom(InvalidType { unexp: unexp, exp: exp })
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
}
/// Raised when a `Deserialize` receives a value of the right type but that
@@ -198,16 +193,7 @@ pub trait Error: Sized + error::Error {
/// that is not valid UTF-8, the unexpected value is the bytes and the
/// expected value is a string.
fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
struct InvalidValue<'a> {
unexp: Unexpected<'a>,
exp: &'a Expected,
}
impl<'a> Display for InvalidValue<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid value: {}, expected {}", self.unexp, self.exp)
}
}
Error::custom(InvalidValue { unexp: unexp, exp: exp })
Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
}
/// Raised when deserializing a sequence or map and the input data contains
@@ -220,93 +206,46 @@ pub trait Error: Sized + error::Error {
/// expected. For example `exp` might say that a tuple of size 6 was
/// expected.
fn invalid_length(len: usize, exp: &Expected) -> Self {
struct InvalidLength<'a> {
len: usize,
exp: &'a Expected,
}
impl<'a> Display for InvalidLength<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "invalid length {}, expected {}", self.len, self.exp)
}
}
Error::custom(InvalidLength { len: len, exp: exp })
Error::custom(format_args!("invalid length {}, expected {}", len, exp))
}
/// Raised when a `Deserialize` enum type received a variant with an
/// unrecognized name.
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
struct UnknownVariant<'a> {
variant: &'a str,
expected: &'static [&'static str],
if expected.is_empty() {
Error::custom(format_args!("unknown variant `{}`, there are no variants",
variant))
} else {
Error::custom(format_args!("unknown variant `{}`, expected {}",
variant,
OneOf { names: expected }))
}
impl<'a> Display for UnknownVariant<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if self.expected.is_empty() {
write!(formatter,
"unknown variant `{}`, there are no variants",
self.variant)
} else {
write!(formatter,
"unknown variant `{}`, expected {}",
self.variant,
OneOf { names: self.expected })
}
}
}
Error::custom(UnknownVariant { variant: variant, expected: expected })
}
/// Raised when a `Deserialize` struct type received a field with an
/// unrecognized name.
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
struct UnknownField<'a> {
field: &'a str,
expected: &'static [&'static str],
if expected.is_empty() {
Error::custom(format_args!("unknown field `{}`, there are no fields",
field))
} else {
Error::custom(format_args!("unknown field `{}`, expected {}",
field,
OneOf { names: expected }))
}
impl<'a> Display for UnknownField<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if self.expected.is_empty() {
write!(formatter,
"unknown field `{}`, there are no fields",
self.field)
} else {
write!(formatter,
"unknown field `{}`, expected {}",
self.field,
OneOf { names: self.expected })
}
}
}
Error::custom(UnknownField { field: field, expected: expected })
}
/// Raised when a `Deserialize` struct type expected to receive a required
/// field with a particular name but that field was not present in the
/// input.
fn missing_field(field: &'static str) -> Self {
struct MissingField {
field: &'static str,
}
impl Display for MissingField {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "missing field `{}`", self.field)
}
}
Error::custom(MissingField { field: field })
Error::custom(format_args!("missing field `{}`", field))
}
/// Raised when a `Deserialize` struct type received more than one of the
/// same field.
fn duplicate_field(field: &'static str) -> Self {
struct DuplicateField {
field: &'static str,
}
impl Display for DuplicateField {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "duplicate field `{}`", self.field)
}
}
Error::custom(DuplicateField { field: field })
Error::custom(format_args!("duplicate field `{}`", field))
}
}
@@ -470,7 +409,9 @@ pub trait Expected {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;
}
impl<T> Expected for T where T: Visitor {
impl<T> Expected for T
where T: Visitor
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.expecting(formatter)
}
@@ -521,8 +462,7 @@ pub trait Deserialize: Sized {
/// manual for more information about how to implement this method.
///
/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer;
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer;
}
/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
@@ -670,8 +610,7 @@ pub trait DeserializeSeed: Sized {
/// Equivalent to the more common `Deserialize::deserialize` method, except
/// with some initial piece of data (the seed) passed in.
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer;
}
impl<T> DeserializeSeed for PhantomData<T>
@@ -784,56 +723,43 @@ pub trait Deserializer: Sized {
/// `Deserializer::deserialize` means your data type will be able to
/// deserialize from self-describing formats only, ruling out Bincode and
/// many others.
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `bool` value.
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `u8` value.
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `u16` value.
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `u32` value.
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `u64` value.
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting an `i8` value.
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting an `i16` value.
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting an `i32` value.
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting an `i64` value.
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `f32` value.
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `f64` value.
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a `char` value.
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a string value and does
/// not benefit from taking ownership of buffered data owned by the
@@ -842,8 +768,7 @@ pub trait Deserializer: Sized {
/// If the `Visitor` would benefit from taking ownership of `String` data,
/// indiciate this to the `Deserializer` by using `deserialize_string`
/// instead.
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a string value and would
/// benefit from taking ownership of buffered data owned by the
@@ -852,8 +777,7 @@ pub trait Deserializer: Sized {
/// If the `Visitor` would not benefit from taking ownership of `String`
/// data, indicate that to the `Deserializer` by using `deserialize_str`
/// instead.
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a byte array and does not
/// benefit from taking ownership of buffered data owned by the
@@ -862,8 +786,7 @@ pub trait Deserializer: Sized {
/// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
/// indicate this to the `Deserializer` by using `deserialize_byte_buf`
/// instead.
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a byte array and would
/// benefit from taking ownership of buffered data owned by the
@@ -872,44 +795,43 @@ pub trait Deserializer: Sized {
/// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
/// data, indicate that to the `Deserializer` by using `deserialize_bytes`
/// instead.
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting an optional value.
///
/// This allows deserializers that encode an optional value as a nullable
/// value to convert the null value into `None` and a regular value into
/// `Some(value)`.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a unit value.
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a unit struct with a
/// particular name.
fn deserialize_unit_struct<V>(self,
name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type is expecting a newtype struct with a
/// particular name.
fn deserialize_newtype_struct<V>(self,
name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type is expecting a sequence of values.
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a sequence of values and
/// knows how many values there are without looking at the serialized data.
fn deserialize_seq_fixed_size<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type is expecting a tuple value with a
@@ -922,19 +844,20 @@ pub trait Deserializer: Sized {
fn deserialize_tuple_struct<V>(self,
name: &'static str,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type is expecting a map of key-value pairs.
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor;
/// Hint that the `Deserialize` type is expecting a struct with a particular
/// name and fields.
fn deserialize_struct<V>(self,
name: &'static str,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type is expecting the name of a struct
@@ -947,7 +870,8 @@ pub trait Deserializer: Sized {
fn deserialize_enum<V>(self,
name: &'static str,
variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
/// Hint that the `Deserialize` type needs to deserialize a value whose type
@@ -1016,77 +940,77 @@ pub trait Visitor: Sized {
/// Deserialize a `bool` into a `Value`.
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Bool(v), &self))
}
/// Deserialize an `i8` into a `Value`.
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_i64(v as i64)
}
/// Deserialize an `i16` into a `Value`.
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_i64(v as i64)
}
/// Deserialize an `i32` into a `Value`.
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_i64(v as i64)
}
/// Deserialize an `i64` into a `Value`.
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Signed(v), &self))
}
/// Deserialize a `u8` into a `Value`.
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_u64(v as u64)
}
/// Deserialize a `u16` into a `Value`.
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_u64(v as u64)
}
/// Deserialize a `u32` into a `Value`.
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_u64(v as u64)
}
/// Deserialize a `u64` into a `Value`.
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
}
/// Deserialize a `f32` into a `Value`.
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_f64(v as f64)
}
/// Deserialize a `f64` into a `Value`.
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Float(v), &self))
}
@@ -1094,7 +1018,7 @@ pub trait Visitor: Sized {
/// Deserialize a `char` into a `Value`.
#[inline]
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_str(::utils::encode_utf8(v).as_str())
}
@@ -1110,7 +1034,7 @@ pub trait Visitor: Sized {
/// It is never correct to implement `visit_string` without implementing
/// `visit_str`. Implement neither, both, or just `visit_str`.
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Str(v), &self))
}
@@ -1132,28 +1056,28 @@ pub trait Visitor: Sized {
#[inline]
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_str(&v)
}
/// Deserialize a `()` into a `Value`.
fn visit_unit<E>(self) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Unit, &self))
}
/// Deserialize an absent optional `Value`.
fn visit_none<E>(self) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
Err(Error::invalid_type(Unexpected::Option, &self))
}
/// Deserialize a present optional `Value`.
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer,
where D: Deserializer
{
let _ = deserializer;
Err(Error::invalid_type(Unexpected::Option, &self))
@@ -1161,7 +1085,7 @@ pub trait Visitor: Sized {
/// Deserialize `Value` as a newtype struct.
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer,
where D: Deserializer
{
let _ = deserializer;
Err(Error::invalid_type(Unexpected::NewtypeStruct, &self))
@@ -1169,7 +1093,7 @@ pub trait Visitor: Sized {
/// Deserialize `Value` as a sequence of elements.
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor,
where V: SeqVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Seq, &self))
@@ -1177,7 +1101,7 @@ pub trait Visitor: Sized {
/// Deserialize `Value` as a key-value map.
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor,
where V: MapVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Map, &self))
@@ -1185,7 +1109,7 @@ pub trait Visitor: Sized {
/// Deserialize `Value` as an enum.
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: EnumVisitor,
where V: EnumVisitor
{
let _ = visitor;
Err(Error::invalid_type(Unexpected::Enum, &self))
@@ -1202,7 +1126,7 @@ pub trait Visitor: Sized {
/// It is never correct to implement `visit_byte_buf` without implementing
/// `visit_bytes`. Implement neither, both, or just `visit_bytes`.
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
let _ = v;
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
@@ -1225,7 +1149,7 @@ pub trait Visitor: Sized {
/// `Vec<u8>`.
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where E: Error,
where E: Error
{
self.visit_bytes(&v)
}
@@ -1269,7 +1193,9 @@ pub trait SeqVisitor {
}
}
impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
impl<'a, V> SeqVisitor for &'a mut V
where V: SeqVisitor
{
type Error = V::Error;
#[inline]
@@ -1326,7 +1252,10 @@ pub trait MapVisitor {
/// `Deserialize` implementations should typically use `MapVisitor::visit`
/// instead.
#[inline]
fn visit_seed<K, V>(&mut self, kseed: K, vseed: V) -> Result<Option<(K::Value, V::Value)>, Self::Error>
fn visit_seed<K, V>(&mut self,
kseed: K,
vseed: V)
-> Result<Option<(K::Value, V::Value)>, Self::Error>
where K: DeserializeSeed,
V: DeserializeSeed
{
@@ -1335,7 +1264,7 @@ pub trait MapVisitor {
let value = try!(self.visit_value_seed(vseed));
Ok(Some((key, value)))
}
None => Ok(None)
None => Ok(None),
}
}
@@ -1370,7 +1299,7 @@ pub trait MapVisitor {
#[inline]
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
where K: Deserialize,
V: Deserialize,
V: Deserialize
{
self.visit_seed(PhantomData, PhantomData)
}
@@ -1382,7 +1311,9 @@ pub trait MapVisitor {
}
}
impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
impl<'a, V_> MapVisitor for &'a mut V_
where V_: MapVisitor
{
type Error = V_::Error;
#[inline]
@@ -1400,7 +1331,10 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
}
#[inline]
fn visit_seed<K, V>(&mut self, kseed: K, vseed: V) -> Result<Option<(K::Value, V::Value)>, Self::Error>
fn visit_seed<K, V>(&mut self,
kseed: K,
vseed: V)
-> Result<Option<(K::Value, V::Value)>, Self::Error>
where K: DeserializeSeed,
V: DeserializeSeed
{
@@ -1410,7 +1344,7 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
#[inline]
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
where K: Deserialize,
V: Deserialize,
V: Deserialize
{
(**self).visit()
}
@@ -1446,7 +1380,7 @@ pub trait EnumVisitor: Sized {
type Error: Error;
/// The `Visitor` that will be used to deserialize the content of the enum
/// variant.
type Variant: VariantVisitor<Error=Self::Error>;
type Variant: VariantVisitor<Error = Self::Error>;
/// `visit_variant` is called to identify which variant to deserialize.
///
@@ -1539,9 +1473,7 @@ pub trait VariantVisitor: Sized {
/// Err(Error::invalid_type(unexp, &"tuple variant"))
/// }
/// ```
fn visit_tuple<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
/// Called when deserializing a struct-like variant.
@@ -1564,7 +1496,8 @@ pub trait VariantVisitor: Sized {
/// ```
fn visit_struct<V>(self,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
visitor: V)
-> Result<V::Value, Self::Error>
where V: Visitor;
}
+3 -8
View File
@@ -3,14 +3,9 @@ use core::marker::PhantomData;
use de::{Deserialize, Deserializer, Error, Visitor};
#[cfg(any(feature = "std", feature = "collections"))]
pub use de::content::{
Content,
ContentRefDeserializer,
ContentDeserializer,
TaggedContentVisitor,
InternallyTaggedUnitVisitor,
UntaggedUnitVisitor,
};
pub use de::content::{Content, ContentRefDeserializer, ContentDeserializer, TaggedContentVisitor,
TagOrContentField, TagOrContentFieldVisitor, InternallyTaggedUnitVisitor,
UntaggedUnitVisitor};
/// If the missing field is of type `Option<T>` then treat is as `None`,
/// otherwise it is an error.
+160 -111
View File
@@ -1,31 +1,15 @@
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
#[cfg(feature = "std")]
use std::collections::{
BTreeMap,
BTreeSet,
HashMap,
HashSet,
btree_map,
btree_set,
hash_map,
hash_set,
};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, btree_map, btree_set, hash_map,
hash_set};
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use std::vec;
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::{
BTreeMap,
BTreeSet,
Vec,
String,
btree_map,
btree_set,
vec,
};
use collections::{BTreeMap, BTreeSet, Vec, String, btree_map, btree_set, vec};
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::borrow::Cow;
#[cfg(all(feature = "collections", not(feature = "std")))]
@@ -41,7 +25,7 @@ use std::error;
use error;
use core::fmt::{self, Display};
use core::iter::{self, Iterator};
use core::iter::{self, Iterator};
use core::marker::PhantomData;
use de::{self, Expected, SeqVisitor};
@@ -63,16 +47,12 @@ type ErrorImpl = ();
impl de::Error for Error {
#[cfg(any(feature = "std", feature = "collections"))]
fn custom<T: Display>(msg: T) -> Self {
Error {
err: msg.to_string().into_boxed_str(),
}
Error { err: msg.to_string().into_boxed_str() }
}
#[cfg(not(any(feature = "std", feature = "collections")))]
fn custom<T: Display>(_msg: T) -> Self {
Error {
err: (),
}
Error { err: () }
}
}
@@ -105,7 +85,7 @@ impl error::Error for Error {
/// This trait converts primitive types into a deserializer.
pub trait ValueDeserializer<E: de::Error = Error> {
/// The actual deserializer type.
type Deserializer: de::Deserializer<Error=E>;
type Deserializer: de::Deserializer<Error = E>;
/// Convert this value into a deserializer.
fn into_deserializer(self) -> Self::Deserializer;
@@ -114,14 +94,12 @@ pub trait ValueDeserializer<E: de::Error = Error> {
///////////////////////////////////////////////////////////////////////////////
impl<E> ValueDeserializer<E> for ()
where E: de::Error,
where E: de::Error
{
type Deserializer = UnitDeserializer<E>;
fn into_deserializer(self) -> UnitDeserializer<E> {
UnitDeserializer {
marker: PhantomData,
}
UnitDeserializer { marker: PhantomData }
}
}
@@ -142,13 +120,13 @@ impl<E> de::Deserializer for UnitDeserializer<E>
}
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_unit()
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_none()
}
@@ -205,13 +183,72 @@ primitive_deserializer!(i64, I64Deserializer, visit_i64);
primitive_deserializer!(isize, IsizeDeserializer, visit_i64 as i64);
primitive_deserializer!(u8, U8Deserializer, visit_u8);
primitive_deserializer!(u16, U16Deserializer, visit_u16);
primitive_deserializer!(u32, U32Deserializer, visit_u32);
primitive_deserializer!(u64, U64Deserializer, visit_u64);
primitive_deserializer!(usize, UsizeDeserializer, visit_u64 as u64);
primitive_deserializer!(f32, F32Deserializer, visit_f32);
primitive_deserializer!(f64, F64Deserializer, visit_f64);
primitive_deserializer!(char, CharDeserializer, visit_char);
/// A helper deserializer that deserializes a number.
pub struct U32Deserializer<E> {
value: u32,
marker: PhantomData<E>,
}
impl<E> ValueDeserializer<E> for u32
where E: de::Error
{
type Deserializer = U32Deserializer<E>;
fn into_deserializer(self) -> U32Deserializer<E> {
U32Deserializer {
value: self,
marker: PhantomData,
}
}
}
impl<E> de::Deserializer for U32Deserializer<E>
where E: de::Error
{
type Error = E;
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
seq seq_fixed_size bytes map unit_struct newtype_struct tuple_struct
struct struct_field tuple ignored_any byte_buf
}
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_u32(self.value)
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_enum(self)
}
}
impl<E> de::EnumVisitor for U32Deserializer<E>
where E: de::Error
{
type Error = E;
type Variant = private::UnitOnly<E>;
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
where T: de::DeserializeSeed
{
seed.deserialize(self).map(private::unit_only)
}
}
///////////////////////////////////////////////////////////////////////////////
/// A helper deserializer that deserializes a `&str`.
@@ -221,7 +258,7 @@ pub struct StrDeserializer<'a, E> {
}
impl<'a, E> ValueDeserializer<E> for &'a str
where E: de::Error,
where E: de::Error
{
type Deserializer = StrDeserializer<'a, E>;
@@ -234,21 +271,22 @@ impl<'a, E> ValueDeserializer<E> for &'a str
}
impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
where E: de::Error,
where E: de::Error
{
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_str(self.value)
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_enum(self)
}
@@ -261,13 +299,13 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
}
impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E>
where E: de::Error,
where E: de::Error
{
type Error = E;
type Variant = private::UnitOnly<E>;
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
seed.deserialize(self).map(private::unit_only)
}
@@ -284,7 +322,7 @@ pub struct StringDeserializer<E> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> ValueDeserializer<E> for String
where E: de::Error,
where E: de::Error
{
type Deserializer = StringDeserializer<E>;
@@ -298,21 +336,22 @@ impl<E> ValueDeserializer<E> for String
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> de::Deserializer for StringDeserializer<E>
where E: de::Error,
where E: de::Error
{
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_string(self.value)
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_enum(self)
}
@@ -326,13 +365,13 @@ impl<E> de::Deserializer for StringDeserializer<E>
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> de::EnumVisitor for StringDeserializer<E>
where E: de::Error,
where E: de::Error
{
type Error = E;
type Variant = private::UnitOnly<E>;
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
seed.deserialize(self).map(private::unit_only)
}
@@ -349,7 +388,7 @@ pub struct CowStrDeserializer<'a, E> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
where E: de::Error,
where E: de::Error
{
type Deserializer = CowStrDeserializer<'a, E>;
@@ -363,12 +402,12 @@ impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
where E: de::Error,
where E: de::Error
{
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
match self.value {
Cow::Borrowed(string) => visitor.visit_str(string),
@@ -377,10 +416,11 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
}
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
_name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
visitor.visit_enum(self)
}
@@ -394,13 +434,13 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E>
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> de::EnumVisitor for CowStrDeserializer<'a, E>
where E: de::Error,
where E: de::Error
{
type Error = E;
type Variant = private::UnitOnly<E>;
fn visit_variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
seed.deserialize(self).map(private::unit_only)
}
@@ -417,7 +457,7 @@ pub struct SeqDeserializer<I, E> {
impl<I, E> SeqDeserializer<I, E>
where I: Iterator,
E: de::Error,
E: de::Error
{
/// Construct a new `SeqDeserializer<I>`.
pub fn new(iter: I) -> Self {
@@ -446,14 +486,14 @@ impl<I, E> SeqDeserializer<I, E>
}
impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
where I: Iterator<Item=T>,
where I: Iterator<Item = T>,
T: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
let v = try!(visitor.visit_seq(&mut self));
try!(self.end());
@@ -468,9 +508,9 @@ impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
}
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
where I: Iterator<Item=T>,
where I: Iterator<Item = T>,
T: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
@@ -508,7 +548,7 @@ impl Expected for ExpectedInSeq {
#[cfg(any(feature = "std", feature = "collections"))]
impl<T, E> ValueDeserializer<E> for Vec<T>
where T: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Deserializer = SeqDeserializer<vec::IntoIter<T>, E>;
@@ -520,7 +560,7 @@ impl<T, E> ValueDeserializer<E> for Vec<T>
#[cfg(any(feature = "std", feature = "collections"))]
impl<T, E> ValueDeserializer<E> for BTreeSet<T>
where T: ValueDeserializer<E> + Eq + Ord,
E: de::Error,
E: de::Error
{
type Deserializer = SeqDeserializer<btree_set::IntoIter<T>, E>;
@@ -532,7 +572,7 @@ impl<T, E> ValueDeserializer<E> for BTreeSet<T>
#[cfg(feature = "std")]
impl<T, E> ValueDeserializer<E> for HashSet<T>
where T: ValueDeserializer<E> + Eq + Hash,
E: de::Error,
E: de::Error
{
type Deserializer = SeqDeserializer<hash_set::IntoIter<T>, E>;
@@ -551,20 +591,20 @@ pub struct SeqVisitorDeserializer<V_, E> {
impl<V_, E> SeqVisitorDeserializer<V_, E>
where V_: de::SeqVisitor<Error = E>,
E: de::Error,
E: de::Error
{
/// Construct a new `SeqVisitorDeserializer<V_, E>`.
pub fn new(visitor: V_) -> Self {
SeqVisitorDeserializer{
SeqVisitorDeserializer {
visitor: visitor,
marker: PhantomData
marker: PhantomData,
}
}
}
impl<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
where V_: de::SeqVisitor<Error = E>,
E: de::Error,
E: de::Error
{
type Error = E;
@@ -587,7 +627,7 @@ pub struct MapDeserializer<I, E>
I::Item: private::Pair,
<I::Item as private::Pair>::First: ValueDeserializer<E>,
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
iter: iter::Fuse<I>,
value: Option<<I::Item as private::Pair>::Second>,
@@ -600,7 +640,7 @@ impl<I, E> MapDeserializer<I, E>
I::Item: private::Pair,
<I::Item as private::Pair>::First: ValueDeserializer<E>,
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
/// Construct a new `MapDeserializer<I, K, V, E>`.
pub fn new(iter: I) -> Self {
@@ -628,7 +668,9 @@ impl<I, E> MapDeserializer<I, E>
}
}
fn next_pair(&mut self) -> Option<(<I::Item as private::Pair>::First, <I::Item as private::Pair>::Second)> {
fn next_pair
(&mut self)
-> Option<(<I::Item as private::Pair>::First, <I::Item as private::Pair>::Second)> {
match self.iter.next() {
Some(kv) => {
self.count += 1;
@@ -644,12 +686,12 @@ impl<I, E> de::Deserializer for MapDeserializer<I, E>
I::Item: private::Pair,
<I::Item as private::Pair>::First: ValueDeserializer<E>,
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
fn deserialize<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
where V_: de::Visitor
{
let value = try!(visitor.visit_map(&mut self));
try!(self.end());
@@ -657,15 +699,18 @@ impl<I, E> de::Deserializer for MapDeserializer<I, E>
}
fn deserialize_seq<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
where V_: de::Visitor
{
let value = try!(visitor.visit_seq(&mut self));
try!(self.end());
Ok(value)
}
fn deserialize_seq_fixed_size<V_>(self, _len: usize, visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
fn deserialize_seq_fixed_size<V_>(self,
_len: usize,
visitor: V_)
-> Result<V_::Value, Self::Error>
where V_: de::Visitor
{
self.deserialize_seq(visitor)
}
@@ -682,12 +727,12 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
I::Item: private::Pair,
<I::Item as private::Pair>::First: ValueDeserializer<E>,
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
match self.next_pair() {
Some((key, value)) => {
@@ -699,7 +744,7 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
}
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
let value = self.value.take();
// Panic because this indicates a bug in the program rather than an
@@ -708,7 +753,10 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
seed.deserialize(value.into_deserializer())
}
fn visit_seed<TK, TV>(&mut self, kseed: TK, vseed: TV) -> Result<Option<(TK::Value, TV::Value)>, Self::Error>
fn visit_seed<TK, TV>(&mut self,
kseed: TK,
vseed: TV)
-> Result<Option<(TK::Value, TV::Value)>, Self::Error>
where TK: de::DeserializeSeed,
TV: de::DeserializeSeed
{
@@ -718,7 +766,7 @@ impl<I, E> de::MapVisitor for MapDeserializer<I, E>
let value = try!(vseed.deserialize(value.into_deserializer()));
Ok(Some((key, value)))
}
None => Ok(None)
None => Ok(None),
}
}
@@ -732,12 +780,12 @@ impl<I, E> de::SeqVisitor for MapDeserializer<I, E>
I::Item: private::Pair,
<I::Item as private::Pair>::First: ValueDeserializer<E>,
<I::Item as private::Pair>::Second: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
match self.next_pair() {
Some((k, v)) => {
@@ -771,13 +819,13 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
}
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
self.deserialize_seq(visitor)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
let pair = try!(visitor.visit_seq(&mut pair_visitor));
@@ -792,7 +840,7 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
}
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
if len == 2 {
self.deserialize_seq(visitor)
@@ -809,12 +857,12 @@ struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>);
impl<A, B, E> de::SeqVisitor for PairVisitor<A, B, E>
where A: ValueDeserializer<E>,
B: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Error = E;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
if let Some(k) = self.0.take() {
seed.deserialize(k.into_deserializer()).map(Some)
@@ -855,7 +903,7 @@ impl Expected for ExpectedInMap {
impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
where K: ValueDeserializer<E> + Eq + Ord,
V: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Deserializer = MapDeserializer<btree_map::IntoIter<K, V>, E>;
@@ -868,7 +916,7 @@ impl<K, V, E> ValueDeserializer<E> for BTreeMap<K, V>
impl<K, V, E> ValueDeserializer<E> for HashMap<K, V>
where K: ValueDeserializer<E> + Eq + Hash,
V: ValueDeserializer<E>,
E: de::Error,
E: de::Error
{
type Deserializer = MapDeserializer<hash_map::IntoIter<K, V>, E>;
@@ -887,20 +935,20 @@ pub struct MapVisitorDeserializer<V_, E> {
impl<V_, E> MapVisitorDeserializer<V_, E>
where V_: de::MapVisitor<Error = E>,
E: de::Error,
E: de::Error
{
/// Construct a new `MapVisitorDeserializer<V_, E>`.
pub fn new(visitor: V_) -> Self {
MapVisitorDeserializer{
MapVisitorDeserializer {
visitor: visitor,
marker: PhantomData
marker: PhantomData,
}
}
}
impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
where V_: de::MapVisitor<Error = E>,
E: de::Error,
E: de::Error
{
type Error = E;
@@ -918,7 +966,7 @@ impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
///////////////////////////////////////////////////////////////////////////////
impl<'a, E> ValueDeserializer<E> for bytes::Bytes<'a>
where E: de::Error,
where E: de::Error
{
type Deserializer = BytesDeserializer<'a, E>;
@@ -942,7 +990,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_bytes(self.value)
}
@@ -958,7 +1006,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> ValueDeserializer<E> for bytes::ByteBuf
where E: de::Error,
where E: de::Error
{
type Deserializer = ByteBufDeserializer<E>;
@@ -979,12 +1027,12 @@ pub struct ByteBufDeserializer<E> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> de::Deserializer for ByteBufDeserializer<E>
where E: de::Error,
where E: de::Error
{
type Error = E;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
where V: de::Visitor
{
visitor.visit_byte_buf(self.value)
}
@@ -1020,14 +1068,12 @@ mod private {
}
fn visit_newtype_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
where T: de::DeserializeSeed,
where T: de::DeserializeSeed
{
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant"))
}
fn visit_tuple<V>(self,
_len: usize,
_visitor: V) -> Result<V::Value, Self::Error>
fn visit_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant"))
@@ -1035,7 +1081,8 @@ mod private {
fn visit_struct<V>(self,
_fields: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
_visitor: V)
-> Result<V::Value, Self::Error>
where V: de::Visitor
{
Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant"))
@@ -1053,6 +1100,8 @@ mod private {
impl<A, B> Pair for (A, B) {
type First = A;
type Second = B;
fn split(self) -> (A, B) { self }
fn split(self) -> (A, B) {
self
}
}
}
+3 -1
View File
@@ -11,5 +11,7 @@ pub trait Error: Debug + Display {
fn description(&self) -> &str;
/// The lower-level cause of this error, if any.
fn cause(&self) -> Option<&Error> { None }
fn cause(&self) -> Option<&Error> {
None
}
}
+2
View File
@@ -6,6 +6,8 @@ use std::borrow::Cow;
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::borrow::Cow;
pub use core::clone::Clone;
pub use core::convert::{From, Into};
pub use core::default::Default;
pub use core::fmt;
pub use core::marker::PhantomData;
+24 -12
View File
@@ -4,13 +4,13 @@ use std::io;
use std::iter::Peekable;
/// Iterator over a byte stream that tracks the current position's line and column.
pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
pub struct LineColIterator<Iter: Iterator<Item = io::Result<u8>>> {
iter: Iter,
line: usize,
col: usize,
}
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
impl<Iter: Iterator<Item = io::Result<u8>>> LineColIterator<Iter> {
/// Construct a new `LineColIterator<Iter>`.
pub fn new(iter: Iter) -> LineColIterator<Iter> {
LineColIterator {
@@ -21,27 +21,39 @@ impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
}
/// Report the current line inside the iterator.
pub fn line(&self) -> usize { self.line }
pub fn line(&self) -> usize {
self.line
}
/// Report the current column inside the iterator.
pub fn col(&self) -> usize { self.col }
pub fn col(&self) -> usize {
self.col
}
/// Gets a reference to the underlying iterator.
pub fn get_ref(&self) -> &Iter { &self.iter }
pub fn get_ref(&self) -> &Iter {
&self.iter
}
/// Gets a mutable reference to the underlying iterator.
pub fn get_mut(&mut self) -> &mut Iter { &mut self.iter }
pub fn get_mut(&mut self) -> &mut Iter {
&mut self.iter
}
/// Unwraps this `LineColIterator`, returning the underlying iterator.
pub fn into_inner(self) -> Iter { self.iter }
pub fn into_inner(self) -> Iter {
self.iter
}
}
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Peekable<Iter>> {
impl<Iter: Iterator<Item = io::Result<u8>>> LineColIterator<Peekable<Iter>> {
/// peeks at the next value
pub fn peek(&mut self) -> Option<&io::Result<u8>> { self.iter.peek() }
pub fn peek(&mut self) -> Option<&io::Result<u8>> {
self.iter.peek()
}
}
impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
impl<Iter: Iterator<Item = io::Result<u8>>> Iterator for LineColIterator<Iter> {
type Item = io::Result<u8>;
fn next(&mut self) -> Option<io::Result<u8>> {
match self.iter.next() {
@@ -50,11 +62,11 @@ impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
self.line += 1;
self.col = 0;
Some(Ok(b'\n'))
},
}
Some(Ok(c)) => {
self.col += 1;
Some(Ok(c))
},
}
Some(Err(e)) => Some(Err(e)),
}
}
+50 -2
View File
@@ -61,7 +61,7 @@
#![doc(html_root_url="https://docs.serde.rs")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "unstable", feature(inclusive_range, nonzero, specialization, zero_one))]
#![cfg_attr(feature = "unstable", feature(nonzero, specialization, zero_one, into_boxed_c_str))]
#![cfg_attr(feature = "alloc", feature(alloc))]
#![cfg_attr(feature = "collections", feature(collections))]
#![cfg_attr(feature = "cargo-clippy", allow(linkedlist, type_complexity, doc_markdown))]
@@ -79,7 +79,8 @@ extern crate core as actual_core;
#[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, cell, default, result, option};
usize, f32, f64, char, str, num, slice, iter, cell, default, result, option,
clone, convert};
#[cfg(feature = "unstable")]
pub use actual_core::nonzero;
}
@@ -105,3 +106,50 @@ mod utils;
// Generated code uses these to support no_std. Not public API.
#[doc(hidden)]
pub mod export;
// Re-export #[derive(Serialize, Deserialize)].
//
// This is a workaround for https://github.com/rust-lang/cargo/issues/1286.
// Without this re-export, crates that put Serde derives behind a cfg_attr would
// need to use some silly feature name that depends on both serde and
// serde_derive.
//
// [features]
// serde-impls = ["serde", "serde_derive"]
//
// [dependencies]
// serde = { version = "1.0", optional = true }
// serde_derive = { version = "1.0", optional = true }
//
// # Used like this:
// # #[cfg(feature = "serde-impls")]
// # #[macro_use]
// # extern crate serde_derive;
// #
// # #[cfg_attr(feature = "serde-impls", derive(Serialize, Deserialize))]
// # struct S { /* ... */ }
//
// The re-exported derives allow crates to use "serde" as the name of their
// Serde feature which is more intuitive.
//
// [dependencies]
// serde = { version = "1.0", optional = true, features = ["derive"] }
//
// # Used like this:
// # #[cfg(feature = "serde")]
// # #[macro_use]
// # extern crate serde;
// #
// # #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// # struct S { /* ... */ }
//
// The reason re-exporting is not enabled by default is that disabling it would
// be annoying for crates that provide handwritten impls or data formats. They
// would need to disable default features and then explicitly re-enable std.
#[cfg(feature = "serde_derive")]
#[allow(unused_imports)]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "serde_derive")]
#[doc(hidden)]
pub use serde_derive::*;
+33 -16
View File
@@ -1,24 +1,9 @@
#[cfg(feature = "std")]
#[doc(hidden)]
#[macro_export]
macro_rules! forward_to_deserialize_method {
($func:ident($($arg:ty),*)) => {
#[inline]
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::std::result::Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor
{
self.deserialize(visitor)
}
};
}
#[cfg(not(feature = "std"))]
#[doc(hidden)]
#[macro_export]
macro_rules! forward_to_deserialize_method {
($func:ident($($arg:ty),*)) => {
#[inline]
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::core::result::Result<__V::Value, Self::Error>
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> $crate::export::Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor
{
self.deserialize(visitor)
@@ -188,3 +173,35 @@ macro_rules! forward_to_deserialize {
$(forward_to_deserialize_helper!{$func})*
};
}
/// Seralize the `$value` that implements Display as a string,
/// when that string is statically known to never have more than
/// a constant `$MAX_LEN` bytes.
///
/// Panics if the Display impl tries to write more than `$MAX_LEN` bytes.
#[cfg(feature = "std")]
// Not exported
macro_rules! serialize_display_bounded_length {
($value: expr, $MAX_LEN: expr, $serializer: expr) => {
{
use std::io::Write;
let mut buffer: [u8; $MAX_LEN] = unsafe { ::std::mem::uninitialized() };
let remaining_len;
{
let mut remaining = &mut buffer[..];
write!(remaining, "{}", $value).unwrap();
remaining_len = remaining.len()
}
let written_len = buffer.len() - remaining_len;
let written = &buffer[..written_len];
// write! only provides std::fmt::Formatter to Display implementations,
// which has methods write_str and write_char but no method to write arbitrary bytes.
// Therefore, `written` is well-formed in UTF-8.
let written_str = unsafe {
::std::str::from_utf8_unchecked(written)
};
$serializer.serialize_str(written_str)
}
}
}
+607
View File
@@ -0,0 +1,607 @@
use core::marker::PhantomData;
#[cfg(all(not(feature = "std"), feature = "collections"))]
use collections::{String, Vec};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
#[cfg(feature = "collections")]
use collections::borrow::ToOwned;
use ser::{self, Serialize, Serializer};
pub struct SerializeTupleVariantAsMapValue<M> {
map: M,
name: &'static str,
fields: Vec<Content>,
}
impl<M> SerializeTupleVariantAsMapValue<M> {
pub fn new(map: M, name: &'static str, len: usize) -> Self {
SerializeTupleVariantAsMapValue {
map: map,
name: name,
fields: Vec::with_capacity(len),
}
}
}
impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M>
where M: ser::SerializeMap
{
type Ok = M::Ok;
type Error = M::Error;
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), M::Error> {
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push(value);
Ok(())
}
fn end(mut self) -> Result<M::Ok, M::Error> {
try!(self.map.serialize_value(&Content::TupleStruct(self.name, self.fields)));
self.map.end()
}
}
pub struct SerializeStructVariantAsMapValue<M> {
map: M,
name: &'static str,
fields: Vec<(&'static str, Content)>,
}
impl<M> SerializeStructVariantAsMapValue<M> {
pub fn new(map: M, name: &'static str, len: usize) -> Self {
SerializeStructVariantAsMapValue {
map: map,
name: name,
fields: Vec::with_capacity(len),
}
}
}
impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M>
where M: ser::SerializeMap
{
type Ok = M::Ok;
type Error = M::Error;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), M::Error> {
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push((key, value));
Ok(())
}
fn end(mut self) -> Result<M::Ok, M::Error> {
try!(self.map.serialize_value(&Content::Struct(self.name, self.fields)));
self.map.end()
}
}
#[derive(Debug)]
enum Content {
Bool(bool),
U8(u8),
U16(u16),
U32(u32),
U64(u64),
I8(i8),
I16(i16),
I32(i32),
I64(i64),
F32(f32),
F64(f64),
Char(char),
String(String),
Bytes(Vec<u8>),
None,
Some(Box<Content>),
Unit,
UnitStruct(&'static str),
UnitVariant(&'static str, usize, &'static str),
NewtypeStruct(&'static str, Box<Content>),
NewtypeVariant(&'static str, usize, &'static str, Box<Content>),
Seq(Vec<Content>),
SeqFixedSize(Vec<Content>),
Tuple(Vec<Content>),
TupleStruct(&'static str, Vec<Content>),
TupleVariant(&'static str, usize, &'static str, Vec<Content>),
Map(Vec<(Content, Content)>),
Struct(&'static str, Vec<(&'static str, Content)>),
StructVariant(&'static str, usize, &'static str, Vec<(&'static str, Content)>),
}
impl Serialize for Content {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
match *self {
Content::Bool(b) => serializer.serialize_bool(b),
Content::U8(u) => serializer.serialize_u8(u),
Content::U16(u) => serializer.serialize_u16(u),
Content::U32(u) => serializer.serialize_u32(u),
Content::U64(u) => serializer.serialize_u64(u),
Content::I8(i) => serializer.serialize_i8(i),
Content::I16(i) => serializer.serialize_i16(i),
Content::I32(i) => serializer.serialize_i32(i),
Content::I64(i) => serializer.serialize_i64(i),
Content::F32(f) => serializer.serialize_f32(f),
Content::F64(f) => serializer.serialize_f64(f),
Content::Char(c) => serializer.serialize_char(c),
Content::String(ref s) => serializer.serialize_str(s),
Content::Bytes(ref b) => serializer.serialize_bytes(b),
Content::None => serializer.serialize_none(),
Content::Some(ref c) => serializer.serialize_some(&**c),
Content::Unit => serializer.serialize_unit(),
Content::UnitStruct(n) => serializer.serialize_unit_struct(n),
Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v),
Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c),
Content::NewtypeVariant(n, i, v, ref c) => {
serializer.serialize_newtype_variant(n, i, v, &**c)
}
Content::Seq(ref elements) => elements.serialize(serializer),
Content::SeqFixedSize(ref elements) => {
use ser::SerializeSeq;
let mut seq = try!(serializer.serialize_seq_fixed_size(elements.len()));
for e in elements {
try!(seq.serialize_element(e));
}
seq.end()
}
Content::Tuple(ref elements) => {
use ser::SerializeTuple;
let mut tuple = try!(serializer.serialize_tuple(elements.len()));
for e in elements {
try!(tuple.serialize_element(e));
}
tuple.end()
}
Content::TupleStruct(n, ref fields) => {
use ser::SerializeTupleStruct;
let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
for f in fields {
try!(ts.serialize_field(f));
}
ts.end()
}
Content::TupleVariant(n, i, v, ref fields) => {
use ser::SerializeTupleVariant;
let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
for f in fields {
try!(tv.serialize_field(f));
}
tv.end()
}
Content::Map(ref entries) => {
use ser::SerializeMap;
let mut map = try!(serializer.serialize_map(Some(entries.len())));
for &(ref k, ref v) in entries {
try!(map.serialize_entry(k, v));
}
map.end()
}
Content::Struct(n, ref fields) => {
use ser::SerializeStruct;
let mut s = try!(serializer.serialize_struct(n, fields.len()));
for &(k, ref v) in fields {
try!(s.serialize_field(k, v));
}
s.end()
}
Content::StructVariant(n, i, v, ref fields) => {
use ser::SerializeStructVariant;
let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
for &(k, ref v) in fields {
try!(sv.serialize_field(k, v));
}
sv.end()
}
}
}
}
struct ContentSerializer<E> {
error: PhantomData<E>,
}
impl<E> ContentSerializer<E> {
fn new() -> Self {
ContentSerializer { error: PhantomData }
}
}
impl<E> Serializer for ContentSerializer<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
type SerializeSeq = SerializeSeq<E>;
type SerializeTuple = SerializeTuple<E>;
type SerializeTupleStruct = SerializeTupleStruct<E>;
type SerializeTupleVariant = SerializeTupleVariant<E>;
type SerializeMap = SerializeMap<E>;
type SerializeStruct = SerializeStruct<E>;
type SerializeStructVariant = SerializeStructVariant<E>;
fn serialize_bool(self, v: bool) -> Result<Content, E> {
Ok(Content::Bool(v))
}
fn serialize_i8(self, v: i8) -> Result<Content, E> {
Ok(Content::I8(v))
}
fn serialize_i16(self, v: i16) -> Result<Content, E> {
Ok(Content::I16(v))
}
fn serialize_i32(self, v: i32) -> Result<Content, E> {
Ok(Content::I32(v))
}
fn serialize_i64(self, v: i64) -> Result<Content, E> {
Ok(Content::I64(v))
}
fn serialize_u8(self, v: u8) -> Result<Content, E> {
Ok(Content::U8(v))
}
fn serialize_u16(self, v: u16) -> Result<Content, E> {
Ok(Content::U16(v))
}
fn serialize_u32(self, v: u32) -> Result<Content, E> {
Ok(Content::U32(v))
}
fn serialize_u64(self, v: u64) -> Result<Content, E> {
Ok(Content::U64(v))
}
fn serialize_f32(self, v: f32) -> Result<Content, E> {
Ok(Content::F32(v))
}
fn serialize_f64(self, v: f64) -> Result<Content, E> {
Ok(Content::F64(v))
}
fn serialize_char(self, v: char) -> Result<Content, E> {
Ok(Content::Char(v))
}
fn serialize_str(self, value: &str) -> Result<Content, E> {
Ok(Content::String(value.to_owned()))
}
fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> {
Ok(Content::Bytes(value.to_owned()))
}
fn serialize_none(self) -> Result<Content, E> {
Ok(Content::None)
}
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Content, E> {
Ok(Content::Some(Box::new(try!(value.serialize(self)))))
}
fn serialize_unit(self) -> Result<Content, E> {
Ok(Content::Unit)
}
fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> {
Ok(Content::UnitStruct(name))
}
fn serialize_unit_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str)
-> Result<Content, E> {
Ok(Content::UnitVariant(name, variant_index, variant))
}
fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
name: &'static str,
value: &T)
-> Result<Content, E> {
Ok(Content::NewtypeStruct(name, Box::new(try!(value.serialize(self)))))
}
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
value: &T)
-> Result<Content, E> {
Ok(Content::NewtypeVariant(name,
variant_index,
variant,
Box::new(try!(value.serialize(self)))))
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> {
Ok(SerializeSeq {
fixed_size: false,
elements: Vec::with_capacity(len.unwrap_or(0)),
error: PhantomData,
})
}
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, E> {
Ok(SerializeSeq {
fixed_size: true,
elements: Vec::with_capacity(size),
error: PhantomData,
})
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> {
Ok(SerializeTuple {
elements: Vec::with_capacity(len),
error: PhantomData,
})
}
fn serialize_tuple_struct(self,
name: &'static str,
len: usize)
-> Result<Self::SerializeTupleStruct, E> {
Ok(SerializeTupleStruct {
name: name,
fields: Vec::with_capacity(len),
error: PhantomData,
})
}
fn serialize_tuple_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize)
-> Result<Self::SerializeTupleVariant, E> {
Ok(SerializeTupleVariant {
name: name,
variant_index: variant_index,
variant: variant,
fields: Vec::with_capacity(len),
error: PhantomData,
})
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> {
Ok(SerializeMap {
entries: Vec::with_capacity(len.unwrap_or(0)),
key: None,
error: PhantomData,
})
}
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, E> {
Ok(SerializeStruct {
name: name,
fields: Vec::with_capacity(len),
error: PhantomData,
})
}
fn serialize_struct_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize)
-> Result<Self::SerializeStructVariant, E> {
Ok(SerializeStructVariant {
name: name,
variant_index: variant_index,
variant: variant,
fields: Vec::with_capacity(len),
error: PhantomData,
})
}
}
struct SerializeSeq<E> {
fixed_size: bool,
elements: Vec<Content>,
error: PhantomData<E>,
}
impl<E> ser::SerializeSeq for SerializeSeq<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.elements.push(value);
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(if self.fixed_size {
Content::SeqFixedSize(self.elements)
} else {
Content::Seq(self.elements)
})
}
}
struct SerializeTuple<E> {
elements: Vec<Content>,
error: PhantomData<E>,
}
impl<E> ser::SerializeTuple for SerializeTuple<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.elements.push(value);
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::Tuple(self.elements))
}
}
struct SerializeTupleStruct<E> {
name: &'static str,
fields: Vec<Content>,
error: PhantomData<E>,
}
impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push(value);
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::TupleStruct(self.name, self.fields))
}
}
struct SerializeTupleVariant<E> {
name: &'static str,
variant_index: usize,
variant: &'static str,
fields: Vec<Content>,
error: PhantomData<E>,
}
impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push(value);
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::TupleVariant(self.name, self.variant_index, self.variant, self.fields))
}
}
struct SerializeMap<E> {
entries: Vec<(Content, Content)>,
key: Option<Content>,
error: PhantomData<E>,
}
impl<E> ser::SerializeMap for SerializeMap<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<(), E> {
let key = try!(key.serialize(ContentSerializer::<E>::new()));
self.key = Some(key);
Ok(())
}
fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), E> {
let key = self.key.take().expect("serialize_value called before serialize_key");
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.entries.push((key, value));
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::Map(self.entries))
}
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
key: &K,
value: &V)
-> Result<(), E> {
let key = try!(key.serialize(ContentSerializer::<E>::new()));
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.entries.push((key, value));
Ok(())
}
}
struct SerializeStruct<E> {
name: &'static str,
fields: Vec<(&'static str, Content)>,
error: PhantomData<E>,
}
impl<E> ser::SerializeStruct for SerializeStruct<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push((key, value));
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::Struct(self.name, self.fields))
}
}
struct SerializeStructVariant<E> {
name: &'static str,
variant_index: usize,
variant: &'static str,
fields: Vec<(&'static str, Content)>,
error: PhantomData<E>,
}
impl<E> ser::SerializeStructVariant for SerializeStructVariant<E>
where E: ser::Error
{
type Ok = Content;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), E> {
let value = try!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push((key, value));
Ok(())
}
fn end(self) -> Result<Content, E> {
Ok(Content::StructVariant(self.name, self.variant_index, self.variant, self.fields))
}
}
+178 -91
View File
@@ -4,40 +4,24 @@ use std::borrow::Cow;
use collections::borrow::Cow;
#[cfg(feature = "std")]
use std::collections::{
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
HashMap,
HashSet,
VecDeque,
};
use std::collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, HashMap, HashSet, VecDeque};
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::{
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
VecDeque,
String,
Vec,
};
use collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque, String, Vec};
#[cfg(feature = "collections")]
use collections::borrow::ToOwned;
#[cfg(feature = "std")]
use core::hash::{Hash, BuildHasher};
#[cfg(feature = "unstable")]
use core::iter;
#[cfg(feature = "std")]
use std::net;
#[cfg(feature = "unstable")]
#[cfg(any(feature = "std", feature = "unstable"))]
use core::ops;
#[cfg(feature = "std")]
use std::path;
#[cfg(feature = "std")]
use std::ffi::{CString, CStr, OsString, OsStr};
#[cfg(feature = "std")]
use std::rc::Rc;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::rc::Rc;
@@ -52,17 +36,17 @@ use alloc::arc::Arc;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
use core::cell::{Cell, RefCell};
#[cfg(feature = "std")]
use std::sync::{Mutex, RwLock};
use core::marker::PhantomData;
#[cfg(feature = "unstable")]
use core::nonzero::{NonZero, Zeroable};
use super::{
Serialize,
SerializeSeq,
SerializeTuple,
Serializer,
};
use super::{Serialize, SerializeSeq, SerializeTuple, Serializer};
#[cfg(feature = "std")]
use super::Error;
@@ -101,7 +85,7 @@ impl_visit!(char, serialize_char);
impl Serialize for str {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
serializer.serialize_str(self)
}
@@ -111,7 +95,7 @@ impl Serialize for str {
impl Serialize for String {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(&self[..]).serialize(serializer)
}
@@ -119,12 +103,34 @@ impl Serialize for String {
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
impl Serialize for CStr {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.serialize_bytes(self.to_bytes())
}
}
#[cfg(feature = "std")]
impl Serialize for CString {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.serialize_bytes(self.to_bytes())
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T> Serialize for Option<T>
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
match *self {
Some(ref value) => serializer.serialize_some(value),
@@ -138,7 +144,7 @@ impl<T> Serialize for Option<T>
impl<T> Serialize for PhantomData<T> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
serializer.serialize_unit_struct("PhantomData")
}
@@ -211,7 +217,7 @@ macro_rules! serialize_seq {
}
impl<T> Serialize for [T]
where T: Serialize,
where T: Serialize
{
serialize_seq!();
}
@@ -225,7 +231,7 @@ impl<T> Serialize for BinaryHeap<T>
#[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for BTreeSet<T>
where T: Serialize + Ord,
where T: Serialize + Ord
{
serialize_seq!();
}
@@ -233,14 +239,14 @@ impl<T> Serialize for BTreeSet<T>
#[cfg(feature = "std")]
impl<T, H> Serialize for HashSet<T, H>
where T: Serialize + Eq + Hash,
H: BuildHasher,
H: BuildHasher
{
serialize_seq!();
}
#[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for LinkedList<T>
where T: Serialize,
where T: Serialize
{
serialize_seq!();
}
@@ -259,37 +265,18 @@ impl<T> Serialize for VecDeque<T>
serialize_seq!();
}
#[cfg(feature = "unstable")]
impl<A> Serialize for ops::Range<A>
where ops::Range<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
A: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
{
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
for e in self.clone() {
try!(seq.serialize_element(&e));
}
seq.end()
}
}
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "unstable")]
impl<A> Serialize for ops::RangeInclusive<A>
where ops::RangeInclusive<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,
A: Serialize,
{
#[inline]
#[cfg(feature = "std")]
impl<Idx: Serialize> Serialize for ops::Range<Idx> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
for e in self.clone() {
try!(seq.serialize_element(&e));
}
seq.end()
use super::SerializeStruct;
let mut state = try!(serializer.serialize_struct("Range", 2));
try!(state.serialize_field("start", &self.start));
try!(state.serialize_field("end", &self.end));
state.end()
}
}
@@ -298,7 +285,7 @@ impl<A> Serialize for ops::RangeInclusive<A>
impl Serialize for () {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
serializer.serialize_unit()
}
@@ -518,7 +505,7 @@ macro_rules! serialize_map {
#[cfg(any(feature = "std", feature = "collections"))]
impl<K, V> Serialize for BTreeMap<K, V>
where K: Serialize + Ord,
V: Serialize,
V: Serialize
{
serialize_map!();
}
@@ -527,26 +514,30 @@ impl<K, V> Serialize for BTreeMap<K, V>
impl<K, V, H> Serialize for HashMap<K, V, H>
where K: Serialize + Eq + Hash,
V: Serialize,
H: BuildHasher,
H: BuildHasher
{
serialize_map!();
}
///////////////////////////////////////////////////////////////////////////////
impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
impl<'a, T: ?Sized> Serialize for &'a T
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
}
impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
impl<'a, T: ?Sized> Serialize for &'a mut T
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
@@ -558,7 +549,7 @@ impl<T: ?Sized> Serialize for Box<T>
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
@@ -570,7 +561,7 @@ impl<T> Serialize for Rc<T>
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
@@ -582,7 +573,7 @@ impl<T> Serialize for Arc<T>
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
@@ -594,12 +585,64 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T>
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
(**self).serialize(serializer)
}
}
impl<T> Serialize for Cell<T>
where T: Serialize + Copy
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.get().serialize(serializer)
}
}
impl<T> Serialize for RefCell<T>
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.borrow().serialize(serializer)
}
}
#[cfg(feature = "std")]
impl<T> Serialize for Mutex<T>
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
match self.lock() {
Ok(locked) => locked.serialize(serializer),
Err(_) => Err(S::Error::custom("lock poison error while serializing")),
}
}
}
#[cfg(feature = "std")]
impl<T> Serialize for RwLock<T>
where T: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
match self.read() {
Ok(locked) => locked.serialize(serializer),
Err(_) => Err(S::Error::custom("lock poison error while serializing")),
}
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T, E> Serialize for Result<T, E>
@@ -610,9 +653,7 @@ impl<T, E> Serialize for Result<T, E>
where S: Serializer
{
match *self {
Result::Ok(ref value) => {
serializer.serialize_newtype_variant("Result", 0, "Ok", value)
}
Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value),
Result::Err(ref value) => {
serializer.serialize_newtype_variant("Result", 1, "Err", value)
}
@@ -625,7 +666,7 @@ impl<T, E> Serialize for Result<T, E>
#[cfg(feature = "std")]
impl Serialize for Duration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
use super::SerializeStruct;
let mut state = try!(serializer.serialize_struct("Duration", 2));
@@ -640,27 +681,34 @@ impl Serialize for Duration {
#[cfg(feature = "std")]
impl Serialize for net::IpAddr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.to_string().serialize(serializer)
match *self {
net::IpAddr::V4(ref a) => a.serialize(serializer),
net::IpAddr::V6(ref a) => a.serialize(serializer),
}
}
}
#[cfg(feature = "std")]
impl Serialize for net::Ipv4Addr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.to_string().serialize(serializer)
/// "101.102.103.104".len()
const MAX_LEN: usize = 15;
serialize_display_bounded_length!(self, MAX_LEN, serializer)
}
}
#[cfg(feature = "std")]
impl Serialize for net::Ipv6Addr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.to_string().serialize(serializer)
/// "1000:1002:1003:1004:1005:1006:1007:1008".len()
const MAX_LEN: usize = 39;
serialize_display_bounded_length!(self, MAX_LEN, serializer)
}
}
@@ -669,7 +717,7 @@ impl Serialize for net::Ipv6Addr {
#[cfg(feature = "std")]
impl Serialize for net::SocketAddr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
match *self {
net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
@@ -681,18 +729,22 @@ impl Serialize for net::SocketAddr {
#[cfg(feature = "std")]
impl Serialize for net::SocketAddrV4 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.to_string().serialize(serializer)
/// "101.102.103.104:65000".len()
const MAX_LEN: usize = 21;
serialize_display_bounded_length!(self, MAX_LEN, serializer)
}
}
#[cfg(feature = "std")]
impl Serialize for net::SocketAddrV6 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.to_string().serialize(serializer)
/// "[1000:1002:1003:1004:1005:1006:1007:1008]:65000".len()
const MAX_LEN: usize = 47;
serialize_display_bounded_length!(self, MAX_LEN, serializer)
}
}
@@ -701,7 +753,7 @@ impl Serialize for net::SocketAddrV6 {
#[cfg(feature = "std")]
impl Serialize for path::Path {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
match self.to_str() {
Some(s) => s.serialize(serializer),
@@ -713,12 +765,47 @@ impl Serialize for path::Path {
#[cfg(feature = "std")]
impl Serialize for path::PathBuf {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
where S: Serializer
{
self.as_path().serialize(serializer)
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
impl Serialize for OsStr {
#[cfg(unix)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
use std::os::unix::ffi::OsStrExt;
serializer.serialize_newtype_variant("OsString",
0,
"Unix",
self.as_bytes())
}
#[cfg(windows)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
use std::os::windows::ffi::OsStrExt;
let val = self.encode_wide().collect::<Vec<_>>();
serializer.serialize_newtype_variant("OsString",
1,
"Windows",
&val)
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
#[cfg(feature = "std")]
impl Serialize for OsString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
self.as_os_str().serialize(serializer)
}
}
#[cfg(feature = "unstable")]
impl<T> Serialize for NonZero<T>
where T: Serialize + Zeroable
+17 -38
View File
@@ -2,17 +2,8 @@
use core::marker::PhantomData;
use ser::{
self,
Serialize,
SerializeSeq,
SerializeTuple,
SerializeTupleStruct,
SerializeTupleVariant,
SerializeMap,
SerializeStruct,
SerializeStructVariant,
};
use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
SerializeTupleVariant, SerializeMap, SerializeStruct, SerializeStructVariant};
/// Helper type for implementing a `Serializer` that does not support
/// serializing one of the compound types.
@@ -50,14 +41,12 @@ pub struct Impossible<Ok, E> {
enum Void {}
impl<Ok, E> SerializeSeq for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_element<T: ?Sized + Serialize>(&mut self,
_value: &T)
-> Result<(), E> {
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
match self.void {}
}
@@ -67,14 +56,12 @@ impl<Ok, E> SerializeSeq for Impossible<Ok, E>
}
impl<Ok, E> SerializeTuple for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_element<T: ?Sized + Serialize>(&mut self,
_value: &T)
-> Result<(), E> {
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
match self.void {}
}
@@ -84,14 +71,12 @@ impl<Ok, E> SerializeTuple for Impossible<Ok, E>
}
impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
_value: &T)
-> Result<(), E> {
fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
match self.void {}
}
@@ -101,14 +86,12 @@ impl<Ok, E> SerializeTupleStruct for Impossible<Ok, E>
}
impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
_value: &T)
-> Result<(), E> {
fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
match self.void {}
}
@@ -118,20 +101,16 @@ impl<Ok, E> SerializeTupleVariant for Impossible<Ok, E>
}
impl<Ok, E> SerializeMap for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_key<T: ?Sized + Serialize>(&mut self,
_key: &T)
-> Result<(), E> {
fn serialize_key<T: ?Sized + Serialize>(&mut self, _key: &T) -> Result<(), E> {
match self.void {}
}
fn serialize_value<T: ?Sized + Serialize>(&mut self,
_value: &T)
-> Result<(), E> {
fn serialize_value<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<(), E> {
match self.void {}
}
@@ -141,13 +120,13 @@ impl<Ok, E> SerializeMap for Impossible<Ok, E>
}
impl<Ok, E> SerializeStruct for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
_key: &'static str,
_key: &'static str,
_value: &T)
-> Result<(), E> {
match self.void {}
@@ -159,13 +138,13 @@ impl<Ok, E> SerializeStruct for Impossible<Ok, E>
}
impl<Ok, E> SerializeStructVariant for Impossible<Ok, E>
where E: ser::Error,
where E: ser::Error
{
type Ok = Ok;
type Error = E;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
_key: &'static str,
_key: &'static str,
_value: &T)
-> Result<(), E> {
match self.void {}
+145 -93
View File
@@ -61,6 +61,10 @@
//! - Rc\<T\>
//! - Arc\<T\>
//! - Cow\<'a, T\>
//! - Cell\<T\>
//! - RefCell\<T\>
//! - Mutex\<T\>
//! - RwLock\<T\>
//! - **Collection types**:
//! - BTreeMap\<K, V\>
//! - BTreeSet\<T\>
@@ -71,12 +75,11 @@
//! - VecDeque\<T\>
//! - Vec\<T\>
//! - EnumSet\<T\> (unstable)
//! - Range\<T\> (unstable)
//! - RangeInclusive\<T\> (unstable)
//! - **Miscellaneous standard library types**:
//! - Duration
//! - Path
//! - PathBuf
//! - Range\<T\>
//! - NonZero\<T\> (unstable)
//! - **Net types**:
//! - IpAddr
@@ -98,7 +101,11 @@ use std::error;
#[cfg(not(feature = "std"))]
use error;
#[cfg(all(feature = "collections", not(feature = "std")))]
use collections::string::String;
use core::fmt::Display;
#[cfg(any(feature = "std", feature = "collections"))]
use core::fmt::Write;
use core::iter::IntoIterator;
mod impls;
@@ -107,6 +114,8 @@ mod impossible;
// Helpers used by generated code. Not public API.
#[doc(hidden)]
pub mod private;
#[cfg(any(feature = "std", feature = "collections"))]
mod content;
pub use self::impossible::Impossible;
@@ -174,8 +183,7 @@ pub trait Serialize {
/// for more information about how to implement this method.
///
/// [impl-serialize]: https://serde.rs/impl-serialize.html
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer;
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer;
}
///////////////////////////////////////////////////////////////////////////////
@@ -254,31 +262,31 @@ pub trait Serializer: Sized {
/// Type returned from `serialize_seq` and `serialize_seq_fixed_size` for
/// serializing the content of the sequence.
type SerializeSeq: SerializeSeq<Ok=Self::Ok, Error=Self::Error>;
type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_tuple` for serializing the content of the
/// tuple.
type SerializeTuple: SerializeTuple<Ok=Self::Ok, Error=Self::Error>;
type SerializeTuple: SerializeTuple<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_tuple_struct` for serializing the content
/// of the tuple struct.
type SerializeTupleStruct: SerializeTupleStruct<Ok=Self::Ok, Error=Self::Error>;
type SerializeTupleStruct: SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_tuple_variant` for serializing the content
/// of the tuple variant.
type SerializeTupleVariant: SerializeTupleVariant<Ok=Self::Ok, Error=Self::Error>;
type SerializeTupleVariant: SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_map` for serializing the content of the
/// map.
type SerializeMap: SerializeMap<Ok=Self::Ok, Error=Self::Error>;
type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_struct` for serializing the content of the
/// struct.
type SerializeStruct: SerializeStruct<Ok=Self::Ok, Error=Self::Error>;
type SerializeStruct: SerializeStruct<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from `serialize_struct_variant` for serializing the
/// content of the struct variant.
type SerializeStructVariant: SerializeStructVariant<Ok=Self::Ok, Error=Self::Error>;
type SerializeStructVariant: SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>;
/// Serialize a `bool` value.
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
@@ -371,10 +379,7 @@ pub trait Serializer: Sized {
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
/// Serialize a `Some(T)` value.
fn serialize_some<T: ?Sized + Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Self::Error>;
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error>;
/// Serialize a `()` value.
fn serialize_unit(self) -> Result<Self::Ok, Self::Error>;
@@ -382,10 +387,7 @@ pub trait Serializer: Sized {
/// Serialize a unit struct like `struct Unit` or `PhantomData<T>`.
///
/// A reasonable implementation would be to forward to `serialize_unit`.
fn serialize_unit_struct(
self,
name: &'static str,
) -> Result<Self::Ok, Self::Error>;
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error>;
/// Serialize a unit variant like `E::A` in `enum E { A, B }`.
///
@@ -401,12 +403,11 @@ pub trait Serializer: Sized {
/// E::B => serializer.serialize_unit_variant("E", 1, "B"),
/// }
/// ```
fn serialize_unit_variant(
self,
name: &'static str,
variant_index: usize,
variant: &'static str,
) -> Result<Self::Ok, Self::Error>;
fn serialize_unit_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str)
-> Result<Self::Ok, Self::Error>;
/// Serialize a newtype struct like `struct Millimeters(u8)`.
///
@@ -417,11 +418,10 @@ pub trait Serializer: Sized {
/// ```rust,ignore
/// serializer.serialize_newtype_struct("Millimeters", &self.0)
/// ```
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>;
fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
name: &'static str,
value: &T)
-> Result<Self::Ok, Self::Error>;
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
///
@@ -434,13 +434,12 @@ pub trait Serializer: Sized {
/// E::N(ref n) => serializer.serialize_newtype_variant("E", 0, "N", n),
/// }
/// ```
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
name: &'static str,
variant_index: usize,
variant: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>;
fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
value: &T)
-> Result<Self::Ok, Self::Error>;
/// Begin to serialize a dynamically sized sequence. This call must be
/// followed by zero or more calls to `serialize_element`, then a call to
@@ -457,10 +456,7 @@ pub trait Serializer: Sized {
/// }
/// seq.end()
/// ```
fn serialize_seq(
self,
len: Option<usize>,
) -> Result<Self::SerializeSeq, Self::Error>;
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>;
/// Begin to serialize a statically sized sequence whose length will be
/// known at deserialization time without looking at the serialized data.
@@ -474,25 +470,19 @@ pub trait Serializer: Sized {
/// }
/// seq.end()
/// ```
fn serialize_seq_fixed_size(
self,
size: usize,
) -> Result<Self::SerializeSeq, Self::Error>;
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, Self::Error>;
/// Begin to serialize a tuple. This call must be followed by zero or more
/// calls to `serialize_field`, then a call to `end`.
/// calls to `serialize_element`, then a call to `end`.
///
/// ```rust,ignore
/// let mut tup = serializer.serialize_tuple(3)?;
/// tup.serialize_field(&self.0)?;
/// tup.serialize_field(&self.1)?;
/// tup.serialize_field(&self.2)?;
/// tup.serialize_element(&self.0)?;
/// tup.serialize_element(&self.1)?;
/// tup.serialize_element(&self.2)?;
/// tup.end()
/// ```
fn serialize_tuple(
self,
len: usize,
) -> Result<Self::SerializeTuple, Self::Error>;
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error>;
/// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
/// call must be followed by zero or more calls to `serialize_field`, then a
@@ -508,11 +498,10 @@ pub trait Serializer: Sized {
/// ts.serialize_field(&self.2)?;
/// ts.end()
/// ```
fn serialize_tuple_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error>;
fn serialize_tuple_struct(self,
name: &'static str,
len: usize)
-> Result<Self::SerializeTupleStruct, Self::Error>;
/// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8)
/// }`. This call must be followed by zero or more calls to
@@ -532,13 +521,12 @@ pub trait Serializer: Sized {
/// }
/// }
/// ```
fn serialize_tuple_variant(
self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error>;
fn serialize_tuple_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error>;
/// Begin to serialize a map. This call must be followed by zero or more
/// calls to `serialize_key` and `serialize_value`, then a call to `end`.
@@ -554,10 +542,7 @@ pub trait Serializer: Sized {
/// }
/// map.end()
/// ```
fn serialize_map(
self,
len: Option<usize>,
) -> Result<Self::SerializeMap, Self::Error>;
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error>;
/// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`.
/// This call must be followed by zero or more calls to `serialize_field`,
@@ -573,11 +558,10 @@ pub trait Serializer: Sized {
/// struc.serialize_field("b", &self.b)?;
/// struc.end()
/// ```
fn serialize_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error>;
fn serialize_struct(self,
name: &'static str,
len: usize)
-> Result<Self::SerializeStruct, Self::Error>;
/// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8,
/// g: u8, b: u8 } }`. This call must be followed by zero or more calls to
@@ -598,13 +582,12 @@ pub trait Serializer: Sized {
/// }
/// }
/// ```
fn serialize_struct_variant(
self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error>;
fn serialize_struct_variant(self,
name: &'static str,
variant_index: usize,
variant: &'static str,
len: usize)
-> Result<Self::SerializeStructVariant, Self::Error>;
/// Collect an iterator as a sequence.
///
@@ -613,7 +596,7 @@ pub trait Serializer: Sized {
/// this method.
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
<I as IntoIterator>::Item: Serialize
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_seq(iter.len_hint()));
@@ -631,7 +614,7 @@ pub trait Serializer: Sized {
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where K: Serialize,
V: Serialize,
I: IntoIterator<Item = (K, V)>,
I: IntoIterator<Item = (K, V)>
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_map(iter.len_hint()));
@@ -640,6 +623,70 @@ pub trait Serializer: Sized {
}
serializer.end()
}
/// Serialize a string produced by an implementation of `Display`.
///
/// The default implementation builds a heap-allocated `String` and
/// delegates to `serialize_str`. Serializers are encouraged to provide a
/// more efficient implementation if possible.
///
/// ```rust
/// # use serde::{Serialize, Serializer};
/// # struct DateTime;
/// # impl DateTime {
/// # fn naive_local(&self) -> () { () }
/// # fn offset(&self) -> () { () }
/// # }
/// impl Serialize for DateTime {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// }
/// }
/// ```
#[cfg(any(feature = "std", feature = "collections"))]
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where T: Display
{
let mut string = String::new();
write!(string, "{}", value).unwrap();
self.serialize_str(&string)
}
/// Serialize a string produced by an implementation of `Display`.
///
/// The default implementation returns an error unconditionally when
/// compiled with `no_std`.
///
/// ```rust
/// # use serde::{Serialize, Serializer};
/// # struct DateTime;
/// # impl DateTime {
/// # fn naive_local(&self) -> () { () }
/// # fn offset(&self) -> () { () }
/// # }
/// impl Serialize for DateTime {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// }
/// }
/// ```
#[cfg(not(any(feature = "std", feature = "collections")))]
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where T: Display
{
// TODO https://github.com/serde-rs/serde/issues/805
// Remove this impl and force no_std formats to implement collect_str.
let _ = value;
Err(Error::custom("this no_std format does not support serializing strings with collect_str"))
}
}
/// Returned from `Serializer::serialize_seq` and
@@ -670,9 +717,9 @@ pub trait SerializeSeq {
///
/// ```rust,ignore
/// let mut tup = serializer.serialize_tuple(3)?;
/// tup.serialize_field(&self.0)?;
/// tup.serialize_field(&self.1)?;
/// tup.serialize_field(&self.2)?;
/// tup.serialize_element(&self.0)?;
/// tup.serialize_element(&self.1)?;
/// tup.serialize_element(&self.2)?;
/// tup.end()
/// ```
pub trait SerializeTuple {
@@ -773,11 +820,10 @@ pub trait SerializeMap {
/// `serialize_value`. This is appropriate for serializers that do not care
/// about performance or are not able to optimize `serialize_entry` any
/// better than this.
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(
&mut self,
key: &K,
value: &V,
) -> Result<(), Self::Error> {
fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
key: &K,
value: &V)
-> Result<(), Self::Error> {
try!(self.serialize_key(key));
self.serialize_value(value)
}
@@ -803,7 +849,10 @@ pub trait SerializeStruct {
type Error: Error;
/// Serialize a struct field.
fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Self::Error>;
/// Finish serializing a struct.
fn end(self) -> Result<Self::Ok, Self::Error>;
@@ -830,7 +879,10 @@ pub trait SerializeStructVariant {
type Error: Error;
/// Serialize a struct variant field.
fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>;
fn serialize_field<T: ?Sized + Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Self::Error>;
/// Finish serializing a struct variant.
fn end(self) -> Result<Self::Ok, Self::Error>;
+114 -34
View File
@@ -1,26 +1,28 @@
use core::fmt::{self, Display};
use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct};
use ser::{self, Serialize, Serializer, SerializeMap, SerializeStruct, Impossible};
#[cfg(any(feature = "std", feature = "collections"))]
use ser::content::{SerializeTupleVariantAsMapValue, SerializeStructVariantAsMapValue};
/// Not public API.
pub fn serialize_tagged_newtype<S, T>(
serializer: S,
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
value: T,
) -> Result<S::Ok, S::Error>
pub fn serialize_tagged_newtype<S, T>(serializer: S,
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
value: &T)
-> Result<S::Ok, S::Error>
where S: Serializer,
T: Serialize
{
value.serialize(TaggedSerializer {
type_ident: type_ident,
variant_ident: variant_ident,
tag: tag,
variant_name: variant_name,
delegate: serializer,
})
type_ident: type_ident,
variant_ident: variant_ident,
tag: tag,
variant_name: variant_name,
delegate: serializer,
})
}
struct TaggedSerializer<S> {
@@ -44,6 +46,7 @@ enum Unsupported {
Sequence,
Tuple,
TupleStruct,
#[cfg(not(any(feature = "std", feature = "collections")))]
Enum,
}
@@ -62,6 +65,7 @@ impl Display for Unsupported {
Unsupported::Sequence => formatter.write_str("a sequence"),
Unsupported::Tuple => formatter.write_str("a tuple"),
Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
#[cfg(not(any(feature = "std", feature = "collections")))]
Unsupported::Enum => formatter.write_str("an enum"),
}
}
@@ -77,7 +81,9 @@ impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter,
"cannot serialize tagged newtype variant {}::{} containing {}",
self.type_ident, self.variant_ident, self.ty)
self.type_ident,
self.variant_ident,
self.ty)
}
}
@@ -86,10 +92,10 @@ impl<S> TaggedSerializer<S>
{
fn bad_type(self, what: Unsupported) -> S::Error {
ser::Error::custom(Error {
type_ident: self.type_ident,
variant_ident: self.variant_ident,
ty: what,
})
type_ident: self.type_ident,
variant_ident: self.variant_ident,
ty: what,
})
}
}
@@ -99,13 +105,21 @@ impl<S> Serializer for TaggedSerializer<S>
type Ok = S::Ok;
type Error = S::Error;
type SerializeSeq = S::SerializeSeq;
type SerializeTuple = S::SerializeTuple;
type SerializeTupleStruct = S::SerializeTupleStruct;
type SerializeTupleVariant = S::SerializeTupleVariant;
type SerializeSeq = Impossible<S::Ok, S::Error>;
type SerializeTuple = Impossible<S::Ok, S::Error>;
type SerializeTupleStruct = Impossible<S::Ok, S::Error>;
type SerializeMap = S::SerializeMap;
type SerializeStruct = S::SerializeStruct;
type SerializeStructVariant = S::SerializeStructVariant;
#[cfg(not(any(feature = "std", feature = "collections")))]
type SerializeTupleVariant = Impossible<S::Ok, S::Error>;
#[cfg(any(feature = "std", feature = "collections"))]
type SerializeTupleVariant = SerializeTupleVariantAsMapValue<S::SerializeMap>;
#[cfg(not(any(feature = "std", feature = "collections")))]
type SerializeStructVariant = Impossible<S::Ok, S::Error>;
#[cfg(any(feature = "std", feature = "collections"))]
type SerializeStructVariant = SerializeStructVariantAsMapValue<S::SerializeMap>;
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
Err(self.bad_type(Unsupported::Boolean))
@@ -181,20 +195,38 @@ impl<S> Serializer for TaggedSerializer<S>
Err(self.bad_type(Unsupported::UnitStruct))
}
fn serialize_unit_variant(self, _: &'static str, _: usize, _: &'static str) -> Result<Self::Ok, Self::Error> {
Err(self.bad_type(Unsupported::Enum))
fn serialize_unit_variant(self,
_: &'static str,
_: usize,
inner_variant: &'static str)
-> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(inner_variant, &()));
map.end()
}
fn serialize_newtype_struct<T: ?Sized>(self, _: &'static str, value: &T) -> Result<Self::Ok, Self::Error>
fn serialize_newtype_struct<T: ?Sized>(self,
_: &'static str,
value: &T)
-> Result<Self::Ok, Self::Error>
where T: Serialize
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(self, _: &'static str, _: usize, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error>
fn serialize_newtype_variant<T: ?Sized>(self,
_: &'static str,
_: usize,
inner_variant: &'static str,
inner_value: &T)
-> Result<Self::Ok, Self::Error>
where T: Serialize
{
Err(self.bad_type(Unsupported::Enum))
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
@@ -209,27 +241,75 @@ impl<S> Serializer for TaggedSerializer<S>
Err(self.bad_type(Unsupported::Tuple))
}
fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeTupleStruct, Self::Error> {
fn serialize_tuple_struct(self,
_: &'static str,
_: usize)
-> Result<Self::SerializeTupleStruct, Self::Error> {
Err(self.bad_type(Unsupported::TupleStruct))
}
fn serialize_tuple_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
#[cfg(not(any(feature = "std", feature = "collections")))]
fn serialize_tuple_variant(self,
_: &'static str,
_: usize,
_: &'static str,
_: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> {
// Lack of push-based serialization means we need to buffer the content
// of the tuple variant, so it requires std.
Err(self.bad_type(Unsupported::Enum))
}
#[cfg(any(feature = "std", feature = "collections"))]
fn serialize_tuple_variant(self,
_: &'static str,
_: usize,
inner_variant: &'static str,
len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(map, inner_variant, len))
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1)));
try!(map.serialize_entry(self.tag, self.variant_name));
Ok(map)
}
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, Self::Error> {
fn serialize_struct(self,
name: &'static str,
len: usize)
-> Result<Self::SerializeStruct, Self::Error> {
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
try!(state.serialize_field(self.tag, self.variant_name));
Ok(state)
}
fn serialize_struct_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
#[cfg(not(any(feature = "std", feature = "collections")))]
fn serialize_struct_variant(self,
_: &'static str,
_: usize,
_: &'static str,
_: usize)
-> Result<Self::SerializeStructVariant, Self::Error> {
// Lack of push-based serialization means we need to buffer the content
// of the struct variant, so it requires std.
Err(self.bad_type(Unsupported::Enum))
}
#[cfg(any(feature = "std", feature = "collections"))]
fn serialize_struct_variant(self,
_: &'static str,
_: usize,
inner_variant: &'static str,
len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_key(inner_variant));
Ok(SerializeStructVariantAsMapValue::new(map, inner_variant, len))
}
}
+22 -20
View File
@@ -1,12 +1,12 @@
//! Private utility functions
const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO_B: u8 = 0b1100_0000;
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;
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 {
@@ -21,17 +21,20 @@ pub fn encode_utf8(c: char) -> EncodeUtf8 {
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[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[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
0
};
EncodeUtf8 { buf: buf, pos: pos }
EncodeUtf8 {
buf: buf,
pos: pos,
}
}
pub struct EncodeUtf8 {
@@ -47,23 +50,22 @@ impl EncodeUtf8 {
}
#[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}')
];
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 {
r.binary_search_by(|&(lo, hi)| if c < lo {
Greater
} else if hi < c {
Less
} else {
Equal
}
})
.is_ok()
})
.is_ok()
}
#[allow(non_snake_case)]
+4 -3
View File
@@ -1,6 +1,6 @@
[package]
name = "serde_codegen_internals"
version = "0.13.0"
version = "0.14.2"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "AST representation used by Serde codegen. Unstable."
@@ -8,10 +8,11 @@ homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://docs.serde.rs/serde_codegen_internals/"
keywords = ["serde", "serialization"]
include = ["Cargo.toml", "src/**/*.rs"]
readme = "../README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
[dependencies]
syn = "0.11"
syn = { version = "0.11", default-features = false, features = ["parsing"] }
[badges]
travis-ci = { repository = "serde-rs/serde" }
+1
View File
@@ -0,0 +1 @@
../LICENSE-APACHE
+1
View File
@@ -0,0 +1 @@
../LICENSE-MIT
+1
View File
@@ -0,0 +1 @@
../README.md
+24 -18
View File
@@ -38,16 +38,30 @@ impl<'a> Item<'a> {
pub fn from_ast(cx: &Ctxt, item: &'a syn::MacroInput) -> Item<'a> {
let attrs = attr::Item::from_ast(cx, item);
let body = match item.body {
syn::Body::Enum(ref variants) => {
Body::Enum(enum_from_ast(cx, variants))
}
let mut body = match item.body {
syn::Body::Enum(ref variants) => Body::Enum(enum_from_ast(cx, variants)),
syn::Body::Struct(ref variant_data) => {
let (style, fields) = struct_from_ast(cx, variant_data);
Body::Struct(style, fields)
}
};
match body {
Body::Enum(ref mut variants) => {
for ref mut variant in variants {
variant.attrs.rename_by_rule(attrs.rename_all());
for ref mut field in &mut variant.fields {
field.attrs.rename_by_rule(variant.attrs.rename_all());
}
}
}
Body::Struct(_, ref mut fields) => {
for field in fields {
field.attrs.rename_by_rule(attrs.rename_all());
}
}
}
Item {
ident: item.ident.clone(),
attrs: attrs,
@@ -58,15 +72,13 @@ impl<'a> Item<'a> {
}
impl<'a> Body<'a> {
pub fn all_fields(&'a self) -> Box<Iterator<Item=&'a Field<'a>> + 'a> {
pub fn all_fields(&'a self) -> Box<Iterator<Item = &'a Field<'a>> + 'a> {
match *self {
Body::Enum(ref variants) => {
Box::new(variants.iter()
.flat_map(|variant| variant.fields.iter()))
}
Body::Struct(_, ref fields) => {
Box::new(fields.iter())
.flat_map(|variant| variant.fields.iter()))
}
Body::Struct(_, ref fields) => Box::new(fields.iter()),
}
}
}
@@ -87,18 +99,12 @@ fn enum_from_ast<'a>(cx: &Ctxt, variants: &'a [syn::Variant]) -> Vec<Variant<'a>
fn struct_from_ast<'a>(cx: &Ctxt, data: &'a syn::VariantData) -> (Style, Vec<Field<'a>>) {
match *data {
syn::VariantData::Struct(ref fields) => {
(Style::Struct, fields_from_ast(cx, fields))
}
syn::VariantData::Struct(ref fields) => (Style::Struct, fields_from_ast(cx, fields)),
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
(Style::Newtype, fields_from_ast(cx, fields))
}
syn::VariantData::Tuple(ref fields) => {
(Style::Tuple, fields_from_ast(cx, fields))
}
syn::VariantData::Unit => {
(Style::Unit, Vec::new())
}
syn::VariantData::Tuple(ref fields) => (Style::Tuple, fields_from_ast(cx, fields)),
syn::VariantData::Unit => (Style::Unit, Vec::new()),
}
}
+262 -52
View File
@@ -2,6 +2,7 @@ use Ctxt;
use syn;
use syn::MetaItem::{List, NameValue, Word};
use syn::NestedMetaItem::{Literal, MetaItem};
use std::str::FromStr;
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
// are `attr::Item::from_ast`, `attr::Variant::from_ast`, and
@@ -11,6 +12,8 @@ use syn::NestedMetaItem::{Literal, MetaItem};
// user will see errors simultaneously for all bad attributes in the crate
// rather than just the first.
pub use case::RenameRule;
struct Attr<'c, T> {
cx: &'c Ctxt,
name: &'static str,
@@ -90,9 +93,13 @@ impl Name {
pub struct Item {
name: Name,
deny_unknown_fields: bool,
default: Default,
rename_all: RenameRule,
ser_bound: Option<Vec<syn::WherePredicate>>,
de_bound: Option<Vec<syn::WherePredicate>>,
tag: EnumTag,
from_type: Option<syn::Ty>,
into_type: Option<syn::Ty>,
}
/// Styles of representing an enum.
@@ -110,7 +117,14 @@ pub enum EnumTag {
/// ```json
/// {"type": "variant1", "key1": "value1", "key2": "value2"}
/// ```
Internal(String),
Internal { tag: String },
/// `#[serde(tag = "t", content = "c")]`
///
/// ```json
/// {"t": "variant1", "c": {"key1": "value1", "key2": "value2"}}
/// ```
Adjacent { tag: String, content: String },
/// `#[serde(untagged)]`
///
@@ -126,10 +140,15 @@ impl Item {
let mut ser_name = Attr::none(cx, "rename");
let mut de_name = Attr::none(cx, "rename");
let mut deny_unknown_fields = BoolAttr::none(cx, "deny_unknown_fields");
let mut default = Attr::none(cx, "default");
let mut rename_all = Attr::none(cx, "rename_all");
let mut ser_bound = Attr::none(cx, "bound");
let mut de_bound = Attr::none(cx, "bound");
let mut untagged = BoolAttr::none(cx, "untagged");
let mut internal_tag = Attr::none(cx, "tag");
let mut content = Attr::none(cx, "content");
let mut from_type = Attr::none(cx, "from");
let mut into_type = Attr::none(cx, "into");
for meta_items in item.attrs.iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
@@ -150,14 +169,57 @@ impl Item {
}
}
// Parse `#[serde(rename_all="foo")]`
MetaItem(NameValue(ref name, ref lit)) if name == "rename_all" => {
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
match RenameRule::from_str(&s) {
Ok(rename_rule) => rename_all.set(rename_rule),
Err(()) => {
cx.error(format!("unknown rename rule for #[serde(rename_all \
= {:?})]",
s))
}
}
}
}
// Parse `#[serde(deny_unknown_fields)]`
MetaItem(Word(ref name)) if name == "deny_unknown_fields" => {
deny_unknown_fields.set_true();
}
// Parse `#[serde(default)]`
MetaItem(Word(ref name)) if name == "default" => {
match item.body {
syn::Body::Struct(syn::VariantData::Struct(_)) => {
default.set(Default::Default);
}
_ => {
cx.error("#[serde(default)] can only be used on structs \
with named fields")
}
}
}
// Parse `#[serde(default="...")]`
MetaItem(NameValue(ref name, ref lit)) if name == "default" => {
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
match item.body {
syn::Body::Struct(syn::VariantData::Struct(_)) => {
default.set(Default::Path(path));
}
_ => {
cx.error("#[serde(default = \"...\")] can only be used \
on structs with named fields")
}
}
}
}
// Parse `#[serde(bound="D: Serialize")]`
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
if let Ok(where_predicates) =
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
ser_bound.set(where_predicates.clone());
de_bound.set(where_predicates);
}
@@ -197,6 +259,35 @@ impl Item {
}
}
// Parse `#[serde(content = "c")]`
MetaItem(NameValue(ref name, ref lit)) if name == "content" => {
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
match item.body {
syn::Body::Enum(_) => {
content.set(s);
}
syn::Body::Struct(_) => {
cx.error("#[serde(content = \"...\")] can only be used on \
enums")
}
}
}
}
// Parse `#[serde(from = "Type")]
MetaItem(NameValue(ref name, ref lit)) if name == "from" => {
if let Ok(from_ty) = parse_lit_into_ty(cx, name.as_ref(), lit) {
from_type.set_opt(Some(from_ty));
}
}
// Parse `#[serde(into = "Type")]
MetaItem(NameValue(ref name, ref lit)) if name == "into" => {
if let Ok(into_ty) = parse_lit_into_ty(cx, name.as_ref(), lit) {
into_type.set_opt(Some(into_ty));
}
}
MetaItem(ref meta_item) => {
cx.error(format!("unknown serde container attribute `{}`",
meta_item.name()));
@@ -209,30 +300,50 @@ impl Item {
}
}
let tag = match (untagged.get(), internal_tag.get()) {
(false, None) => EnumTag::External,
(true, None) => EnumTag::None,
(false, Some(tag)) => {
let tag = match (untagged.get(), internal_tag.get(), content.get()) {
(false, None, None) => EnumTag::External,
(true, None, None) => EnumTag::None,
(false, Some(tag), None) => {
// Check that there are no tuple variants.
if let syn::Body::Enum(ref variants) = item.body {
for variant in variants {
match variant.data {
syn::VariantData::Struct(_) | syn::VariantData::Unit => {}
syn::VariantData::Struct(_) |
syn::VariantData::Unit => {}
syn::VariantData::Tuple(ref fields) => {
if fields.len() != 1 {
cx.error("#[serde(tag = \"...\")] cannot be used with tuple variants");
cx.error("#[serde(tag = \"...\")] cannot be used with tuple \
variants");
break;
}
}
}
}
}
EnumTag::Internal(tag)
EnumTag::Internal { tag: tag }
}
(true, Some(_)) => {
(true, Some(_), None) => {
cx.error("enum cannot be both untagged and internally tagged");
EnumTag::External // doesn't matter, will error
}
(false, None, Some(_)) => {
cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together");
EnumTag::External
}
(true, None, Some(_)) => {
cx.error("untagged enum cannot have #[serde(content = \"...\")]");
EnumTag::External
}
(false, Some(tag), Some(content)) => {
EnumTag::Adjacent {
tag: tag,
content: content,
}
}
(true, Some(_), Some(_)) => {
cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]");
EnumTag::External
}
};
Item {
@@ -241,9 +352,13 @@ impl Item {
deserialize: de_name.get().unwrap_or_else(|| item.ident.to_string()),
},
deny_unknown_fields: deny_unknown_fields.get(),
default: default.get().unwrap_or(Default::None),
rename_all: rename_all.get().unwrap_or(RenameRule::None),
ser_bound: ser_bound.get(),
de_bound: de_bound.get(),
tag: tag,
from_type: from_type.get(),
into_type: into_type.get(),
}
}
@@ -251,10 +366,18 @@ impl Item {
&self.name
}
pub fn rename_all(&self) -> &RenameRule {
&self.rename_all
}
pub fn deny_unknown_fields(&self) -> bool {
self.deny_unknown_fields
}
pub fn default(&self) -> &Default {
&self.default
}
pub fn ser_bound(&self) -> Option<&[syn::WherePredicate]> {
self.ser_bound.as_ref().map(|vec| &vec[..])
}
@@ -266,12 +389,23 @@ impl Item {
pub fn tag(&self) -> &EnumTag {
&self.tag
}
pub fn from_type(&self) -> Option<&syn::Ty> {
self.from_type.as_ref()
}
pub fn into_type(&self) -> Option<&syn::Ty> {
self.into_type.as_ref()
}
}
/// Represents variant attribute information
#[derive(Debug)]
pub struct Variant {
name: Name,
ser_renamed: bool,
de_renamed: bool,
rename_all: RenameRule,
skip_deserializing: bool,
skip_serializing: bool,
}
@@ -282,6 +416,7 @@ impl Variant {
let mut de_name = Attr::none(cx, "rename");
let mut skip_deserializing = BoolAttr::none(cx, "skip_deserializing");
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
let mut rename_all = Attr::none(cx, "rename_all");
for meta_items in variant.attrs.iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
@@ -301,6 +436,21 @@ impl Variant {
de_name.set_opt(de);
}
}
// Parse `#[serde(rename_all="foo")]`
MetaItem(NameValue(ref name, ref lit)) if name == "rename_all" => {
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
match RenameRule::from_str(&s) {
Ok(rename_rule) => rename_all.set(rename_rule),
Err(()) => {
cx.error(format!("unknown rename rule for #[serde(rename_all \
= {:?})]",
s))
}
}
}
}
// Parse `#[serde(skip_deserializing)]`
MetaItem(Word(ref name)) if name == "skip_deserializing" => {
skip_deserializing.set_true();
@@ -311,8 +461,7 @@ impl Variant {
}
MetaItem(ref meta_item) => {
cx.error(format!("unknown serde variant attribute `{}`",
meta_item.name()));
cx.error(format!("unknown serde variant attribute `{}`", meta_item.name()));
}
Literal(_) => {
@@ -322,11 +471,18 @@ impl Variant {
}
}
let ser_name = ser_name.get();
let ser_renamed = ser_name.is_some();
let de_name = de_name.get();
let de_renamed = de_name.is_some();
Variant {
name: Name {
serialize: ser_name.get().unwrap_or_else(|| variant.ident.to_string()),
deserialize: de_name.get().unwrap_or_else(|| variant.ident.to_string()),
serialize: ser_name.unwrap_or_else(|| variant.ident.to_string()),
deserialize: de_name.unwrap_or_else(|| variant.ident.to_string()),
},
ser_renamed: ser_renamed,
de_renamed: de_renamed,
rename_all: rename_all.get().unwrap_or(RenameRule::None),
skip_deserializing: skip_deserializing.get(),
skip_serializing: skip_serializing.get(),
}
@@ -336,6 +492,19 @@ impl Variant {
&self.name
}
pub fn rename_by_rule(&mut self, rule: &RenameRule) {
if !self.ser_renamed {
self.name.serialize = rule.apply_to_variant(&self.name.serialize);
}
if !self.de_renamed {
self.name.deserialize = rule.apply_to_variant(&self.name.deserialize);
}
}
pub fn rename_all(&self) -> &RenameRule {
&self.rename_all
}
pub fn skip_deserializing(&self) -> bool {
self.skip_deserializing
}
@@ -349,10 +518,12 @@ impl Variant {
#[derive(Debug)]
pub struct Field {
name: Name,
ser_renamed: bool,
de_renamed: bool,
skip_serializing: bool,
skip_deserializing: bool,
skip_serializing_if: Option<syn::Path>,
default: FieldDefault,
default: Default,
serialize_with: Option<syn::Path>,
deserialize_with: Option<syn::Path>,
ser_bound: Option<Vec<syn::WherePredicate>>,
@@ -361,7 +532,7 @@ pub struct Field {
/// Represents the default to use for a field when deserializing.
#[derive(Debug, PartialEq)]
pub enum FieldDefault {
pub enum Default {
/// Field must always be specified because it does not have a default.
None,
/// The default is given by `std::default::Default::default()`.
@@ -372,9 +543,7 @@ pub enum FieldDefault {
impl Field {
/// Extract out the `#[serde(...)]` attributes from a struct field.
pub fn from_ast(cx: &Ctxt,
index: usize,
field: &syn::Field) -> Self {
pub fn from_ast(cx: &Ctxt, index: usize, field: &syn::Field) -> Self {
let mut ser_name = Attr::none(cx, "rename");
let mut de_name = Attr::none(cx, "rename");
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
@@ -412,13 +581,13 @@ impl Field {
// Parse `#[serde(default)]`
MetaItem(Word(ref name)) if name == "default" => {
default.set(FieldDefault::Default);
default.set(Default::Default);
}
// Parse `#[serde(default="...")]`
MetaItem(NameValue(ref name, ref lit)) if name == "default" => {
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
default.set(FieldDefault::Path(path));
default.set(Default::Path(path));
}
}
@@ -453,9 +622,22 @@ impl Field {
}
}
// Parse `#[serde(with="...")]`
MetaItem(NameValue(ref name, ref lit)) if name == "with" => {
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
let mut ser_path = path.clone();
ser_path.segments.push("serialize".into());
serialize_with.set(ser_path);
let mut de_path = path;
de_path.segments.push("deserialize".into());
deserialize_with.set(de_path);
}
}
// Parse `#[serde(bound="D: Serialize")]`
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
if let Ok(where_predicates) =
parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
ser_bound.set(where_predicates.clone());
de_bound.set(where_predicates);
}
@@ -470,8 +652,7 @@ impl Field {
}
MetaItem(ref meta_item) => {
cx.error(format!("unknown serde field attribute `{}`",
meta_item.name()));
cx.error(format!("unknown serde field attribute `{}`", meta_item.name()));
}
Literal(_) => {
@@ -484,18 +665,24 @@ impl Field {
// Is skip_deserializing, initialize the field to Default::default()
// unless a different default is specified by `#[serde(default="...")]`
if skip_deserializing.0.value.is_some() {
default.set_if_none(FieldDefault::Default);
default.set_if_none(Default::Default);
}
let ser_name = ser_name.get();
let ser_renamed = ser_name.is_some();
let de_name = de_name.get();
let de_renamed = de_name.is_some();
Field {
name: Name {
serialize: ser_name.get().unwrap_or_else(|| ident.clone()),
deserialize: de_name.get().unwrap_or(ident),
serialize: ser_name.unwrap_or_else(|| ident.clone()),
deserialize: de_name.unwrap_or(ident),
},
ser_renamed: ser_renamed,
de_renamed: de_renamed,
skip_serializing: skip_serializing.get(),
skip_deserializing: skip_deserializing.get(),
skip_serializing_if: skip_serializing_if.get(),
default: default.get().unwrap_or(FieldDefault::None),
default: default.get().unwrap_or(Default::None),
serialize_with: serialize_with.get(),
deserialize_with: deserialize_with.get(),
ser_bound: ser_bound.get(),
@@ -507,6 +694,15 @@ impl Field {
&self.name
}
pub fn rename_by_rule(&mut self, rule: &RenameRule) {
if !self.ser_renamed {
self.name.serialize = rule.apply_to_field(&self.name.serialize);
}
if !self.de_renamed {
self.name.deserialize = rule.apply_to_field(&self.name.deserialize);
}
}
pub fn skip_serializing(&self) -> bool {
self.skip_serializing
}
@@ -519,7 +715,7 @@ impl Field {
self.skip_serializing_if.as_ref()
}
pub fn default(&self) -> &FieldDefault {
pub fn default(&self) -> &Default {
&self.default
}
@@ -542,13 +738,12 @@ impl Field {
type SerAndDe<T> = (Option<T>, Option<T>);
fn get_ser_and_de<T, F>(
cx: &Ctxt,
attr_name: &'static str,
items: &[syn::NestedMetaItem],
f: F
) -> Result<SerAndDe<T>, ()>
where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result<T, ()>,
fn get_ser_and_de<T, F>(cx: &Ctxt,
attr_name: &'static str,
items: &[syn::NestedMetaItem],
f: F)
-> Result<SerAndDe<T>, ()>
where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result<T, ()>
{
let mut ser_item = Attr::none(cx, attr_name);
let mut de_item = Attr::none(cx, attr_name);
@@ -568,7 +763,8 @@ fn get_ser_and_de<T, F>(
}
_ => {
cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., deserialize = ...)`",
cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., \
deserialize = ...)`",
attr_name));
return Err(());
}
@@ -578,35 +774,34 @@ fn get_ser_and_de<T, F>(
Ok((ser_item.get(), de_item.get()))
}
fn get_renames(
cx: &Ctxt,
items: &[syn::NestedMetaItem],
) -> Result<SerAndDe<String>, ()> {
fn get_renames(cx: &Ctxt, items: &[syn::NestedMetaItem]) -> Result<SerAndDe<String>, ()> {
get_ser_and_de(cx, "rename", items, get_string_from_lit)
}
fn get_where_predicates(
cx: &Ctxt,
items: &[syn::NestedMetaItem],
) -> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {
fn get_where_predicates(cx: &Ctxt,
items: &[syn::NestedMetaItem])
-> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
}
pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMetaItem>> {
match attr.value {
List(ref name, ref items) if name == "serde" => {
Some(items.iter().cloned().collect())
}
_ => None
List(ref name, ref items) if name == "serde" => Some(items.iter().cloned().collect()),
_ => None,
}
}
fn get_string_from_lit(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<String, ()> {
fn get_string_from_lit(cx: &Ctxt,
attr_name: &str,
meta_item_name: &str,
lit: &syn::Lit)
-> Result<String, ()> {
if let syn::Lit::Str(ref s, _) = *lit {
Ok(s.clone())
} else {
cx.error(format!("expected serde {} attribute to be a string: `{} = \"...\"`",
attr_name, meta_item_name));
attr_name,
meta_item_name));
Err(())
}
}
@@ -616,7 +811,11 @@ fn parse_lit_into_path(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result<syn
syn::parse_path(&string).map_err(|err| cx.error(err))
}
fn parse_lit_into_where(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<Vec<syn::WherePredicate>, ()> {
fn parse_lit_into_where(cx: &Ctxt,
attr_name: &str,
meta_item_name: &str,
lit: &syn::Lit)
-> Result<Vec<syn::WherePredicate>, ()> {
let string = try!(get_string_from_lit(cx, attr_name, meta_item_name, lit));
if string.is_empty() {
return Ok(Vec::new());
@@ -626,3 +825,14 @@ fn parse_lit_into_where(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &
syn::parse_where_clause(&where_string).map(|wh| wh.predicates).map_err(|err| cx.error(err))
}
fn parse_lit_into_ty(cx: &Ctxt,
attr_name: &str,
lit: &syn::Lit)
-> Result<syn::Ty, ()> {
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
syn::parse_type(&string).map_err(|_| {
cx.error(format!("failed to parse type: {} = {:?}", attr_name, string))
})
}
+115
View File
@@ -0,0 +1,115 @@
use std::ascii::AsciiExt;
use std::str::FromStr;
use self::RenameRule::*;
#[derive(Debug, PartialEq)]
pub enum RenameRule {
/// Don't apply a default rename rule.
None,
/// Rename direct children to "PascalCase" style, as typically used for enum variants.
PascalCase,
/// Rename direct children to "camelCase" style.
CamelCase,
/// Rename direct children to "snake_case" style, as commonly used for fields.
SnakeCase,
/// Rename direct children to "SCREAMING_SNAKE_CASE" style, as commonly used for constants.
ScreamingSnakeCase,
/// Rename direct children to "kebab-case" style.
KebabCase,
}
impl RenameRule {
pub fn apply_to_variant(&self, variant: &str) -> String {
match *self {
None | PascalCase => variant.to_owned(),
CamelCase => variant[..1].to_ascii_lowercase() + &variant[1..],
SnakeCase => {
let mut snake = String::new();
for (i, ch) in variant.char_indices() {
if i > 0 && ch.is_uppercase() {
snake.push('_');
}
snake.push(ch.to_ascii_lowercase());
}
snake
}
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
}
}
pub fn apply_to_field(&self, field: &str) -> String {
match *self {
None | SnakeCase => field.to_owned(),
PascalCase => {
let mut pascal = String::new();
let mut capitalize = true;
for ch in field.chars() {
if ch == '_' {
capitalize = true;
} else if capitalize {
pascal.push(ch.to_ascii_uppercase());
capitalize = false;
} else {
pascal.push(ch);
}
}
pascal
}
CamelCase => {
let pascal = PascalCase.apply_to_field(field);
pascal[..1].to_ascii_lowercase() + &pascal[1..]
}
ScreamingSnakeCase => field.to_ascii_uppercase(),
KebabCase => field.replace('_', "-"),
}
}
}
impl FromStr for RenameRule {
type Err = ();
fn from_str(rename_all_str: &str) -> Result<Self, Self::Err> {
match rename_all_str {
"PascalCase" => Ok(PascalCase),
"camelCase" => Ok(CamelCase),
"snake_case" => Ok(SnakeCase),
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
"kebab-case" => Ok(KebabCase),
_ => Err(()),
}
}
}
#[test]
fn rename_variants() {
for &(original, camel, snake, screaming, kebab) in
&[("Outcome", "outcome", "outcome", "OUTCOME", "outcome"),
("VeryTasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty"),
("A", "a", "a", "A", "a"),
("Z42", "z42", "z42", "Z42", "z42")] {
assert_eq!(None.apply_to_variant(original), original);
assert_eq!(PascalCase.apply_to_variant(original), original);
assert_eq!(CamelCase.apply_to_variant(original), camel);
assert_eq!(SnakeCase.apply_to_variant(original), snake);
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
assert_eq!(KebabCase.apply_to_variant(original), kebab);
}
}
#[test]
fn rename_fields() {
for &(original, pascal, camel, screaming, kebab) in
&[("outcome", "Outcome", "outcome", "OUTCOME", "outcome"),
("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty"),
("a", "A", "a", "A", "a"),
("z42", "Z42", "z42", "Z42", "z42")] {
assert_eq!(None.apply_to_field(original), original);
assert_eq!(PascalCase.apply_to_field(original), pascal);
assert_eq!(CamelCase.apply_to_field(original), camel);
assert_eq!(SnakeCase.apply_to_field(original), original);
assert_eq!(ScreamingSnakeCase.apply_to_field(original), screaming);
assert_eq!(KebabCase.apply_to_field(original), kebab);
}
}
+1 -3
View File
@@ -8,9 +8,7 @@ pub struct Ctxt {
impl Ctxt {
pub fn new() -> Self {
Ctxt {
errors: RefCell::new(Some(Vec::new())),
}
Ctxt { errors: RefCell::new(Some(Vec::new())) }
}
pub fn error<T: Display>(&self, msg: T) {
+2
View File
@@ -5,3 +5,5 @@ pub mod attr;
mod ctxt;
pub use ctxt::Ctxt;
mod case;
+5 -4
View File
@@ -1,6 +1,6 @@
[package]
name = "serde_derive"
version = "0.9.7"
version = "0.9.14"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
@@ -8,7 +8,8 @@ homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://serde.rs/codegen.html"
keywords = ["serde", "serialization", "no_std"]
include = ["Cargo.toml", "src/**/*.rs"]
readme = "../README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
[features]
unstable = []
@@ -22,5 +23,5 @@ proc-macro = true
[dependencies]
quote = "0.3.8"
serde_codegen_internals = { version = "=0.13.0", default-features = false, path = "../serde_codegen_internals" }
syn = { version = "0.11", features = ["aster", "visit"] }
serde_codegen_internals = { version = "=0.14.2", default-features = false, path = "../serde_codegen_internals" }
syn = { version = "0.11", features = ["visit"] }
+1
View File
@@ -0,0 +1 @@
../LICENSE-APACHE
+1
View File
@@ -0,0 +1 @@
../LICENSE-MIT
+1
View File
@@ -0,0 +1 @@
../README.md
+154 -47
View File
@@ -1,46 +1,71 @@
use std::collections::HashSet;
use syn::{self, aster, visit};
use syn::{self, visit};
use internals::ast::Item;
use internals::attr;
macro_rules! path {
($first:ident $(:: $rest:ident)*) => {
syn::Path {
global: false,
segments: vec![
stringify!($first).into(),
$(
stringify!($rest).into(),
)*
],
}
};
(::$first:ident $(:: $rest:ident)*) => {
syn::Path {
global: true,
segments: vec![
stringify!($first).into(),
$(
stringify!($rest).into(),
)*
],
}
};
}
// 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: &syn::Generics) -> syn::Generics {
syn::Generics {
ty_params: generics.ty_params.iter().map(|ty_param| {
syn::TyParam {
default: None,
.. ty_param.clone()
}}).collect(),
.. generics.clone()
ty_params: generics.ty_params
.iter()
.map(|ty_param| syn::TyParam { default: None, ..ty_param.clone() })
.collect(),
..generics.clone()
}
}
pub fn with_where_predicates(
generics: &syn::Generics,
predicates: &[syn::WherePredicate],
) -> syn::Generics {
aster::from_generics(generics.clone())
.with_predicates(predicates.to_vec())
.build()
pub fn with_where_predicates(generics: &syn::Generics,
predicates: &[syn::WherePredicate])
-> syn::Generics {
let mut generics = generics.clone();
generics.where_clause.predicates.extend_from_slice(predicates);
generics
}
pub fn with_where_predicates_from_fields<F>(
item: &Item,
generics: &syn::Generics,
from_field: F,
) -> syn::Generics
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
pub fn with_where_predicates_from_fields<F>(item: &Item,
generics: &syn::Generics,
from_field: F)
-> syn::Generics
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>
{
aster::from_generics(generics.clone())
.with_predicates(
item.body.all_fields()
.flat_map(|field| from_field(&field.attrs))
.flat_map(|predicates| predicates.to_vec()))
.build()
let predicates = item.body
.all_fields()
.flat_map(|field| from_field(&field.attrs))
.flat_map(|predicates| predicates.to_vec());
let mut generics = generics.clone();
generics.where_clause.predicates.extend(predicates);
generics
}
// Puts the given bound on any generic type parameters that are used in fields
@@ -54,13 +79,12 @@ pub fn with_where_predicates_from_fields<F>(
// #[serde(skip_serializing)]
// c: C,
// }
pub fn with_bound<F>(
item: &Item,
generics: &syn::Generics,
filter: F,
bound: &syn::Path,
) -> syn::Generics
where F: Fn(&attr::Field) -> bool,
pub fn with_bound<F>(item: &Item,
generics: &syn::Generics,
filter: F,
bound: &syn::Path)
-> syn::Generics
where F: Fn(&attr::Field) -> bool
{
struct FindTyParams {
// Set of all generic type parameters on the current struct (A, B, C in
@@ -90,11 +114,13 @@ pub fn with_bound<F>(
}
}
let all_ty_params: HashSet<_> = generics.ty_params.iter()
let all_ty_params: HashSet<_> = generics.ty_params
.iter()
.map(|ty_param| ty_param.ident.clone())
.collect();
let relevant_tys = item.body.all_fields()
let relevant_tys = item.body
.all_fields()
.filter(|&field| filter(&field.attrs))
.map(|field| &field.ty);
@@ -106,16 +132,97 @@ pub fn with_bound<F>(
visit::walk_ty(&mut visitor, ty);
}
aster::from_generics(generics.clone())
.with_predicates(
generics.ty_params.iter()
.map(|ty_param| ty_param.ident.clone())
.filter(|id| visitor.relevant_ty_params.contains(id))
.map(|id| aster::where_predicate()
// the type parameter that is being bounded e.g. T
.bound().build(aster::ty().id(id))
// the bound e.g. Serialize
.bound().trait_(bound.clone()).build()
.build()))
.build()
let new_predicates = generics.ty_params
.iter()
.map(|ty_param| ty_param.ident.clone())
.filter(|id| visitor.relevant_ty_params.contains(id))
.map(|id| {
syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
bound_lifetimes: Vec::new(),
// the type parameter that is being bounded e.g. T
bounded_ty: syn::Ty::Path(None, id.into()),
// the bound e.g. Serialize
bounds: vec![syn::TyParamBound::Trait(
syn::PolyTraitRef {
bound_lifetimes: Vec::new(),
trait_ref: bound.clone(),
},
syn::TraitBoundModifier::None
)],
})
});
let mut generics = generics.clone();
generics.where_clause.predicates.extend(new_predicates);
generics
}
pub fn with_self_bound(item: &Item,
generics: &syn::Generics,
bound: &syn::Path)
-> syn::Generics
{
let mut generics = generics.clone();
generics.where_clause.predicates.push(
syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
bound_lifetimes: Vec::new(),
// the type that is being bounded e.g. MyStruct<'a, T>
bounded_ty: type_of_item(item),
// the bound e.g. Default
bounds: vec![syn::TyParamBound::Trait(
syn::PolyTraitRef {
bound_lifetimes: Vec::new(),
trait_ref: bound.clone(),
},
syn::TraitBoundModifier::None
)],
})
);
generics
}
pub fn with_lifetime_bound(generics: &syn::Generics,
lifetime: &str)
-> syn::Generics {
let mut generics = generics.clone();
for lifetime_def in &mut generics.lifetimes {
lifetime_def.bounds.push(syn::Lifetime::new(lifetime));
}
for ty_param in &mut generics.ty_params {
ty_param.bounds.push(syn::TyParamBound::Region(syn::Lifetime::new(lifetime)));
}
generics.lifetimes.push(syn::LifetimeDef {
attrs: Vec::new(),
lifetime: syn::Lifetime::new(lifetime),
bounds: Vec::new(),
});
generics
}
fn type_of_item(item: &Item) -> syn::Ty {
syn::Ty::Path(None, syn::Path {
global: false,
segments: vec![
syn::PathSegment {
ident: item.ident.clone(),
parameters: syn::PathParameters::AngleBracketed(syn::AngleBracketedParameterData {
lifetimes: item.generics
.lifetimes
.iter()
.map(|def| def.lifetime.clone())
.collect(),
types: item.generics
.ty_params
.iter()
.map(|param| syn::Ty::Path(None, param.ident.clone().into()))
.collect(),
bindings: Vec::new(),
}),
}
]
})
}
+698 -622
View File
File diff suppressed because it is too large Load Diff
+67
View File
@@ -0,0 +1,67 @@
use quote::{Tokens, ToTokens};
pub enum Fragment {
/// Tokens that can be used as an expression.
Expr(Tokens),
/// Tokens that can be used inside a block. The surrounding curly braces are
/// not part of these tokens.
Block(Tokens),
}
macro_rules! quote_expr {
($($tt:tt)*) => {
$crate::fragment::Fragment::Expr(quote!($($tt)*))
}
}
macro_rules! quote_block {
($($tt:tt)*) => {
$crate::fragment::Fragment::Block(quote!($($tt)*))
}
}
/// Interpolate a fragment in place of an expression. This involves surrounding
/// Block fragments in curly braces.
pub struct Expr(pub Fragment);
impl ToTokens for Expr {
fn to_tokens(&self, out: &mut Tokens) {
match self.0 {
Fragment::Expr(ref expr) => expr.to_tokens(out),
Fragment::Block(ref block) => {
out.append("{");
block.to_tokens(out);
out.append("}");
}
}
}
}
/// Interpolate a fragment as the statements of a block.
pub struct Stmts(pub Fragment);
impl ToTokens for Stmts {
fn to_tokens(&self, out: &mut Tokens) {
match self.0 {
Fragment::Expr(ref expr) => expr.to_tokens(out),
Fragment::Block(ref block) => block.to_tokens(out),
}
}
}
/// Interpolate a fragment as the value part of a `match` expression. This
/// involves putting a comma after expressions and curly braces around blocks.
pub struct Match(pub Fragment);
impl ToTokens for Match {
fn to_tokens(&self, out: &mut Tokens) {
match self.0 {
Fragment::Expr(ref expr) => {
expr.to_tokens(out);
out.append(",");
}
Fragment::Block(ref block) => {
out.append("{");
block.to_tokens(out);
out.append("}");
}
}
}
}
+5 -1
View File
@@ -13,9 +13,13 @@ extern crate serde_codegen_internals as internals;
extern crate proc_macro;
use proc_macro::TokenStream;
#[macro_use]
mod bound;
mod de;
#[macro_use]
mod fragment;
mod ser;
mod de;
#[proc_macro_derive(Serialize, attributes(serde))]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
+385 -404
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,15 +1,15 @@
[package]
name = "serde_test"
version = "0.9.7"
version = "0.9.14"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Token De/Serializer for testing De/Serialize implementations"
homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://docs.serde.rs/serde_test/"
readme = "../README.md"
keywords = ["serde", "serialization"]
include = ["Cargo.toml", "src/**/*.rs"]
readme = "../README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
[dependencies]
serde = { version = "0.9", path = "../serde" }
+1
View File
@@ -0,0 +1 @@
../LICENSE-APACHE
+1
View File
@@ -0,0 +1 @@
../LICENSE-MIT
+1
View File
@@ -0,0 +1 @@
../README.md
+10 -7
View File
@@ -7,24 +7,26 @@ use token::Token;
use std::fmt::Debug;
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
where T: Serialize + Deserialize + PartialEq + Debug,
where T: Serialize + Deserialize + PartialEq + Debug
{
assert_ser_tokens(value, tokens);
assert_de_tokens(value, tokens);
}
/// Asserts that `value` serializes to the given `tokens`.
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
where T: Serialize,
where T: Serialize
{
let mut ser = Serializer::new(tokens.iter());
assert_eq!(Serialize::serialize(value, &mut ser), Ok(()));
assert_eq!(ser.next_token(), None);
}
/// Expect an error serializing `T`.
/// Asserts that `value` serializes to the given `tokens`, and then yields `error`.
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
where T: Serialize + PartialEq + Debug,
where T: Serialize + PartialEq + Debug
{
let mut ser = Serializer::new(tokens.iter());
let v: Result<(), Error> = Serialize::serialize(value, &mut ser);
@@ -32,8 +34,9 @@ pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
assert_eq!(ser.next_token(), None);
}
/// Asserts that the given `tokens` deserialize into `value`.
pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
where T: Deserialize + PartialEq + Debug,
where T: Deserialize + PartialEq + Debug
{
let mut de = Deserializer::new(tokens.to_vec().into_iter());
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
@@ -41,9 +44,9 @@ pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
assert_eq!(de.next_token(), None);
}
/// Expect an error deserializing tokens into a `T`.
/// Asserts that the given `tokens` yield `error` when deserializing.
pub fn assert_de_tokens_error<T>(tokens: &[Token<'static>], error: Error)
where T: Deserialize + PartialEq + Debug,
where T: Deserialize + PartialEq + Debug
{
let mut de = Deserializer::new(tokens.to_vec().into_iter());
let v: Result<T, Error> = Deserialize::deserialize(&mut de);
+181 -97
View File
@@ -1,39 +1,33 @@
use std::iter;
use serde::de::{
self,
Deserialize,
DeserializeSeed,
EnumVisitor,
MapVisitor,
SeqVisitor,
VariantVisitor,
Visitor,
};
use serde::de::value::ValueDeserializer;
use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, MapVisitor, SeqVisitor,
VariantVisitor, Visitor};
use serde::de::value::{ValueDeserializer, MapVisitorDeserializer, SeqVisitorDeserializer};
use error::Error;
use token::Token;
/// A `Deserializer` that reads from a list of tokens.
pub struct Deserializer<I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
tokens: iter::Peekable<I>,
}
impl<I> Deserializer<I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
/// Creates the deserializer.
pub fn new(tokens: I) -> Deserializer<I> {
Deserializer {
tokens: tokens.peekable(),
}
Deserializer { tokens: tokens.peekable() }
}
/// Pulls the next token off of the deserializer, ignoring it.
pub fn next_token(&mut self) -> Option<Token<'static>> {
self.tokens.next()
}
/// Pulls the next token off of the deserializer and checks if it matches an expected token.
pub fn expect_token(&mut self, expected: Token) -> Result<(), Error> {
match self.tokens.next() {
Some(token) => {
@@ -47,8 +41,13 @@ impl<I> Deserializer<I>
}
}
fn visit_seq<V>(&mut self, len: Option<usize>, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn visit_seq<V>(&mut self,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor
{
let value = try!(visitor.visit_seq(DeserializerSeqVisitor {
de: self,
@@ -60,8 +59,13 @@ impl<I> Deserializer<I>
Ok(value)
}
fn visit_map<V>(&mut self, len: Option<usize>, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn visit_map<V>(&mut self,
len: Option<usize>,
sep: Token<'static>,
end: Token<'static>,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor
{
let value = try!(visitor.visit_map(DeserializerMapVisitor {
de: self,
@@ -75,7 +79,7 @@ impl<I> Deserializer<I>
}
impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
@@ -85,7 +89,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,
where V: Visitor
{
match self.tokens.next() {
Some(Token::Bool(v)) => visitor.visit_bool(v),
@@ -118,7 +122,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
self.visit_seq(Some(len), Token::TupleSep, Token::TupleEnd, visitor)
}
Some(Token::TupleStructStart(_, len)) => {
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
self.visit_seq(Some(len),
Token::TupleStructSep,
Token::TupleStructEnd,
visitor)
}
Some(Token::MapStart(len)) => {
self.visit_map(len, Token::MapSep, Token::MapEnd, visitor)
@@ -126,6 +133,13 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
Some(Token::StructStart(_, len)) => {
self.visit_map(Some(len), Token::StructSep, Token::StructEnd, visitor)
}
Some(Token::EnumUnit(_, variant)) => visitor.visit_str(variant),
Some(Token::EnumStart(variant)) |
Some(Token::EnumNewType(_, variant)) |
Some(Token::EnumSeqStart(_, variant, _)) |
Some(Token::EnumMapStart(_, variant, _)) => {
visitor.visit_map(EnumMapVisitor::new(self, variant))
}
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfTokens),
}
@@ -134,10 +148,11 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
/// Hook into `Option` deserializing so we can treat `Unit` as a
/// `None`, or a regular value as `Some(value)`.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor,
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::Unit) | Some(&Token::Option(false)) => {
Some(&Token::Unit) |
Some(&Token::Option(false)) => {
self.tokens.next();
visitor.visit_none()
}
@@ -151,26 +166,23 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
fn deserialize_enum<V>(self,
name: &str,
_variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
name: &str,
_variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::EnumStart(n)) if name == n => {
self.tokens.next();
visitor.visit_enum(DeserializerEnumVisitor {
de: self,
})
visitor.visit_enum(DeserializerEnumVisitor { de: self })
}
Some(&Token::EnumUnit(n, _))
| Some(&Token::EnumNewType(n, _))
| Some(&Token::EnumSeqStart(n, _, _))
| Some(&Token::EnumMapStart(n, _, _)) if name == n => {
visitor.visit_enum(DeserializerEnumVisitor {
de: self,
})
Some(&Token::EnumUnit(n, _)) |
Some(&Token::EnumNewType(n, _)) |
Some(&Token::EnumSeqStart(n, _, _)) |
Some(&Token::EnumMapStart(n, _, _)) if name == n => {
visitor.visit_enum(DeserializerEnumVisitor { de: self })
}
Some(_) => {
let token = self.tokens.next().unwrap();
@@ -181,7 +193,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
fn deserialize_unit_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
where V: Visitor,
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::UnitStruct(n)) => {
@@ -197,10 +209,8 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
}
fn deserialize_newtype_struct<V>(self,
name: &str,
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::StructNewType(n)) => {
@@ -216,10 +226,8 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
}
fn deserialize_seq_fixed_size<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::SeqArrayStart(_)) => {
@@ -231,13 +239,12 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
}
fn deserialize_tuple<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::Unit) | Some(&Token::UnitStruct(_)) => {
Some(&Token::Unit) |
Some(&Token::UnitStruct(_)) => {
self.tokens.next();
visitor.visit_unit()
}
@@ -255,7 +262,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
}
Some(&Token::TupleStructStart(_, _)) => {
self.tokens.next();
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
self.visit_seq(Some(len),
Token::TupleStructSep,
Token::TupleStructEnd,
visitor)
}
Some(_) => self.deserialize(visitor),
None => Err(Error::EndOfTokens),
@@ -265,8 +275,9 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
fn deserialize_tuple_struct<V>(self,
name: &str,
len: usize,
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::Unit) => {
@@ -296,7 +307,10 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
Some(&Token::TupleStructStart(n, _)) => {
self.tokens.next();
if name == n {
self.visit_seq(Some(len), Token::TupleStructSep, Token::TupleStructEnd, visitor)
self.visit_seq(Some(len),
Token::TupleStructSep,
Token::TupleStructEnd,
visitor)
} else {
Err(Error::InvalidName(n))
}
@@ -309,14 +323,18 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
fn deserialize_struct<V>(self,
name: &str,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
visitor: V)
-> Result<V::Value, Error>
where V: Visitor
{
match self.tokens.peek() {
Some(&Token::StructStart(n, _)) => {
self.tokens.next();
if name == n {
self.visit_map(Some(fields.len()), Token::StructSep, Token::StructEnd, visitor)
self.visit_map(Some(fields.len()),
Token::StructSep,
Token::StructEnd,
visitor)
} else {
Err(Error::InvalidName(n))
}
@@ -333,7 +351,9 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer<I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
struct DeserializerSeqVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
{
de: &'a mut Deserializer<I>,
len: Option<usize>,
sep: Token<'static>,
@@ -341,12 +361,12 @@ struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>>
}
impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
where T: DeserializeSeed,
where T: DeserializeSeed
{
if self.de.tokens.peek() == Some(&self.end) {
return Ok(None);
@@ -369,7 +389,9 @@ impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
struct DeserializerMapVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
{
de: &'a mut Deserializer<I>,
len: Option<usize>,
sep: Token<'static>,
@@ -377,12 +399,12 @@ struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>>
}
impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: DeserializeSeed,
where K: DeserializeSeed
{
if self.de.tokens.peek() == Some(&self.end) {
return Ok(None);
@@ -398,7 +420,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
}
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where V: DeserializeSeed,
where V: DeserializeSeed
{
seed.deserialize(&mut *self.de)
}
@@ -411,24 +433,26 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
//////////////////////////////////////////////////////////////////////////
struct DeserializerEnumVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
struct DeserializerEnumVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
{
de: &'a mut Deserializer<I>,
}
impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I>
where I: Iterator<Item=Token<'static>>,
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
type Variant = Self;
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
where V: DeserializeSeed,
where V: DeserializeSeed
{
match self.de.tokens.peek() {
Some(&Token::EnumUnit(_, v))
| Some(&Token::EnumNewType(_, v))
| Some(&Token::EnumSeqStart(_, v, _))
| Some(&Token::EnumMapStart(_, v, _)) => {
Some(&Token::EnumUnit(_, v)) |
Some(&Token::EnumNewType(_, v)) |
Some(&Token::EnumSeqStart(_, v, _)) |
Some(&Token::EnumMapStart(_, v, _)) => {
let de = v.into_deserializer();
let value = try!(seed.deserialize(de));
Ok((value, self))
@@ -443,7 +467,7 @@ impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I>
}
impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
where I: Iterator<Item=Token<'static>>
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
@@ -453,32 +477,26 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
self.de.tokens.next();
Ok(())
}
Some(_) => {
Deserialize::deserialize(self.de)
}
Some(_) => Deserialize::deserialize(self.de),
None => Err(Error::EndOfTokens),
}
}
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed,
where T: DeserializeSeed
{
match self.de.tokens.peek() {
Some(&Token::EnumNewType(_, _)) => {
self.de.tokens.next();
seed.deserialize(self.de)
}
Some(_) => {
seed.deserialize(self.de)
}
Some(_) => seed.deserialize(self.de),
None => Err(Error::EndOfTokens),
}
}
fn visit_tuple<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn visit_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where V: Visitor
{
match self.de.tokens.peek() {
Some(&Token::EnumSeqStart(_, _, enum_len)) => {
@@ -499,24 +517,23 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
Err(Error::UnexpectedToken(token))
}
}
Some(_) => {
de::Deserializer::deserialize(self.de, visitor)
}
Some(_) => de::Deserializer::deserialize(self.de, visitor),
None => Err(Error::EndOfTokens),
}
}
fn visit_struct<V>(self,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Error>
where V: Visitor,
fn visit_struct<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error>
where V: Visitor
{
match self.de.tokens.peek() {
Some(&Token::EnumMapStart(_, _, enum_len)) => {
let token = self.de.tokens.next().unwrap();
if fields.len() == enum_len {
self.de.visit_map(Some(fields.len()), Token::EnumMapSep, Token::EnumMapEnd, visitor)
self.de.visit_map(Some(fields.len()),
Token::EnumMapSep,
Token::EnumMapEnd,
visitor)
} else {
Err(Error::UnexpectedToken(token))
}
@@ -530,10 +547,77 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I>
Err(Error::UnexpectedToken(token))
}
}
Some(_) => {
de::Deserializer::deserialize(self.de, visitor)
}
Some(_) => de::Deserializer::deserialize(self.de, visitor),
None => Err(Error::EndOfTokens),
}
}
}
//////////////////////////////////////////////////////////////////////////
struct EnumMapVisitor<'a, I: 'a>
where I: Iterator<Item = Token<'static>>
{
de: &'a mut Deserializer<I>,
variant: Option<&'a str>,
}
impl<'a, I: 'a> EnumMapVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
{
fn new(de: &'a mut Deserializer<I>, variant: &'a str) -> Self {
EnumMapVisitor {
de: de,
variant: Some(variant),
}
}
}
impl<'a, I: 'a> MapVisitor for EnumMapVisitor<'a, I>
where I: Iterator<Item = Token<'static>>
{
type Error = Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: DeserializeSeed
{
match self.variant.take() {
Some(variant) => seed.deserialize(variant.into_deserializer()).map(Some),
None => Ok(None),
}
}
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where V: DeserializeSeed
{
match self.de.tokens.peek() {
Some(&Token::EnumSeqSep) => {
let value = {
let visitor = DeserializerSeqVisitor {
de: self.de,
len: None,
sep: Token::EnumSeqSep,
end: Token::EnumSeqEnd,
};
try!(seed.deserialize(SeqVisitorDeserializer::new(visitor)))
};
try!(self.de.expect_token(Token::EnumSeqEnd));
Ok(value)
}
Some(&Token::EnumMapSep) => {
let value = {
let visitor = DeserializerMapVisitor {
de: self.de,
len: None,
sep: Token::EnumMapSep,
end: Token::EnumMapEnd,
};
try!(seed.deserialize(MapVisitorDeserializer::new(visitor)))
};
try!(self.de.expect_token(Token::EnumMapEnd));
Ok(value)
}
_ => seed.deserialize(&mut *self.de),
}
}
}
+8
View File
@@ -5,11 +5,19 @@ use serde::{ser, de};
use token::Token;
/// Error returned by the test `Serializer` and `Deserializer`.
#[derive(Clone, PartialEq, Debug)]
pub enum Error {
/// A custom error.
Message(String),
/// `Deserialize` was expecting a struct of one name, and another was found.
InvalidName(&'static str),
/// `Serialize` generated a token that didn't match the test.
UnexpectedToken(Token<'static>),
/// The expected token list was too short.
EndOfTokens,
}
+2 -7
View File
@@ -2,13 +2,8 @@
extern crate serde;
mod assert;
pub use assert::{
assert_tokens,
assert_ser_tokens,
assert_ser_tokens_error,
assert_de_tokens,
assert_de_tokens_error,
};
pub use assert::{assert_tokens, assert_ser_tokens, assert_ser_tokens_error, assert_de_tokens,
assert_de_tokens_error};
mod ser;
pub use ser::Serializer;
+49 -29
View File
@@ -5,16 +5,18 @@ use serde::{ser, Serialize};
use error::Error;
use token::Token;
/// A `Serializer` that ensures that a value serializes to a given list of tokens.
pub struct Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
tokens: I,
phantom: PhantomData<&'a Token<'a>>,
}
impl<'a, I> Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
/// Creates the serializer.
pub fn new(tokens: I) -> Serializer<'a, I> {
Serializer {
tokens: tokens,
@@ -22,13 +24,14 @@ impl<'a, I> Serializer<'a, I>
}
}
/// Pulls the next token off of the serializer, ignoring it.
pub fn next_token(&mut self) -> Option<&'a Token<'a>> {
self.tokens.next()
}
}
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
@@ -124,15 +127,14 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
fn serialize_unit_variant(self,
name: &str,
_variant_index: usize,
variant: &str) -> Result<(), Error> {
variant: &str)
-> Result<(), Error> {
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
Ok(())
}
fn serialize_newtype_struct<T: ?Sized>(self,
name: &'static str,
value: &T) -> Result<(), Error>
where T: Serialize,
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name)));
value.serialize(self)
@@ -142,8 +144,9 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
name: &str,
_variant_index: usize,
variant: &str,
value: &T) -> Result<(), Error>
where T: Serialize,
value: &T)
-> Result<(), Error>
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
value.serialize(self)
@@ -155,7 +158,7 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
where T: Serialize,
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
value.serialize(self)
@@ -177,7 +180,8 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
}
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
assert_eq!(self.tokens.next(),
Some(&Token::TupleStructStart(name, len)));
Ok(self)
}
@@ -185,9 +189,10 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
name: &str,
_variant_index: usize,
variant: &str,
len: usize) -> Result<Self, Error>
{
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
len: usize)
-> Result<Self, Error> {
assert_eq!(self.tokens.next(),
Some(&Token::EnumSeqStart(name, variant, len)));
Ok(self)
}
@@ -205,15 +210,16 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
name: &str,
_variant_index: usize,
variant: &str,
len: usize) -> Result<Self, Error>
{
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
len: usize)
-> Result<Self, Error> {
assert_eq!(self.tokens.next(),
Some(&Token::EnumMapStart(name, variant, len)));
Ok(self)
}
}
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
@@ -232,7 +238,7 @@ impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
@@ -251,7 +257,7 @@ impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
@@ -270,7 +276,7 @@ impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
@@ -289,17 +295,21 @@ impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> where T: Serialize {
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
key.serialize(&mut **self)
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> where T: Serialize {
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where T: Serialize
{
value.serialize(&mut **self)
}
@@ -310,12 +320,17 @@ impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: Serialize {
fn serialize_field<T: ?Sized>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Self::Error>
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
try!(key.serialize(&mut **self));
value.serialize(&mut **self)
@@ -328,12 +343,17 @@ impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
}
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
where I: Iterator<Item=&'a Token<'a>>,
where I: Iterator<Item = &'a Token<'a>>
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: Serialize {
fn serialize_field<T: ?Sized>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Self::Error>
where T: Serialize
{
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
try!(key.serialize(&mut **self));
value.serialize(&mut **self)
+111
View File
@@ -1,59 +1,170 @@
#[derive(Clone, PartialEq, Debug)]
pub enum Token<'a> {
/// A serialized `bool`.
Bool(bool),
/// A serialized `i8`.
I8(i8),
/// A serialized `i16`.
I16(i16),
/// A serialized `i32`.
I32(i32),
/// A serialized `i64`.
I64(i64),
/// A serialized `u8`.
U8(u8),
/// A serialized `u16`.
U16(u16),
/// A serialized `u32`.
U32(u32),
/// A serialized `u64`.
U64(u64),
/// A serialized `f32`.
F32(f32),
/// A serialized `f64`.
F64(f64),
/// A serialized `char`.
Char(char),
/// A serialized `str`.
Str(&'a str),
/// A serialized `String`.
String(String),
/// A serialized `[u8]`
Bytes(&'a [u8]),
/// A serialized `ByteBuf`
ByteBuf(Vec<u8>),
/// The header to a serialized `Option<T>`.
///
/// `None` is serialized as `Option(false)`, while `Some` is serialized as `Option(true)`, then
/// the value contained in the option.
Option(bool),
/// A serialized `()`.
Unit,
/// A serialized unit struct of the given name.
UnitStruct(&'a str),
/// The header to a serialized newtype struct of the given name.
///
/// Newtype structs are serialized with this header, followed by the value contained in the
/// newtype struct.
StructNewType(&'a str),
/// The header to an enum of the given name.
///
/// This token is only used for deserializers, and ensures that the following tokens are read as
/// an enum. Because this is never emitted by serializers, calling `assert_ser_tokens` or
/// `assert_tokens` will fail if this token is used.
///
/// TODO: Trash this.
EnumStart(&'a str),
/// A unit variant of an enum of the given name, of the given name.
///
/// The first string represents the name of the enum, and the second represents the name of the
/// variant.
EnumUnit(&'a str, &'a str),
/// The header to a newtype variant of an enum of the given name, of the given name.
///
/// The first string represents the name of the enum, and the second represents the name of the
/// variant. The value contained within this enum works the same as `StructNewType`.
EnumNewType(&'a str, &'a str),
/// The header to a sequence of the given length.
///
/// These are serialized via `serialize_seq`, which takes an optional length. After this
/// header is a list of elements, followed by `SeqEnd`.
SeqStart(Option<usize>),
/// The header to an array of the given length.
///
/// These are serialized via `serialize_seq_fized_size`, which requires a length. After this
/// header is a list of elements, followed by `SeqEnd`.
SeqArrayStart(usize),
/// A separator, which occurs *before* every element in a sequence.
///
/// Elements in sequences are represented by a `SeqSep`, followed by the value of the element.
SeqSep,
/// An indicator of the end of a sequence.
SeqEnd,
/// The header to a tuple of the given length, similar to `SeqArrayStart`.
TupleStart(usize),
/// A separator, similar to `SeqSep`.
TupleSep,
/// An indicator of the end of a tuple, similar to `SeqEnd`.
TupleEnd,
/// The header to a tuple struct of the given name and length.
TupleStructStart(&'a str, usize),
/// A separator, similar to `TupleSep`.
TupleStructSep,
/// An indicator of the end of a tuple struct, similar to `TupleEnd`.
TupleStructEnd,
/// The header to a map of the given length.
///
/// These are serialized via `serialize_map`, which takes an optional length. After this header
/// is a list of key-value pairs, followed by `MapEnd`.
MapStart(Option<usize>),
/// A separator, which occurs *before* every key-value pair in a map.
///
/// Elements in maps are represented by a `MapSep`, followed by a serialized key, followed
/// by a serialized value.
MapSep,
/// An indicator of the end of a map.
MapEnd,
/// The header of a struct of the given name and length, similar to `MapStart`.
StructStart(&'a str, usize),
/// A separator, similar to `MapSep`.
StructSep,
/// An indicator of the end of a struct, similar to `MapEnd`.
StructEnd,
/// The header to a tuple variant of an enum of the given name, of the given name and length.
EnumSeqStart(&'a str, &'a str, usize),
/// A separator, similar to `TupleSep`.
EnumSeqSep,
/// An indicator of the end of a tuple variant, similar to `TupleEnd`.
EnumSeqEnd,
/// The header of a struct variant of an enum of the given name, of the given name and length,
/// similar to `StructStart`.
EnumMapStart(&'a str, &'a str, usize),
/// A separator, similar to `StructSep`.
EnumMapSep,
/// An indicator of the end of a struct, similar to `StructEnd`.
EnumMapEnd,
}
+1 -15
View File
@@ -2,20 +2,10 @@
name = "serde_test_suite"
version = "0.0.0"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://docs.serde.rs/serde/"
readme = "README.md"
keywords = ["serialization"]
publish = false
[features]
unstable-testing = [
"compiletest_rs",
"serde/unstable-testing",
]
unstable = ["serde/unstable", "compiletest_rs"]
[dev-dependencies]
fnv = "1.0"
@@ -26,7 +16,3 @@ serde_test = { path = "../serde_test" }
[dependencies]
compiletest_rs = { version = "0.2", optional = true }
[[test]]
name = "test"
path = "tests/test.rs"
+2 -1
View File
@@ -1,7 +1,8 @@
#![feature(lang_items, start, libc)]
#![feature(lang_items, start, libc, compiler_builtins_lib)]
#![no_std]
extern crate libc;
extern crate compiler_builtins;
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
#[serde(default)] //~^ HELP: #[serde(default)] can only be used on structs
enum E {
S { f: u8 },
}
@@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
#[serde(default)] //~^ HELP: #[serde(default)] can only be used on structs
struct T(u8, u8);
fn main() { }
@@ -0,0 +1,10 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(with = "w", serialize_with = "s")] //~^^ HELP: duplicate serde attribute `serialize_with`
x: (),
}
fn main() {}
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize)] //~ ERROR: proc-macro derive panicked
#[serde(from = "Option<T")] //~^ HELP: failed to parse type: from = "Option<T"
enum TestOne {
Testing,
One,
Two,
Three,
}
@@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(into = "Option<T")] //~^ HELP: failed to parse type: into = "Option<T"
enum TestOne {
Testing,
One,
Two,
Three,
}
+2
View File
@@ -1,3 +1,5 @@
#![cfg(feature = "unstable")]
extern crate compiletest_rs as compiletest;
use std::env;
-23
View File
@@ -1,23 +0,0 @@
#![cfg_attr(feature = "unstable-testing", feature(test, non_ascii_idents))]
#[cfg(feature = "unstable-testing")]
extern crate test;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_test;
#[macro_use]
mod macros;
mod test_annotations;
mod test_bytes;
mod test_de;
mod test_gen;
mod test_macros;
mod test_ser;
#[cfg(feature = "unstable-testing")]
mod compiletest;
+83
View File
@@ -1,3 +1,6 @@
#[macro_use]
extern crate serde_derive;
extern crate serde;
use self::serde::{Serialize, Serializer, Deserialize, Deserializer};
@@ -974,3 +977,83 @@ fn test_invalid_length_enum() {
Error::Message("invalid length 1, expected tuple of 2 elements".to_owned()),
);
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(into="EnumToU32", from="EnumToU32")]
struct StructFromEnum(Option<u32>);
impl Into<EnumToU32> for StructFromEnum {
fn into(self) -> EnumToU32 {
match self {
StructFromEnum(v) => v.into()
}
}
}
impl From<EnumToU32> for StructFromEnum {
fn from(v: EnumToU32) -> Self {
StructFromEnum(v.into())
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(into="Option<u32>", from="Option<u32>")]
enum EnumToU32 {
One,
Two,
Three,
Four,
Nothing
}
impl Into<Option<u32>> for EnumToU32 {
fn into(self) -> Option<u32> {
match self {
EnumToU32::One => Some(1),
EnumToU32::Two => Some(2),
EnumToU32::Three => Some(3),
EnumToU32::Four => Some(4),
EnumToU32::Nothing => None
}
}
}
impl From<Option<u32>> for EnumToU32 {
fn from(v: Option<u32>) -> Self {
match v {
Some(1) => EnumToU32::One,
Some(2) => EnumToU32::Two,
Some(3) => EnumToU32::Three,
Some(4) => EnumToU32::Four,
_ => EnumToU32::Nothing
}
}
}
#[test]
fn test_from_into_traits() {
assert_ser_tokens::<EnumToU32>(&EnumToU32::One,
&[Token::Option(true),
Token::U32(1)
]
);
assert_ser_tokens::<EnumToU32>(&EnumToU32::Nothing,
&[Token::Option(false)]
);
assert_de_tokens::<EnumToU32>(&EnumToU32::Two,
&[Token::Option(true),
Token::U32(2)
]
);
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(Some(5)),
&[Token::Option(false)]
);
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(None),
&[Token::Option(false)]
);
assert_de_tokens::<StructFromEnum>(&StructFromEnum(Some(2)),
&[Token::Option(true),
Token::U32(2)
]
);
}
+3
View File
@@ -1,4 +1,7 @@
extern crate serde;
use serde::bytes::{ByteBuf, Bytes};
extern crate serde_test;
use serde_test::{assert_tokens, assert_ser_tokens, assert_de_tokens, Token};
#[test]
+166 -1
View File
@@ -1,8 +1,19 @@
#![cfg_attr(feature = "unstable", feature(into_boxed_c_str))]
#[macro_use]
extern crate serde_derive;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::net;
use std::path::PathBuf;
use std::time::Duration;
use std::default::Default;
use std::ffi::{CString, OsString};
#[cfg(feature = "unstable")]
use std::ffi::CStr;
extern crate serde;
use serde::Deserialize;
extern crate fnv;
@@ -16,6 +27,9 @@ use self::serde_test::{
assert_de_tokens_error,
};
#[macro_use]
mod macros;
//////////////////////////////////////////////////////////////////////////
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
@@ -40,6 +54,22 @@ struct StructDenyUnknown {
b: i32,
}
#[derive(PartialEq, Debug, Deserialize)]
#[serde(default)]
struct StructDefault<T> {
a: i32,
b: T,
}
impl Default for StructDefault<String> {
fn default() -> Self {
StructDefault {
a: 100,
b: "default".to_string(),
}
}
}
#[derive(PartialEq, Debug, Deserialize)]
struct StructSkipAll {
#[serde(skip_deserializing)]
@@ -728,6 +758,23 @@ declare_tests! {
Token::StructEnd,
],
}
test_struct_default {
StructDefault { a: 50, b: "overwritten".to_string() } => &[
Token::StructStart("StructDefault", 1),
Token::StructSep,
Token::Str("a"),
Token::I32(50),
Token::StructSep,
Token::Str("b"),
Token::String("overwritten".to_string()),
Token::StructEnd,
],
StructDefault { a: 100, b: "default".to_string() } => &[
Token::StructStart("StructDefault", 0),
Token::StructEnd,
],
}
test_enum_unit {
Enum::Unit => &[
Token::EnumUnit("Enum", "Unit"),
@@ -821,6 +868,28 @@ declare_tests! {
Token::SeqEnd,
],
}
test_range {
1u32..2u32 => &[
Token::StructStart("Range", 2),
Token::StructSep,
Token::Str("start"),
Token::U32(1),
Token::StructSep,
Token::Str("end"),
Token::U32(2),
Token::StructEnd,
],
1u32..2u32 => &[
Token::SeqStart(Some(2)),
Token::SeqSep,
Token::U64(1),
Token::SeqSep,
Token::U64(2),
Token::SeqEnd,
],
}
test_net_ipv4addr {
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
}
@@ -837,17 +906,101 @@ declare_tests! {
Token::String("/usr/local/lib".to_owned()),
],
}
test_cstring {
CString::new("abc").unwrap() => &[
Token::Bytes(b"abc"),
],
}
}
#[cfg(unix)]
#[test]
fn test_osstring() {
use std::os::unix::ffi::OsStringExt;
let value = OsString::from_vec(vec![1, 2, 3]);
let tokens = [
Token::EnumStart("OsString"),
Token::Str("Unix"),
Token::SeqStart(Some(2)),
Token::SeqSep,
Token::U8(1),
Token::SeqSep,
Token::U8(2),
Token::SeqSep,
Token::U8(3),
Token::SeqEnd,
];
assert_de_tokens(&value, &tokens);
assert_de_tokens_ignore(&tokens);
}
#[cfg(windows)]
#[test]
fn test_osstring() {
use std::os::windows::ffi::OsStringExt;
let value = OsString::from_wide(&[1, 2, 3]);
let tokens = [
Token::EnumStart("OsString"),
Token::Str("Windows"),
Token::SeqStart(Some(2)),
Token::SeqSep,
Token::U16(1),
Token::SeqSep,
Token::U16(2),
Token::SeqSep,
Token::U16(3),
Token::SeqEnd,
];
assert_de_tokens(&value, &tokens);
assert_de_tokens_ignore(&tokens);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr() {
assert_de_tokens::<Box<CStr>>(&CString::new("abc").unwrap().into_boxed_c_str(),
&[Token::Bytes(b"abc")]);
}
#[cfg(feature = "unstable")]
#[test]
fn test_net_ipaddr() {
assert_de_tokens(
"1.2.3.4".parse::<net::IpAddr>().unwrap(),
&"1.2.3.4".parse::<net::IpAddr>().unwrap(),
&[Token::Str("1.2.3.4")],
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null() {
assert_de_tokens_error::<Box<CStr>>(
&[
Token::Bytes(b"a\0c"),
],
Error::Message("nul byte found in provided data at position: 1".into())
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null_end() {
assert_de_tokens_error::<Box<CStr>>(
&[
Token::Bytes(b"ac\0"),
],
Error::Message("nul byte found in provided data at position: 2".into())
);
}
declare_error_tests! {
test_unknown_field<StructDenyUnknown> {
&[
@@ -954,4 +1107,16 @@ declare_error_tests! {
],
Error::Message("invalid length 1, expected an array of length 3".into()),
}
test_cstring_internal_null<CString> {
&[
Token::Bytes(b"a\0c"),
],
Error::Message("nul byte found in provided data at position: 1".into()),
}
test_cstring_internal_null_end<CString> {
&[
Token::Bytes(b"ac\0"),
],
Error::Message("nul byte found in provided data at position: 2".into()),
}
}
+30 -13
View File
@@ -2,6 +2,11 @@
// successfully when there are a variety of generics and non-(de)serializable
// types involved.
#![cfg_attr(feature = "unstable", feature(non_ascii_idents))]
#[macro_use]
extern crate serde_derive;
extern crate serde;
use self::serde::ser::{Serialize, Serializer};
use self::serde::de::{Deserialize, Deserializer};
@@ -30,6 +35,14 @@ fn test_gen() {
}
assert::<With<i32>>();
#[derive(Serialize, Deserialize)]
struct WithTogether<T> {
t: T,
#[serde(with="both_x")]
x: X,
}
assert::<WithTogether<i32>>();
#[derive(Serialize, Deserialize)]
struct WithRef<'a, T: 'a> {
#[serde(skip_deserializing)]
@@ -207,8 +220,8 @@ fn test_gen() {
}
assert::<EmptyEnumVariant>();
#[cfg(feature = "unstable-testing")]
#[cfg_attr(feature = "unstable-testing", derive(Serialize, Deserialize))]
#[cfg(feature = "unstable")]
#[derive(Serialize, Deserialize)]
struct NonAsciiIdents {
σ: f64
}
@@ -233,12 +246,12 @@ fn test_gen() {
f: u8,
}
#[cfg(feature = "unstable-testing")]
#[cfg_attr(feature = "unstable-testing", derive(Serialize, Deserialize))]
#[cfg(feature = "unstable")]
#[derive(Serialize, Deserialize)]
struct EmptyTuple();
#[cfg(feature = "unstable-testing")]
#[cfg_attr(feature = "unstable-testing", derive(Serialize, Deserialize))]
#[cfg(feature = "unstable")]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct EmptyTupleDenyUnknown();
@@ -263,8 +276,8 @@ fn test_gen() {
Variant,
}
#[cfg(feature = "unstable-testing")]
#[cfg_attr(feature = "unstable-testing", derive(Serialize, Deserialize))]
#[cfg(feature = "unstable")]
#[derive(Serialize, Deserialize)]
enum EmptyVariants {
Braced {},
Tuple(),
@@ -275,8 +288,8 @@ fn test_gen() {
TupleSkip(#[serde(skip_deserializing)] u8),
}
#[cfg(feature = "unstable-testing")]
#[cfg_attr(feature = "unstable-testing", derive(Serialize, Deserialize))]
#[cfg(feature = "unstable")]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
enum EmptyVariantsDenyUnknown {
Braced {},
@@ -307,16 +320,20 @@ trait DeserializeWith: Sized {
}
// Implements neither Serialize nor Deserialize
struct X;
pub struct X;
fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
pub fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
unimplemented!()
}
fn de_x<D: Deserializer>(_: D) -> StdResult<X, D::Error> {
pub fn de_x<D: Deserializer>(_: D) -> StdResult<X, D::Error> {
unimplemented!()
}
mod both_x {
pub use super::{ser_x as serialize, de_x as deserialize};
}
impl SerializeWith for X {
fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error> {
unimplemented!()
+430
View File
@@ -1,3 +1,6 @@
#[macro_use]
extern crate serde_derive;
extern crate serde_test;
use self::serde_test::{
Error,
@@ -881,3 +884,430 @@ fn test_internally_tagged_enum() {
Error::Message("unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`, `F`".to_owned()),
);
}
#[test]
fn test_adjacently_tagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "t", content = "c")]
enum AdjacentlyTagged<T> {
Unit,
Newtype(T),
Tuple(u8, u8),
Struct { f: u8 },
}
// unit with no content
assert_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::StructStart("AdjacentlyTagged", 1),
Token::StructSep,
Token::Str("t"),
Token::Str("Unit"),
Token::StructEnd,
]
);
// unit with tag first
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::StructStart("AdjacentlyTagged", 1),
Token::StructSep,
Token::Str("t"),
Token::Str("Unit"),
Token::StructSep,
Token::Str("c"),
Token::Unit,
Token::StructEnd,
]
);
// unit with content first
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::StructStart("AdjacentlyTagged", 1),
Token::StructSep,
Token::Str("c"),
Token::Unit,
Token::StructSep,
Token::Str("t"),
Token::Str("Unit"),
Token::StructEnd,
]
);
// newtype with tag first
assert_tokens(
&AdjacentlyTagged::Newtype::<u8>(1),
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("t"),
Token::Str("Newtype"),
Token::StructSep,
Token::Str("c"),
Token::U8(1),
Token::StructEnd,
]
);
// newtype with content first
assert_de_tokens(
&AdjacentlyTagged::Newtype::<u8>(1),
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("c"),
Token::U8(1),
Token::StructSep,
Token::Str("t"),
Token::Str("Newtype"),
Token::StructEnd,
]
);
// tuple with tag first
assert_tokens(
&AdjacentlyTagged::Tuple::<u8>(1, 1),
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("t"),
Token::Str("Tuple"),
Token::StructSep,
Token::Str("c"),
Token::TupleStart(2),
Token::TupleSep,
Token::U8(1),
Token::TupleSep,
Token::U8(1),
Token::TupleEnd,
Token::StructEnd,
]
);
// tuple with content first
assert_de_tokens(
&AdjacentlyTagged::Tuple::<u8>(1, 1),
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("c"),
Token::TupleStart(2),
Token::TupleSep,
Token::U8(1),
Token::TupleSep,
Token::U8(1),
Token::TupleEnd,
Token::StructSep,
Token::Str("t"),
Token::Str("Tuple"),
Token::StructEnd,
]
);
// struct with tag first
assert_tokens(
&AdjacentlyTagged::Struct::<u8> { f: 1 },
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("t"),
Token::Str("Struct"),
Token::StructSep,
Token::Str("c"),
Token::StructStart("Struct", 1),
Token::StructSep,
Token::Str("f"),
Token::U8(1),
Token::StructEnd,
Token::StructEnd,
]
);
// struct with content first
assert_de_tokens(
&AdjacentlyTagged::Struct::<u8> { f: 1 },
&[
Token::StructStart("AdjacentlyTagged", 2),
Token::StructSep,
Token::Str("c"),
Token::StructStart("Struct", 1),
Token::StructSep,
Token::Str("f"),
Token::U8(1),
Token::StructEnd,
Token::StructSep,
Token::Str("t"),
Token::Str("Struct"),
Token::StructEnd,
]
);
}
#[test]
fn test_enum_in_internally_tagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
enum Outer {
Inner(Inner),
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Inner {
Unit,
Newtype(u8),
Tuple(u8, u8),
Struct { f: u8 },
}
assert_tokens(
&Outer::Inner(Inner::Unit),
&[
Token::MapStart(Some(2)),
Token::MapSep,
Token::Str("type"),
Token::Str("Inner"),
Token::MapSep,
Token::Str("Unit"),
Token::Unit,
Token::MapEnd,
]
);
assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::MapStart(Some(2)),
Token::MapSep,
Token::Str("type"),
Token::Str("Inner"),
Token::MapSep,
Token::Str("Newtype"),
Token::U8(1),
Token::MapEnd,
]
);
assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
Token::MapStart(Some(2)),
Token::MapSep,
Token::Str("type"),
Token::Str("Inner"),
Token::MapSep,
Token::Str("Tuple"),
Token::TupleStructStart("Tuple", 2),
Token::TupleStructSep,
Token::U8(1),
Token::TupleStructSep,
Token::U8(1),
Token::TupleStructEnd,
Token::MapEnd,
]
);
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::MapStart(Some(2)),
Token::MapSep,
Token::Str("type"),
Token::Str("Inner"),
Token::MapSep,
Token::Str("Struct"),
Token::StructStart("Struct", 1),
Token::StructSep,
Token::Str("f"),
Token::U8(1),
Token::StructEnd,
Token::MapEnd,
]
);
}
#[test]
fn test_enum_in_untagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
enum Outer {
Inner(Inner),
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Inner {
Unit,
Newtype(u8),
Tuple(u8, u8),
Struct { f: u8 },
}
assert_tokens(
&Outer::Inner(Inner::Unit),
&[
Token::EnumUnit("Inner", "Unit"),
]
);
assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::EnumNewType("Inner", "Newtype"),
Token::U8(1),
]
);
assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
Token::EnumSeqStart("Inner", "Tuple", 2),
Token::EnumSeqSep,
Token::U8(1),
Token::EnumSeqSep,
Token::U8(1),
Token::EnumSeqEnd,
]
);
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::EnumMapStart("Inner", "Struct", 1),
Token::EnumMapSep,
Token::Str("f"),
Token::U8(1),
Token::EnumMapEnd,
]
);
}
#[test]
fn test_rename_all() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
enum E {
#[serde(rename_all = "camelCase")]
Serialize {
serialize: bool,
serialize_seq: bool,
},
#[serde(rename_all = "kebab-case")]
SerializeSeq {
serialize: bool,
serialize_seq: bool,
},
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
SerializeMap {
serialize: bool,
serialize_seq: bool,
},
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "PascalCase")]
struct S {
serialize: bool,
serialize_seq: bool,
}
assert_tokens(
&E::Serialize { serialize: true, serialize_seq: true },
&[
Token::EnumMapStart("E", "serialize", 2),
Token::EnumMapSep,
Token::Str("serialize"),
Token::Bool(true),
Token::EnumMapSep,
Token::Str("serializeSeq"),
Token::Bool(true),
Token::EnumMapEnd,
]
);
assert_tokens(
&E::SerializeSeq { serialize: true, serialize_seq: true },
&[
Token::EnumMapStart("E", "serialize_seq", 2),
Token::EnumMapSep,
Token::Str("serialize"),
Token::Bool(true),
Token::EnumMapSep,
Token::Str("serialize-seq"),
Token::Bool(true),
Token::EnumMapEnd,
]
);
assert_tokens(
&E::SerializeMap { serialize: true, serialize_seq: true },
&[
Token::EnumMapStart("E", "serialize_map", 2),
Token::EnumMapSep,
Token::Str("SERIALIZE"),
Token::Bool(true),
Token::EnumMapSep,
Token::Str("SERIALIZE_SEQ"),
Token::Bool(true),
Token::EnumMapEnd,
]
);
assert_tokens(
&S { serialize: true, serialize_seq: true },
&[
Token::StructStart("S", 2),
Token::StructSep,
Token::Str("Serialize"),
Token::Bool(true),
Token::StructSep,
Token::Str("SerializeSeq"),
Token::Bool(true),
Token::StructEnd,
]
);
}
+33 -46
View File
@@ -1,8 +1,14 @@
#[macro_use]
extern crate serde_derive;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::net;
use std::path::{Path, PathBuf};
use std::str;
use std::time::Duration;
use std::ffi::CString;
extern crate serde;
extern crate serde_test;
use self::serde_test::{
@@ -15,8 +21,8 @@ use self::serde_test::{
extern crate fnv;
use self::fnv::FnvHasher;
#[cfg(feature = "unstable")]
use serde::ser::iterator;
#[macro_use]
mod macros;
//////////////////////////////////////////////////////////////////////////
@@ -360,6 +366,19 @@ declare_tests! {
Token::StructEnd,
],
}
test_range {
1u32..2u32 => &[
Token::StructStart("Range", 2),
Token::StructSep,
Token::Str("start"),
Token::U32(1),
Token::StructSep,
Token::Str("end"),
Token::U32(2),
Token::StructEnd,
],
}
test_net_ipv4addr {
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
}
@@ -381,61 +400,29 @@ declare_tests! {
Token::Str("/usr/local/lib"),
],
}
}
#[cfg(feature = "unstable")]
#[test]
fn test_iterator() {
assert_ser_tokens(iterator([0; 0].iter()), &[
Token::SeqStart(Some(0)),
Token::SeqEnd,
]);
assert_ser_tokens(iterator([1, 2, 3].iter()), &[
Token::SeqStart(Some(3)),
Token::SeqSep,
Token::I32(1),
Token::SeqSep,
Token::I32(2),
Token::SeqSep,
Token::I32(3),
Token::SeqEnd,
]);
assert_ser_tokens(iterator([1, 2, 3].iter().map(|x| x * 2)), &[
Token::SeqStart(Some(3)),
Token::SeqSep,
Token::I32(2),
Token::SeqSep,
Token::I32(4),
Token::SeqSep,
Token::I32(6),
Token::SeqEnd,
]);
assert_ser_tokens(iterator([1, 2, 3].iter().filter(|&x| x % 2 != 0)), &[
Token::SeqStart(None),
Token::SeqSep,
Token::I32(1),
Token::SeqSep,
Token::I32(3),
Token::SeqEnd,
]);
test_cstring {
CString::new("abc").unwrap() => &[
Token::Bytes(b"abc"),
],
}
test_cstr {
(&*CString::new("abc").unwrap()) => &[
Token::Bytes(b"abc"),
],
}
}
#[cfg(feature = "unstable")]
#[test]
fn test_net_ipaddr() {
assert_ser_tokens(
"1.2.3.4".parse::<net::IpAddr>().unwrap(),
&"1.2.3.4".parse::<net::IpAddr>().unwrap(),
&[Token::Str("1.2.3.4")],
);
}
#[test]
#[cfg(unix)]
fn test_cannot_serialize_paths() {
let path = unsafe {
str::from_utf8_unchecked(b"Hello \xF0\x90\x80World")
+19
View File
@@ -0,0 +1,19 @@
#[macro_use]
extern crate serde_derive;
extern crate serde;
use serde::Deserialize;
use serde::de::value::{self, ValueDeserializer};
#[test]
fn test_u32_to_enum() {
#[derive(Deserialize, Debug, PartialEq)]
enum E {
A,
B,
}
let deserializer = ValueDeserializer::<value::Error>::into_deserializer(1u32);
let e: E = E::deserialize(deserializer).unwrap();
assert_eq!(E::B, e);
}
+7 -2
View File
@@ -10,6 +10,11 @@ channel() {
pwd
(set -x; cargo "$@")
fi
elif [ -n "${APPVEYOR}" ]; then
if [ "${APPVEYOR_RUST_CHANNEL}" = "${CHANNEL}" ]; then
pwd
(set -x; cargo "$@")
fi
else
pwd
(set -x; cargo "+${CHANNEL}" "$@")
@@ -30,7 +35,7 @@ if [ -n "${CLIPPY}" ]; then
cargo clippy --features unstable-testing -- -Dclippy
cd "$DIR/test_suite"
cargo clippy --features unstable-testing -- -Dclippy
cargo clippy --features unstable -- -Dclippy
cd "$DIR/test_suite/no_std"
cargo clippy -- -Dclippy
@@ -46,7 +51,7 @@ else
cd "$DIR/test_suite/deps"
channel build
cd "$DIR/test_suite"
channel test --features unstable-testing
channel test --features unstable
cd "$DIR/test_suite/no_std"
channel build