Compare commits

...

19 Commits

Author SHA1 Message Date
David Tolnay 964a2dd4d1 Release 0.9.7 2017-02-09 17:52:11 -08:00
David Tolnay 8a21bbc720 Merge pull request #755 from serde-rs/playground
Playground feature to get serde_derive picked up by integer32 playground
2017-02-09 17:50:39 -08:00
Oliver Schneider 4dba260ad7 Merge pull request #756 from shepmaster/patch-2
Add missing close backtick
2017-02-09 18:01:35 +01:00
Jake Goulding 1d3044fa28 Add missing close backtick 2017-02-09 11:53:12 -05:00
David Tolnay d1f0112bfb Playground feature to get serde_derive picked up by integer32 playground 2017-02-08 12:37:40 -08:00
David Tolnay 3f25cd9a7e No longer need ser::Error import 2017-02-08 08:17:43 -08:00
David Tolnay 45a36f1219 Fix proc macro panic message in tests 2017-02-08 08:12:32 -08:00
David Tolnay 529a1cfedb Keyword no_std for serde and serde_derive 2017-02-08 07:40:02 -08:00
David Tolnay 219abd2e00 The performance is identical 2017-02-04 19:04:29 -08:00
David Tolnay 4bd10528a0 Merge pull request #736 from nox/serialize-iter
Introduce collect_seq and collect_map
2017-02-04 11:18:36 -08:00
David Tolnay b82bba2d0a Merge pull request #747 from serde-rs/phantom_tags
Content clean-up
2017-02-04 08:53:04 -08:00
Anthony Ramine 17c175a1a6 Use specialisation to optimise collect_seq and collect_map 2017-02-04 12:22:58 +01:00
Anthony Ramine 763ab9c2a1 Use serialize_seq! to implement Serialize for [T] 2017-02-04 12:21:45 +01:00
Anthony Ramine 30b8036efa Remove ser::Iterator 2017-02-04 12:17:25 +01:00
Anthony Ramine 89bb16da6b Introduce Serializer::collect_map 2017-02-04 11:49:53 +01:00
Anthony Ramine d00a895902 Introduce Serializer::collect_seq
This function serializes the given iterator as a sequence. Its iter parameter
has type I: IntoIterator, <I as IntoIterator>::Item: Serialize, which means it
will work both for iterators passed by value, therefore consuming them, and as
the value for a #[serde(serialize_with)] attribute, where it will be called as
Serializer::collect_seq(&self.field, serializer), relying on the common practice
of implementing IntoIterator for &C where C is a data type representing some
kind of collection.
2017-02-04 11:49:34 +01:00
Oliver Schneider 393b19ee8a update codegen to the content changes 2017-02-04 10:37:50 +01:00
Oliver Schneider e68888d475 remove possible sublte ambiguity with .into_deserializer 2017-02-04 10:00:04 +01:00
Oliver Schneider 6277079152 move the PhantomData usage to the Deserializer 2017-02-03 15:02:40 +01:00
24 changed files with 213 additions and 199 deletions
+8 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "0.9.6"
version = "0.9.7"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
@@ -8,7 +8,7 @@ homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://docs.serde.rs/serde/"
readme = "../README.md"
keywords = ["serde", "serialization"]
keywords = ["serde", "serialization", "no_std"]
categories = ["encoding"]
include = ["Cargo.toml", "src/**/*.rs"]
@@ -24,5 +24,11 @@ alloc = ["unstable"]
collections = ["alloc"]
unstable-testing = ["unstable", "std"]
# to get serde_derive picked up by play.integer32.com
playground = ["serde_derive"]
[dependencies]
serde_derive = { version = "0.9", optional = true }
[dev-dependencies]
serde_derive = "0.9"
+109 -91
View File
@@ -35,9 +35,9 @@ use de::{
///
/// Not public API. Use serde-value instead.
#[derive(Debug)]
pub enum Content<E> {
pub enum Content {
// Don't mind the PhantomData, just need to use E somewhere.
Bool(bool, PhantomData<E>),
Bool(bool),
U8(u8),
U16(u16),
@@ -57,36 +57,26 @@ pub enum Content<E> {
Bytes(Vec<u8>),
None,
Some(Box<Content<E>>),
Some(Box<Content>),
Unit,
Newtype(Box<Content<E>>),
Seq(Vec<Content<E>>),
Map(Vec<(Content<E>, Content<E>)>),
Newtype(Box<Content>),
Seq(Vec<Content>),
Map(Vec<(Content, Content)>),
}
impl<E> Deserialize for Content<E> {
impl Deserialize for Content {
fn deserialize<D: Deserializer>(deserializer: D) -> Result<Self, D::Error> {
// Untagged and internally tagged enums are only supported in
// self-describing formats.
deserializer.deserialize(ContentVisitor::new())
deserializer.deserialize(ContentVisitor)
}
}
struct ContentVisitor<E> {
err: PhantomData<E>,
}
struct ContentVisitor;
impl<E> ContentVisitor<E> {
fn new() -> Self {
ContentVisitor {
err: PhantomData,
}
}
}
impl<E> Visitor for ContentVisitor<E> {
type Value = Content<E>;
impl Visitor for ContentVisitor {
type Value = Content;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("any value")
@@ -95,7 +85,7 @@ impl<E> Visitor for ContentVisitor<E> {
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
where F: de::Error
{
Ok(Content::Bool(value, PhantomData))
Ok(Content::Bool(value))
}
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
@@ -242,27 +232,25 @@ impl<E> Visitor for ContentVisitor<E> {
/// This is the type of the map keys in an internally tagged enum.
///
/// Not public API.
pub enum TagOrContent<E> {
pub enum TagOrContent {
Tag,
Content(Content<E>),
Content(Content),
}
struct TagOrContentVisitor<E> {
struct TagOrContentVisitor {
name: &'static str,
err: PhantomData<E>,
}
impl<E> TagOrContentVisitor<E> {
impl TagOrContentVisitor {
fn new(name: &'static str) -> Self {
TagOrContentVisitor {
name: name,
err: PhantomData,
}
}
}
impl<E> DeserializeSeed for TagOrContentVisitor<E> {
type Value = TagOrContent<E>;
impl DeserializeSeed for TagOrContentVisitor {
type Value = TagOrContent;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
@@ -273,8 +261,8 @@ impl<E> DeserializeSeed for TagOrContentVisitor<E> {
}
}
impl<E> Visitor for TagOrContentVisitor<E> {
type Value = TagOrContent<E>;
impl Visitor for TagOrContentVisitor {
type Value = TagOrContent;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "a type tag `{}` or any other value", self.name)
@@ -283,73 +271,73 @@ impl<E> Visitor for TagOrContentVisitor<E> {
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_bool(value).map(TagOrContent::Content)
ContentVisitor.visit_bool(value).map(TagOrContent::Content)
}
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i8(value).map(TagOrContent::Content)
ContentVisitor.visit_i8(value).map(TagOrContent::Content)
}
fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i16(value).map(TagOrContent::Content)
ContentVisitor.visit_i16(value).map(TagOrContent::Content)
}
fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i32(value).map(TagOrContent::Content)
ContentVisitor.visit_i32(value).map(TagOrContent::Content)
}
fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_i64(value).map(TagOrContent::Content)
ContentVisitor.visit_i64(value).map(TagOrContent::Content)
}
fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u8(value).map(TagOrContent::Content)
ContentVisitor.visit_u8(value).map(TagOrContent::Content)
}
fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u16(value).map(TagOrContent::Content)
ContentVisitor.visit_u16(value).map(TagOrContent::Content)
}
fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u32(value).map(TagOrContent::Content)
ContentVisitor.visit_u32(value).map(TagOrContent::Content)
}
fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_u64(value).map(TagOrContent::Content)
ContentVisitor.visit_u64(value).map(TagOrContent::Content)
}
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_f32(value).map(TagOrContent::Content)
ContentVisitor.visit_f32(value).map(TagOrContent::Content)
}
fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_f64(value).map(TagOrContent::Content)
ContentVisitor.visit_f64(value).map(TagOrContent::Content)
}
fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_char(value).map(TagOrContent::Content)
ContentVisitor.visit_char(value).map(TagOrContent::Content)
}
fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
@@ -358,7 +346,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_str(value).map(TagOrContent::Content)
ContentVisitor.visit_str(value).map(TagOrContent::Content)
}
}
@@ -368,7 +356,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_string(value).map(TagOrContent::Content)
ContentVisitor.visit_string(value).map(TagOrContent::Content)
}
}
@@ -378,7 +366,7 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name.as_bytes() {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_bytes(value).map(TagOrContent::Content)
ContentVisitor.visit_bytes(value).map(TagOrContent::Content)
}
}
@@ -388,84 +376,82 @@ impl<E> Visitor for TagOrContentVisitor<E> {
if value == self.name.as_bytes() {
Ok(TagOrContent::Tag)
} else {
ContentVisitor::new().visit_byte_buf(value).map(TagOrContent::Content)
ContentVisitor.visit_byte_buf(value).map(TagOrContent::Content)
}
}
fn visit_unit<F>(self) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_unit().map(TagOrContent::Content)
ContentVisitor.visit_unit().map(TagOrContent::Content)
}
fn visit_none<F>(self) -> Result<Self::Value, F>
where F: de::Error
{
ContentVisitor::new().visit_none().map(TagOrContent::Content)
ContentVisitor.visit_none().map(TagOrContent::Content)
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
ContentVisitor::new().visit_some(deserializer).map(TagOrContent::Content)
ContentVisitor.visit_some(deserializer).map(TagOrContent::Content)
}
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
{
ContentVisitor::new().visit_newtype_struct(deserializer).map(TagOrContent::Content)
ContentVisitor.visit_newtype_struct(deserializer).map(TagOrContent::Content)
}
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor
{
ContentVisitor::new().visit_seq(visitor).map(TagOrContent::Content)
ContentVisitor.visit_seq(visitor).map(TagOrContent::Content)
}
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor
{
ContentVisitor::new().visit_map(visitor).map(TagOrContent::Content)
ContentVisitor.visit_map(visitor).map(TagOrContent::Content)
}
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: EnumVisitor
{
ContentVisitor::new().visit_enum(visitor).map(TagOrContent::Content)
ContentVisitor.visit_enum(visitor).map(TagOrContent::Content)
}
}
/// Used by generated code to deserialize an internally tagged enum.
///
/// Not public API.
pub struct TaggedContent<T, E> {
pub struct TaggedContent<T> {
pub tag: T,
pub content: Content<E>,
pub content: Content,
}
/// Not public API.
pub struct TaggedContentVisitor<T, E> {
pub struct TaggedContentVisitor<T> {
tag_name: &'static str,
tag: PhantomData<T>,
err: PhantomData<E>,
}
impl<T, E> TaggedContentVisitor<T, E> {
impl<T> TaggedContentVisitor<T> {
/// Visitor for the content of an internally tagged enum with the given tag
/// name.
pub fn new(name: &'static str) -> Self {
TaggedContentVisitor {
tag_name: name,
tag: PhantomData,
err: PhantomData,
}
}
}
impl<T, E> DeserializeSeed for TaggedContentVisitor<T, E>
impl<T> DeserializeSeed for TaggedContentVisitor<T>
where T: Deserialize
{
type Value = TaggedContent<T, E>;
type Value = TaggedContent<T>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer
@@ -476,10 +462,10 @@ impl<T, E> DeserializeSeed for TaggedContentVisitor<T, E>
}
}
impl<T, E> Visitor for TaggedContentVisitor<T, E>
impl<T> Visitor for TaggedContentVisitor<T>
where T: Deserialize
{
type Value = TaggedContent<T, E>;
type Value = TaggedContent<T>;
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("any value")
@@ -518,9 +504,15 @@ impl<T, E> Visitor for TaggedContentVisitor<T, E>
}
}
/// Not public API
pub struct ContentDeserializer<E> {
content: Content,
err: PhantomData<E>,
}
/// Used when deserializing an internally tagged enum because the content will
/// be used exactly once.
impl<E> Deserializer for Content<E>
impl<E> Deserializer for ContentDeserializer<E>
where E: de::Error
{
type Error = E;
@@ -528,8 +520,8 @@ impl<E> Deserializer for Content<E>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match self {
Content::Bool(v, _) => visitor.visit_bool(v),
match self.content {
Content::Bool(v) => visitor.visit_bool(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
@@ -544,17 +536,17 @@ impl<E> Deserializer for Content<E>
Content::String(v) => visitor.visit_string(v),
Content::Unit => visitor.visit_unit(),
Content::None => visitor.visit_none(),
Content::Some(v) => visitor.visit_some(*v),
Content::Newtype(v) => visitor.visit_newtype_struct(*v),
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
Content::Seq(v) => {
let seq = v.into_iter();
let seq = v.into_iter().map(ContentDeserializer::new);
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
let value = try!(visitor.visit_seq(&mut seq_visitor));
try!(seq_visitor.end());
Ok(value)
},
Content::Map(v) => {
let map = v.into_iter();
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());
@@ -567,9 +559,9 @@ impl<E> Deserializer for Content<E>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match self {
match self.content {
Content::None => visitor.visit_none(),
Content::Some(v) => visitor.visit_some(*v),
Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
}
@@ -588,19 +580,25 @@ impl<E> Deserializer for Content<E>
}
}
impl<E> de::value::ValueDeserializer<E> for Content<E>
where E: de::Error
{
type Deserializer = Self;
fn into_deserializer(self) -> Self {
self
impl<E> ContentDeserializer<E> {
/// private API, don't use
pub fn new(content: Content) -> Self {
ContentDeserializer {
content: content,
err: PhantomData,
}
}
}
/// Not public API.
pub struct ContentRefDeserializer<'a, E> {
content: &'a Content,
err: PhantomData<E>,
}
/// Used when deserializing an untagged enum because the content may need to be
/// used more than once.
impl<'a, E> Deserializer for &'a Content<E>
impl<'a, E> Deserializer for ContentRefDeserializer<'a, E>
where E: de::Error
{
type Error = E;
@@ -608,8 +606,8 @@ impl<'a, E> Deserializer for &'a Content<E>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match *self {
Content::Bool(v, _) => visitor.visit_bool(v),
match *self.content {
Content::Bool(v) => visitor.visit_bool(v),
Content::U8(v) => visitor.visit_u8(v),
Content::U16(v) => visitor.visit_u16(v),
Content::U32(v) => visitor.visit_u32(v),
@@ -624,17 +622,17 @@ impl<'a, E> Deserializer for &'a Content<E>
Content::String(ref v) => visitor.visit_str(v),
Content::Unit => visitor.visit_unit(),
Content::None => visitor.visit_none(),
Content::Some(ref v) => visitor.visit_some(&**v),
Content::Newtype(ref v) => visitor.visit_newtype_struct(&**v),
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
Content::Newtype(ref v) => visitor.visit_newtype_struct(ContentRefDeserializer::new(v)),
Content::Seq(ref v) => {
let seq = v.into_iter();
let seq = v.into_iter().map(ContentRefDeserializer::new);
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
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)| (k, 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());
@@ -647,9 +645,9 @@ impl<'a, E> Deserializer for &'a Content<E>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
match *self {
match *self.content {
Content::None => visitor.visit_none(),
Content::Some(ref v) => visitor.visit_some(&**v),
Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
Content::Unit => visitor.visit_unit(),
_ => visitor.visit_some(self)
}
@@ -668,7 +666,27 @@ impl<'a, E> Deserializer for &'a Content<E>
}
}
impl<'a, E> de::value::ValueDeserializer<E> for &'a Content<E>
impl<'a, E> ContentRefDeserializer<'a, E> {
/// private API, don't use
pub fn new(content: &'a Content) -> Self {
ContentRefDeserializer {
content: content,
err: PhantomData,
}
}
}
impl<E> de::value::ValueDeserializer<E> for ContentDeserializer<E>
where E: de::Error
{
type Deserializer = Self;
fn into_deserializer(self) -> Self {
self
}
}
impl<'a, E> de::value::ValueDeserializer<E> for ContentRefDeserializer<'a, E>
where E: de::Error
{
type Deserializer = Self;
+1 -1
View File
@@ -859,7 +859,7 @@ pub trait Deserializer: Sized {
/// benefit from taking ownership of buffered data owned by the
/// `Deserializer`.
///
/// If the `Visitor would benefit from taking ownership of `Vec<u8>` data,
/// 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>
+2
View File
@@ -5,6 +5,8 @@ use de::{Deserialize, Deserializer, Error, Visitor};
#[cfg(any(feature = "std", feature = "collections"))]
pub use de::content::{
Content,
ContentRefDeserializer,
ContentDeserializer,
TaggedContentVisitor,
InternallyTaggedUnitVisitor,
UntaggedUnitVisitor,
+1 -1
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(nonzero, inclusive_range, zero_one))]
#![cfg_attr(feature = "unstable", feature(inclusive_range, nonzero, specialization, zero_one))]
#![cfg_attr(feature = "alloc", feature(alloc))]
#![cfg_attr(feature = "collections", feature(collections))]
#![cfg_attr(feature = "cargo-clippy", allow(linkedlist, type_complexity, doc_markdown))]
+9 -60
View File
@@ -63,12 +63,9 @@ use super::{
SerializeTuple,
Serializer,
};
#[cfg(any(feature = "std", feature = "unstable"))]
#[cfg(feature = "std")]
use super::Error;
#[cfg(feature = "unstable")]
use super::Iterator;
///////////////////////////////////////////////////////////////////////////////
macro_rules! impl_visit {
@@ -147,24 +144,6 @@ impl<T> Serialize for PhantomData<T> {
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T> Serialize for [T]
where T: 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 {
try!(seq.serialize_element(e));
}
seq.end()
}
}
///////////////////////////////////////////////////////////////////////////////
macro_rules! array_impls {
@@ -220,48 +199,23 @@ array_impls!(32);
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "unstable")]
impl<'a, I> Serialize for Iterator<I>
where I: IntoIterator, <I as IntoIterator>::Item: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
{
// FIXME: use specialization to prevent invalidating the object in case of clonable iterators?
let iter = match self.data.borrow_mut().take() {
Some(iter) => iter.into_iter(),
None => return Err(Error::custom("Iterator used twice")),
};
let size = match iter.size_hint() {
(lo, Some(hi)) if lo == hi => Some(lo),
_ => None,
};
let mut seq = try!(serializer.serialize_seq(size));
for e in iter {
try!(seq.serialize_element(&e));
}
seq.end()
}
}
///////////////////////////////////////////////////////////////////////////////
macro_rules! serialize_seq {
() => {
#[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 {
try!(seq.serialize_element(&e));
}
seq.end()
serializer.collect_seq(self)
}
}
}
impl<T> Serialize for [T]
where T: Serialize,
{
serialize_seq!();
}
#[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for BinaryHeap<T>
where T: Serialize + Ord
@@ -556,12 +510,7 @@ macro_rules! serialize_map {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
{
use super::SerializeMap;
let mut map = try!(serializer.serialize_map(Some(self.len())));
for (k, v) in self {
try!(map.serialize_entry(k, v));
}
map.end()
serializer.collect_map(self)
}
}
}
+62 -23
View File
@@ -98,10 +98,8 @@ use std::error;
#[cfg(not(feature = "std"))]
use error;
#[cfg(feature = "unstable")]
use core::cell::RefCell;
use core::fmt::Display;
use core::iter::IntoIterator;
mod impls;
mod impossible;
@@ -242,7 +240,7 @@ pub trait Serialize {
/// is the `serde_json::value::Serializer` (distinct from the main `serde_json`
/// serializer) that produces a `serde_json::Value` data structure in memory as
/// output.
pub trait Serializer {
pub trait Serializer: Sized {
/// The output type produced by this `Serializer` during successful
/// serialization. Most serializers that produce text or binary output
/// should set `Ok = ()` and serialize into an `io::Write` or buffer
@@ -607,6 +605,41 @@ pub trait Serializer {
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error>;
/// Collect an iterator as a sequence.
///
/// The default implementation serializes each item yielded by the iterator
/// using `Self::SerializeSeq`. Implementors should not need to override
/// this method.
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_seq(iter.len_hint()));
for item in iter {
try!(serializer.serialize_element(&item));
}
serializer.end()
}
/// Collect an iterator as a map.
///
/// The default implementation serializes each pair yielded by the iterator
/// using `Self::SerializeMap`. Implementors should not need to override
/// this method.
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where K: Serialize,
V: Serialize,
I: IntoIterator<Item = (K, V)>,
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_map(iter.len_hint()));
for (key, value) in iter {
try!(serializer.serialize_entry(&key, &value));
}
serializer.end()
}
}
/// Returned from `Serializer::serialize_seq` and
@@ -803,26 +836,32 @@ pub trait SerializeStructVariant {
fn end(self) -> Result<Self::Ok, Self::Error>;
}
/// A wrapper type for iterators that implements `Serialize` for iterators whose
/// items implement `Serialize`. Don't use multiple times. Create new versions
/// of this with the `serde::ser::iterator` function every time you want to
/// serialize an iterator.
#[cfg(feature = "unstable")]
pub struct Iterator<I>
where <I as IntoIterator>::Item: Serialize,
I: IntoIterator
{
data: RefCell<Option<I>>,
trait LenHint: Iterator {
fn len_hint(&self) -> Option<usize>;
}
/// Create a wrapper type that can be passed to any function expecting a
/// `Serialize` and will serialize the given iterator as a sequence.
#[cfg(feature = "unstable")]
pub fn iterator<I>(iter: I) -> Iterator<I>
where <I as IntoIterator>::Item: Serialize,
I: IntoIterator
{
Iterator {
data: RefCell::new(Some(iter)),
impl<I: Iterator> LenHint for I {
#[cfg(not(feature = "unstable"))]
fn len_hint(&self) -> Option<usize> {
iterator_len_hint(self)
}
#[cfg(feature = "unstable")]
default fn len_hint(&self) -> Option<usize> {
iterator_len_hint(self)
}
}
#[cfg(feature = "unstable")]
impl<I: ExactSizeIterator> LenHint for I {
fn len_hint(&self) -> Option<usize> {
Some(self.len())
}
}
fn iterator_len_hint<I: Iterator>(iter: &I) -> Option<usize> {
match iter.size_hint() {
(lo, Some(hi)) if lo == hi => Some(lo),
_ => None,
}
}
+2 -2
View File
@@ -1,13 +1,13 @@
[package]
name = "serde_derive"
version = "0.9.6"
version = "0.9.7"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://serde.rs/codegen.html"
keywords = ["serde", "serialization"]
keywords = ["serde", "serialization", "no_std"]
include = ["Cargo.toml", "src/**/*.rs"]
[features]
+4 -4
View File
@@ -704,7 +704,7 @@ fn deserialize_internally_tagged_enum(
ty.clone(),
variant,
item_attrs,
quote!(_tagged.content),
quote!(_serde::de::private::ContentDeserializer::<__D::Error>::new(_tagged.content)),
);
quote! {
@@ -719,7 +719,7 @@ fn deserialize_internally_tagged_enum(
let _tagged = try!(_serde::Deserializer::deserialize(
deserializer,
_serde::de::private::TaggedContentVisitor::<__Field, __D::Error>::new(#tag)));
_serde::de::private::TaggedContentVisitor::<__Field>::new(#tag)));
match _tagged.tag {
#(#variant_arms)*
@@ -743,7 +743,7 @@ fn deserialize_untagged_enum(
ty.clone(),
variant,
item_attrs,
quote!(&_content),
quote!(_serde::de::private::ContentRefDeserializer::<__D::Error>::new(&_content)),
)
});
@@ -756,7 +756,7 @@ fn deserialize_untagged_enum(
let fallthrough_msg = format!("data did not match any variant of untagged enum {}", type_ident);
quote!({
let _content = try!(<_serde::de::private::Content<__D::Error> as _serde::Deserialize>::deserialize(deserializer));
let _content = try!(<_serde::de::private::Content as _serde::Deserialize>::deserialize(deserializer));
#(
if let _serde::export::Ok(ok) = #attempts {
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "serde_test"
version = "0.9.6"
version = "0.9.7"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Token De/Serializer for testing De/Serialize implementations"
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename="x", serialize="y")] //~^^ HELP: unknown serde field attribute `serialize`
x: (),
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename="x")]
#[serde(rename(deserialize="y"))] //~^^^ HELP: duplicate serde attribute `rename`
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename(serialize="x"), rename(serialize="y"))] //~^^ HELP: duplicate serde attribute `rename`
x: (),
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename(serialize="x"))]
#[serde(rename="y")] //~^^^ HELP: duplicate serde attribute `rename`
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename(serialize="x", serialize="y"))] //~^^ HELP: duplicate serde attribute `rename`
x: (),
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct S {
#[serde(rename(serialize="x"))]
#[serde(rename(serialize="y"))] //~^^^ HELP: duplicate serde attribute `rename`
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(tag = "type")] //~^ HELP: #[serde(tag = "...")] cannot be used with tuple variants
enum E {
Tuple(u8, u8),
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(tag = "type")] //~^ HELP: #[serde(tag = "...")] can only be used on enums
struct S;
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(untagged)]
#[serde(tag = "type")] //~^^ HELP: enum cannot be both untagged and internally tagged
enum E {
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(untagged)] //~^ HELP: #[serde(untagged)] can only be used on enums
struct S;
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize, Deserialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize, Deserialize)] //~ ERROR: proc-macro derive panicked
struct Test<'a> {
s: &'a str, //~^^ HELP: Serde does not support deserializing fields of type &str
}
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
#[serde(abc="xyz")] //~^ HELP: unknown serde container attribute `abc`
struct A {
x: u32,
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
struct C {
#[serde(abc="xyz")] //~^^ HELP: unknown serde field attribute `abc`
x: u32,
@@ -1,7 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: custom derive attribute panicked
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
enum E {
#[serde(abc="xyz")] //~^^ HELP: unknown serde variant attribute `abc`
V,