From e4409bcd8577033272d57a966403277ee197d67c Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 13 Jul 2016 19:45:06 +0200 Subject: [PATCH 01/11] serde push-serialization --- yaml/src/ser.rs | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index 0295da50..de10f6df 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -76,16 +76,16 @@ impl<'a> ser::Serializer for Serializer<'a> { value.serialize(self) } - fn serialize_seq(&mut self, mut visitor: V) -> Result<()> - where V: ser::SeqVisitor, - { - let vec = match visitor.len() { + fn serialize_seq(&mut self, len: Option) -> Result<()> { + let vec = match len { None => yaml::Array::new(), Some(len) => yaml::Array::with_capacity(len), }; *self.doc = Yaml::Array(vec); - while try!(visitor.visit(self)).is_some() { - } + Ok(()) + } + + fn serialize_seq_end(&mut self, _len: Option) -> Result<()> { Ok(()) } @@ -100,12 +100,13 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } - fn serialize_map(&mut self, mut visitor: V) -> Result<()> - where V: ser::MapVisitor, + fn serialize_map(&mut self, _len: Option) -> Result<()> { *self.doc = Yaml::Hash(yaml::Hash::new()); - while try!(visitor.visit(self)).is_some() { - } + Ok(()) + } + + fn serialize_map_end(&mut self, _len: Option) -> Result<()> { Ok(()) } @@ -156,33 +157,25 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } - fn serialize_tuple_variant( + fn serialize_tuple_variant_end( &mut self, _name: &str, _variant_index: usize, variant: &str, - visitor: V - ) -> Result<()> - where V: ser::SeqVisitor, - { - let mut values = Yaml::Null; - try!(Serializer::new(&mut values).serialize_seq(visitor)); - *self.doc = singleton_hash(try!(to_yaml(variant)), values); + _len: usize, + ) -> Result<()> { + *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } - fn serialize_struct_variant( + fn serialize_struct_variant_end( &mut self, _name: &str, _variant_index: usize, variant: &str, - visitor: V - ) -> Result<()> - where V: ser::MapVisitor, - { - let mut values = Yaml::Null; - try!(Serializer::new(&mut values).serialize_map(visitor)); - *self.doc = singleton_hash(try!(to_yaml(variant)), values); + _len: usize, + ) -> Result<()> { + *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } } From 824ad38317c16fa451e5b8e690400643e720d664 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 15 Jul 2016 15:23:31 +0200 Subject: [PATCH 02/11] update to next iteration --- yaml/src/ser.rs | 158 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 146 insertions(+), 12 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index de10f6df..fe9b63e4 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -36,12 +36,51 @@ impl<'a> Serializer<'a> { impl<'a> ser::Serializer for Serializer<'a> { type Error = Error; + type SeqState = (); + type TupleState = (); + type TupleStructState = (); + type TupleVariantState = &'static str; + type MapState = (); + type StructState = (); + type StructVariantState = &'static str; fn serialize_bool(&mut self, v: bool) -> Result<()> { *self.doc = Yaml::Boolean(v); Ok(()) } + fn serialize_isize(&mut self, v: isize) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_usize(&mut self, v: usize) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_i8(&mut self, v: i8) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_u8(&mut self, v: u8) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_i16(&mut self, v: i16) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_u16(&mut self, v: u16) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_i32(&mut self, v: i32) -> Result<()> { + self.serialize_u64(v as u64) + } + + fn serialize_u32(&mut self, v: u32) -> Result<()> { + self.serialize_u64(v as u64) + } + fn serialize_i64(&mut self, v: i64) -> Result<()> { *self.doc = Yaml::Integer(v); Ok(()) @@ -56,6 +95,16 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } + fn serialize_f32(&mut self, v: f32) -> Result<()> { + *self.doc = Yaml::Real(v.to_string()); + Ok(()) + } + + fn serialize_char(&mut self, value: char) -> Result<()> { + *self.doc = Yaml::String(value.to_string()); + Ok(()) + } + fn serialize_str(&mut self, value: &str) -> Result<()> { *self.doc = Yaml::String(String::from(value)); Ok(()) @@ -66,6 +115,11 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } + fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> { + *self.doc = Yaml::Null; + Ok(()) + } + fn serialize_none(&mut self) -> Result<()> { self.serialize_unit() } @@ -76,6 +130,10 @@ impl<'a> ser::Serializer for Serializer<'a> { value.serialize(self) } + fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<()> { + self.serialize_seq(Some(len)) + } + fn serialize_seq(&mut self, len: Option) -> Result<()> { let vec = match len { None => yaml::Array::new(), @@ -85,11 +143,11 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } - fn serialize_seq_end(&mut self, _len: Option) -> Result<()> { + fn serialize_seq_end(&mut self, _: ()) -> Result<()> { Ok(()) } - fn serialize_seq_elt(&mut self, elem: T) -> Result<()> + fn serialize_seq_elt(&mut self, _: &mut(), elem: T) -> Result<()> where T: ser::Serialize, { if let Yaml::Array(ref mut vec) = *self.doc { @@ -100,17 +158,31 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } + fn serialize_tuple(&mut self, len: usize) -> Result<()> { + self.serialize_seq(Some(len)) + } + + fn serialize_tuple_end(&mut self, _: ()) -> Result<()> { + Ok(()) + } + + fn serialize_tuple_elt(&mut self, state: &mut(), elem: T) -> Result<()> + where T: ser::Serialize, + { + self.serialize_seq_elt(state, elem) + } + fn serialize_map(&mut self, _len: Option) -> Result<()> { *self.doc = Yaml::Hash(yaml::Hash::new()); Ok(()) } - fn serialize_map_end(&mut self, _len: Option) -> Result<()> { + fn serialize_map_end(&mut self, _: ()) -> Result<()> { Ok(()) } - fn serialize_map_elt(&mut self, key: K, value: V) -> Result<()> + fn serialize_map_elt(&mut self, _: &mut (), key: K, value: V) -> Result<()> where K: ser::Serialize, V: ser::Serialize, { @@ -122,6 +194,36 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } + fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<()> + { + self.serialize_map(Some(len)) + } + + fn serialize_struct_end(&mut self, _: ()) -> Result<()> { + Ok(()) + } + + fn serialize_struct_elt(&mut self, state: &mut (), key: &'static str, value: V) -> Result<()> + where V: ser::Serialize, + { + self.serialize_map_elt(state, key, value) + } + + fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<()> + { + self.serialize_tuple(len) + } + + fn serialize_tuple_struct_end(&mut self, _: ()) -> Result<()> { + Ok(()) + } + + fn serialize_tuple_struct_elt(&mut self, state: &mut (), value: V) -> Result<()> + where V: ser::Serialize, + { + self.serialize_tuple_elt(state, value) + } + fn serialize_unit_variant( &mut self, _name: &str, @@ -157,27 +259,59 @@ impl<'a> ser::Serializer for Serializer<'a> { Ok(()) } + fn serialize_tuple_variant( + &mut self, + _enum: &'static str, + _idx: usize, + variant: &'static str, + len: usize, + ) -> Result<&'static str> { + try!(self.serialize_tuple(len)); + Ok(variant) + } + + fn serialize_tuple_variant_elt(&mut self, _: &mut &'static str, v: V) -> Result<()> { + self.serialize_tuple_elt(&mut (), v) + } + fn serialize_tuple_variant_end( &mut self, - _name: &str, - _variant_index: usize, - variant: &str, - _len: usize, + variant: &'static str, ) -> Result<()> { *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } + fn serialize_struct_variant( + &mut self, + _enum: &'static str, + _idx: usize, + variant: &'static str, + len: usize, + ) -> Result<&'static str> { + try!(self.serialize_struct(variant, len)); + Ok(variant) + } + + fn serialize_struct_variant_elt(&mut self, _: &mut &'static str, field: &'static str, v: V) -> Result<()> { + self.serialize_struct_elt(&mut (), field, v) + } + fn serialize_struct_variant_end( &mut self, - _name: &str, - _variant_index: usize, - variant: &str, - _len: usize, + variant: &'static str, ) -> Result<()> { *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } + + fn serialize_bytes(&mut self, value: &[u8]) -> Result<()> { + let mut state = try!(self.serialize_seq(Some(value.len()))); + for c in value { + try!(self.serialize_seq_elt(&mut state, c)); + } + self.serialize_seq_end(state) + } } pub fn to_writer(writer: &mut W, value: &T) -> Result<()> From f58064ceeac297e829f787d12565ad492a56ac75 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 15 Jul 2016 16:32:42 +0200 Subject: [PATCH 03/11] give the Serializer its inner object by value --- yaml/src/ser.rs | 53 +++++++++++++++++++++++++---------------------- yaml/src/value.rs | 9 +++----- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index fe9b63e4..044574dd 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -21,20 +21,23 @@ use serde::ser; use super::error::{Error, Result}; /// A structure for serializing a Rust value into a YAML value. -pub struct Serializer<'a> { +pub struct Serializer { /// The YAML value to hold the result. - doc: &'a mut Yaml, + doc: Yaml, } -impl<'a> Serializer<'a> { - pub fn new(doc: &'a mut Yaml) -> Self { +impl Serializer { + pub fn new() -> Self { Serializer { - doc: doc, + doc: Yaml::Null, } } + pub fn take(self) -> Yaml { + self.doc + } } -impl<'a> ser::Serializer for Serializer<'a> { +impl ser::Serializer for Serializer { type Error = Error; type SeqState = (); type TupleState = (); @@ -45,7 +48,7 @@ impl<'a> ser::Serializer for Serializer<'a> { type StructVariantState = &'static str; fn serialize_bool(&mut self, v: bool) -> Result<()> { - *self.doc = Yaml::Boolean(v); + self.doc = Yaml::Boolean(v); Ok(()) } @@ -82,7 +85,7 @@ impl<'a> ser::Serializer for Serializer<'a> { } fn serialize_i64(&mut self, v: i64) -> Result<()> { - *self.doc = Yaml::Integer(v); + self.doc = Yaml::Integer(v); Ok(()) } @@ -91,32 +94,32 @@ impl<'a> ser::Serializer for Serializer<'a> { } fn serialize_f64(&mut self, v: f64) -> Result<()> { - *self.doc = Yaml::Real(v.to_string()); + self.doc = Yaml::Real(v.to_string()); Ok(()) } fn serialize_f32(&mut self, v: f32) -> Result<()> { - *self.doc = Yaml::Real(v.to_string()); + self.doc = Yaml::Real(v.to_string()); Ok(()) } fn serialize_char(&mut self, value: char) -> Result<()> { - *self.doc = Yaml::String(value.to_string()); + self.doc = Yaml::String(value.to_string()); Ok(()) } fn serialize_str(&mut self, value: &str) -> Result<()> { - *self.doc = Yaml::String(String::from(value)); + self.doc = Yaml::String(String::from(value)); Ok(()) } fn serialize_unit(&mut self) -> Result<()> { - *self.doc = Yaml::Null; + self.doc = Yaml::Null; Ok(()) } fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> { - *self.doc = Yaml::Null; + self.doc = Yaml::Null; Ok(()) } @@ -139,7 +142,7 @@ impl<'a> ser::Serializer for Serializer<'a> { None => yaml::Array::new(), Some(len) => yaml::Array::with_capacity(len), }; - *self.doc = Yaml::Array(vec); + self.doc = Yaml::Array(vec); Ok(()) } @@ -150,7 +153,7 @@ impl<'a> ser::Serializer for Serializer<'a> { fn serialize_seq_elt(&mut self, _: &mut(), elem: T) -> Result<()> where T: ser::Serialize, { - if let Yaml::Array(ref mut vec) = *self.doc { + if let Yaml::Array(ref mut vec) = self.doc { vec.push(try!(to_yaml(elem))); } else { panic!("bad call to serialize_seq_elt"); @@ -174,7 +177,7 @@ impl<'a> ser::Serializer for Serializer<'a> { fn serialize_map(&mut self, _len: Option) -> Result<()> { - *self.doc = Yaml::Hash(yaml::Hash::new()); + self.doc = Yaml::Hash(yaml::Hash::new()); Ok(()) } @@ -186,7 +189,7 @@ impl<'a> ser::Serializer for Serializer<'a> { where K: ser::Serialize, V: ser::Serialize, { - if let Yaml::Hash(ref mut map) = *self.doc { + if let Yaml::Hash(ref mut map) = self.doc { map.insert(try!(to_yaml(key)), try!(to_yaml(value))); } else { panic!("bad call to serialize_map_elt"); @@ -230,7 +233,7 @@ impl<'a> ser::Serializer for Serializer<'a> { _variant_index: usize, variant: &str ) -> Result<()> { - *self.doc = Yaml::String(String::from(variant)); + self.doc = Yaml::String(String::from(variant)); Ok(()) } @@ -254,7 +257,7 @@ impl<'a> ser::Serializer for Serializer<'a> { ) -> Result<()> where T: ser::Serialize, { - *self.doc = singleton_hash(try!(to_yaml(variant)), + self.doc = singleton_hash(try!(to_yaml(variant)), try!(to_yaml(value))); Ok(()) } @@ -278,7 +281,7 @@ impl<'a> ser::Serializer for Serializer<'a> { &mut self, variant: &'static str, ) -> Result<()> { - *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } @@ -301,7 +304,7 @@ impl<'a> ser::Serializer for Serializer<'a> { &mut self, variant: &'static str, ) -> Result<()> { - *self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } @@ -365,9 +368,9 @@ impl<'a, W> fmt::Write for FmtToIoWriter<'a, W> fn to_yaml(elem: T) -> Result where T: ser::Serialize, { - let mut result = Yaml::Null; - try!(elem.serialize(&mut Serializer::new(&mut result))); - Ok(result) + let mut ser = Serializer::new(); + try!(elem.serialize(&mut ser)); + Ok(ser.take()) } fn singleton_hash(k: Yaml, v: Yaml) -> Yaml { diff --git a/yaml/src/value.rs b/yaml/src/value.rs index a58c0ece..857ad622 100644 --- a/yaml/src/value.rs +++ b/yaml/src/value.rs @@ -22,12 +22,9 @@ use super::{Deserializer, Result, Serializer}; pub fn to_value(value: &T) -> Value where T: Serialize, { - let mut yaml = Value::Null; - { - let mut ser = Serializer::new(&mut yaml); - value.serialize(&mut ser).unwrap(); - } - yaml + let mut ser = Serializer::new(); + value.serialize(&mut ser).unwrap(); + ser.take() } /// Shortcut function to decode a YAML `Value` into a `T`. From fce768ad271cc1236b2a1c3b92e0e1272b2ed1f9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 09:36:33 -0700 Subject: [PATCH 04/11] Bump serde dependency to 0.8.0-rc3 --- yaml/Cargo.toml | 2 +- yaml_tests/Cargo.toml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/yaml/Cargo.toml b/yaml/Cargo.toml index a859903c..73186ba1 100644 --- a/yaml/Cargo.toml +++ b/yaml/Cargo.toml @@ -14,5 +14,5 @@ preserve_order = ["yaml-rust/preserve_order"] [dependencies] clippy = { version = "^0.*", optional = true } -serde = "^0.7" +serde = "0.8.0-rc3" yaml-rust = "^0.3.3" diff --git a/yaml_tests/Cargo.toml b/yaml_tests/Cargo.toml index 723a04c5..2442e12a 100644 --- a/yaml_tests/Cargo.toml +++ b/yaml_tests/Cargo.toml @@ -10,13 +10,13 @@ with-syntex = ["syntex", "serde_codegen", "indoc/with-syntex"] [build-dependencies] syntex = { version = "*", optional = true } -serde_codegen = { version = "*", optional = true } +serde_codegen = { version = "0.8.0-rc3", optional = true } indoc = "*" [dependencies] -serde = "*" +serde = "0.8.0-rc3" serde_yaml = { version = "*", path = "../yaml" } -serde_macros = { version = "*", optional = true } +serde_macros = { version = "0.8.0-rc3", optional = true } indoc = "*" [[test]] From 26c295e0af8e26321c593f8fa6e711482a92b743 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 09:36:45 -0700 Subject: [PATCH 05/11] Update deserializer API --- yaml/src/de.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++ yaml/src/forward.rs | 37 +++++++++++++++++++++++++++ yaml/src/lib.rs | 3 +++ 3 files changed, 102 insertions(+) create mode 100644 yaml/src/forward.rs diff --git a/yaml/src/de.rs b/yaml/src/de.rs index 23281b8c..28da23d3 100644 --- a/yaml/src/de.rs +++ b/yaml/src/de.rs @@ -136,6 +136,38 @@ impl<'a> de::MapVisitor for MapVisitor<'a> { { visitor.visit_none() } + + forward_to_deserialize!{ + deserialize_bool(); + deserialize_usize(); + deserialize_u8(); + deserialize_u16(); + deserialize_u32(); + deserialize_u64(); + deserialize_isize(); + deserialize_i8(); + deserialize_i16(); + deserialize_i32(); + deserialize_i64(); + deserialize_f32(); + deserialize_f64(); + deserialize_char(); + deserialize_str(); + deserialize_string(); + deserialize_unit(); + deserialize_seq(); + deserialize_seq_fixed_size(len: usize); + deserialize_bytes(); + deserialize_map(); + deserialize_unit_struct(name: &'static str); + deserialize_newtype_struct(name: &'static str); + deserialize_tuple_struct(name: &'static str, len: usize); + deserialize_struct(name: &'static str, fields: &'static [&'static str]); + deserialize_struct_field(); + deserialize_tuple(len: usize); + deserialize_enum(name: &'static str, variants: &'static [&'static str]); + deserialize_ignored_any(); + } } let mut de = MissingFieldDeserializer(field); @@ -271,6 +303,36 @@ impl<'a> de::Deserializer for Deserializer<'a> { _ => Err(Error::VariantNotAMapOrString(String::from(name))), } } + + forward_to_deserialize!{ + deserialize_bool(); + deserialize_usize(); + deserialize_u8(); + deserialize_u16(); + deserialize_u32(); + deserialize_u64(); + deserialize_isize(); + deserialize_i8(); + deserialize_i16(); + deserialize_i32(); + deserialize_i64(); + deserialize_f32(); + deserialize_f64(); + deserialize_char(); + deserialize_str(); + deserialize_string(); + deserialize_unit(); + deserialize_seq(); + deserialize_seq_fixed_size(len: usize); + deserialize_bytes(); + deserialize_map(); + deserialize_unit_struct(name: &'static str); + deserialize_tuple_struct(name: &'static str, len: usize); + deserialize_struct(name: &'static str, fields: &'static [&'static str]); + deserialize_struct_field(); + deserialize_tuple(len: usize); + deserialize_ignored_any(); + } } /// Decodes a YAML value from a `&str`. diff --git a/yaml/src/forward.rs b/yaml/src/forward.rs new file mode 100644 index 00000000..26327236 --- /dev/null +++ b/yaml/src/forward.rs @@ -0,0 +1,37 @@ +#[macro_export] +macro_rules! forward_to_deserialize { + ($( + $name:ident ( $( $arg:ident : $ty:ty ),* ); + )*) => { + $( + forward_to_deserialize!{ + func: $name ( $( $arg: $ty ),* ); + } + )* + }; + + (func: deserialize_enum ( $( $arg:ident : $ty:ty ),* );) => { + fn deserialize_enum( + &mut self, + $(_: $ty,)* + _visitor: V, + ) -> ::std::result::Result + where V: ::serde::de::EnumVisitor + { + Err(::serde::de::Error::invalid_type(::serde::de::Type::Enum)) + } + }; + + (func: $name:ident ( $( $arg:ident : $ty:ty ),* );) => { + #[inline] + fn $name( + &mut self, + $(_: $ty,)* + visitor: V, + ) -> ::std::result::Result + where V: ::serde::de::Visitor + { + self.deserialize(visitor) + } + }; +} diff --git a/yaml/src/lib.rs b/yaml/src/lib.rs index ba4d4087..146bdd2f 100644 --- a/yaml/src/lib.rs +++ b/yaml/src/lib.rs @@ -18,6 +18,9 @@ pub use self::ser::{Serializer, to_string, to_vec, to_writer}; pub use self::value::{Value, from_value, to_value}; pub use self::error::{Error, Result}; +#[macro_use] +mod forward; + pub mod de; pub mod ser; pub mod value; From fafa56cfeb8859dafaf15846bfca977d29825d51 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 09:47:20 -0700 Subject: [PATCH 06/11] Sort Serializer methods in the trait's order --- yaml/src/ser.rs | 224 ++++++++++++++++++++++++------------------------ 1 file changed, 112 insertions(+), 112 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index 044574dd..c0447609 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -32,6 +32,7 @@ impl Serializer { doc: Yaml::Null, } } + pub fn take(self) -> Yaml { self.doc } @@ -53,52 +54,52 @@ impl ser::Serializer for Serializer { } fn serialize_isize(&mut self, v: isize) -> Result<()> { - self.serialize_u64(v as u64) + self.serialize_i64(v as i64) } - fn serialize_usize(&mut self, v: usize) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_i8(&mut self, v: i8) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_i8(&mut self, v: i8) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_i16(&mut self, v: i16) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_u8(&mut self, v: u8) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_i32(&mut self, v: i32) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_i16(&mut self, v: i16) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_i64(&mut self, v: i64) -> Result<()> { + self.doc = Yaml::Integer(v); + Ok(()) } - fn serialize_u16(&mut self, v: u16) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_usize(&mut self, v: usize) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_i32(&mut self, v: i32) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_u8(&mut self, v: u8) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_u32(&mut self, v: u32) -> Result<()> { - self.serialize_u64(v as u64) + fn serialize_u16(&mut self, v: u16) -> Result<()> { + self.serialize_i64(v as i64) } - fn serialize_i64(&mut self, v: i64) -> Result<()> { - self.doc = Yaml::Integer(v); - Ok(()) + fn serialize_u32(&mut self, v: u32) -> Result<()> { + self.serialize_i64(v as i64) } fn serialize_u64(&mut self, v: u64) -> Result<()> { self.serialize_i64(v as i64) } - fn serialize_f64(&mut self, v: f64) -> Result<()> { + fn serialize_f32(&mut self, v: f32) -> Result<()> { self.doc = Yaml::Real(v.to_string()); Ok(()) } - fn serialize_f32(&mut self, v: f32) -> Result<()> { + fn serialize_f64(&mut self, v: f64) -> Result<()> { self.doc = Yaml::Real(v.to_string()); Ok(()) } @@ -113,6 +114,14 @@ impl ser::Serializer for Serializer { Ok(()) } + fn serialize_bytes(&mut self, value: &[u8]) -> Result<()> { + let mut state = try!(self.serialize_seq(Some(value.len()))); + for c in value { + try!(self.serialize_seq_elt(&mut state, c)); + } + self.serialize_seq_end(state) + } + fn serialize_unit(&mut self) -> Result<()> { self.doc = Yaml::Null; Ok(()) @@ -123,6 +132,40 @@ impl ser::Serializer for Serializer { Ok(()) } + fn serialize_unit_variant( + &mut self, + _name: &str, + _variant_index: usize, + variant: &str + ) -> Result<()> { + self.doc = Yaml::String(String::from(variant)); + Ok(()) + } + + fn serialize_newtype_struct( + &mut self, + _name: &'static str, + value: T + ) -> Result<()> + where T: ser::Serialize, + { + value.serialize(self) + } + + fn serialize_newtype_variant( + &mut self, + _name: &str, + _variant_index: usize, + variant: &str, + value: T + ) -> Result<()> + where T: ser::Serialize, + { + self.doc = singleton_hash(try!(to_yaml(variant)), + try!(to_yaml(value))); + Ok(()) + } + fn serialize_none(&mut self) -> Result<()> { self.serialize_unit() } @@ -133,10 +176,6 @@ impl ser::Serializer for Serializer { value.serialize(self) } - fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<()> { - self.serialize_seq(Some(len)) - } - fn serialize_seq(&mut self, len: Option) -> Result<()> { let vec = match len { None => yaml::Array::new(), @@ -146,11 +185,7 @@ impl ser::Serializer for Serializer { Ok(()) } - fn serialize_seq_end(&mut self, _: ()) -> Result<()> { - Ok(()) - } - - fn serialize_seq_elt(&mut self, _: &mut(), elem: T) -> Result<()> + fn serialize_seq_elt(&mut self, _state: &mut(), elem: T) -> Result<()> where T: ser::Serialize, { if let Yaml::Array(ref mut vec) = self.doc { @@ -161,12 +196,16 @@ impl ser::Serializer for Serializer { Ok(()) } - fn serialize_tuple(&mut self, len: usize) -> Result<()> { + fn serialize_seq_end(&mut self, _state: ()) -> Result<()> { + Ok(()) + } + + fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<()> { self.serialize_seq(Some(len)) } - fn serialize_tuple_end(&mut self, _: ()) -> Result<()> { - Ok(()) + fn serialize_tuple(&mut self, len: usize) -> Result<()> { + self.serialize_seq(Some(len)) } fn serialize_tuple_elt(&mut self, state: &mut(), elem: T) -> Result<()> @@ -175,90 +214,22 @@ impl ser::Serializer for Serializer { self.serialize_seq_elt(state, elem) } - fn serialize_map(&mut self, _len: Option) -> Result<()> - { - self.doc = Yaml::Hash(yaml::Hash::new()); - Ok(()) - } - - fn serialize_map_end(&mut self, _: ()) -> Result<()> { + fn serialize_tuple_end(&mut self, _state: ()) -> Result<()> { Ok(()) } - fn serialize_map_elt(&mut self, _: &mut (), key: K, value: V) -> Result<()> - where K: ser::Serialize, - V: ser::Serialize, - { - if let Yaml::Hash(ref mut map) = self.doc { - map.insert(try!(to_yaml(key)), try!(to_yaml(value))); - } else { - panic!("bad call to serialize_map_elt"); - } - Ok(()) - } - - fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<()> - { - self.serialize_map(Some(len)) - } - - fn serialize_struct_end(&mut self, _: ()) -> Result<()> { - Ok(()) - } - - fn serialize_struct_elt(&mut self, state: &mut (), key: &'static str, value: V) -> Result<()> - where V: ser::Serialize, - { - self.serialize_map_elt(state, key, value) - } - fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<()> { self.serialize_tuple(len) } - fn serialize_tuple_struct_end(&mut self, _: ()) -> Result<()> { - Ok(()) - } - fn serialize_tuple_struct_elt(&mut self, state: &mut (), value: V) -> Result<()> where V: ser::Serialize, { self.serialize_tuple_elt(state, value) } - fn serialize_unit_variant( - &mut self, - _name: &str, - _variant_index: usize, - variant: &str - ) -> Result<()> { - self.doc = Yaml::String(String::from(variant)); - Ok(()) - } - - /// Override `serialize_newtype_struct` to serialize newtypes without an object wrapper. - fn serialize_newtype_struct( - &mut self, - _name: &'static str, - value: T - ) -> Result<()> - where T: ser::Serialize, - { - value.serialize(self) - } - - fn serialize_newtype_variant( - &mut self, - _name: &str, - _variant_index: usize, - variant: &str, - value: T - ) -> Result<()> - where T: ser::Serialize, - { - self.doc = singleton_hash(try!(to_yaml(variant)), - try!(to_yaml(value))); + fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<()> { Ok(()) } @@ -273,7 +244,7 @@ impl ser::Serializer for Serializer { Ok(variant) } - fn serialize_tuple_variant_elt(&mut self, _: &mut &'static str, v: V) -> Result<()> { + fn serialize_tuple_variant_elt(&mut self, _state: &mut &'static str, v: V) -> Result<()> { self.serialize_tuple_elt(&mut (), v) } @@ -285,6 +256,43 @@ impl ser::Serializer for Serializer { Ok(()) } + fn serialize_map(&mut self, _len: Option) -> Result<()> + { + self.doc = Yaml::Hash(yaml::Hash::new()); + Ok(()) + } + + fn serialize_map_elt(&mut self, _state: &mut (), key: K, value: V) -> Result<()> + where K: ser::Serialize, + V: ser::Serialize, + { + if let Yaml::Hash(ref mut map) = self.doc { + map.insert(try!(to_yaml(key)), try!(to_yaml(value))); + } else { + panic!("bad call to serialize_map_elt"); + } + Ok(()) + } + + fn serialize_map_end(&mut self, _state: ()) -> Result<()> { + Ok(()) + } + + fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<()> + { + self.serialize_map(Some(len)) + } + + fn serialize_struct_elt(&mut self, state: &mut (), key: &'static str, value: V) -> Result<()> + where V: ser::Serialize, + { + self.serialize_map_elt(state, key, value) + } + + fn serialize_struct_end(&mut self, _state: ()) -> Result<()> { + Ok(()) + } + fn serialize_struct_variant( &mut self, _enum: &'static str, @@ -296,7 +304,7 @@ impl ser::Serializer for Serializer { Ok(variant) } - fn serialize_struct_variant_elt(&mut self, _: &mut &'static str, field: &'static str, v: V) -> Result<()> { + fn serialize_struct_variant_elt(&mut self, _state: &mut &'static str, field: &'static str, v: V) -> Result<()> { self.serialize_struct_elt(&mut (), field, v) } @@ -307,14 +315,6 @@ impl ser::Serializer for Serializer { self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } - - fn serialize_bytes(&mut self, value: &[u8]) -> Result<()> { - let mut state = try!(self.serialize_seq(Some(value.len()))); - for c in value { - try!(self.serialize_seq_elt(&mut state, c)); - } - self.serialize_seq_end(state) - } } pub fn to_writer(writer: &mut W, value: &T) -> Result<()> From 032bee8d96ca19250b57b1aca628c171a4d75e59 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 09:47:48 -0700 Subject: [PATCH 07/11] Import std::mem --- yaml/src/ser.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index c0447609..5f3791ec 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -10,8 +10,7 @@ //! //! This module provides YAML serialization with the type `Serializer`. -use std::fmt; -use std::io; +use std::{fmt, io, mem}; use yaml_rust::{Yaml, YamlEmitter}; use yaml_rust::yaml; @@ -252,7 +251,7 @@ impl ser::Serializer for Serializer { &mut self, variant: &'static str, ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } @@ -312,7 +311,7 @@ impl ser::Serializer for Serializer { &mut self, variant: &'static str, ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), ::std::mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } } From 5cc36d984093aed887437af047600d65ab411e82 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 09:48:55 -0700 Subject: [PATCH 08/11] Run cargo fmt --- yaml/src/ser.rs | 71 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index 5f3791ec..e5ae5e8b 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -160,8 +160,7 @@ impl ser::Serializer for Serializer { ) -> Result<()> where T: ser::Serialize, { - self.doc = singleton_hash(try!(to_yaml(variant)), - try!(to_yaml(value))); + self.doc = singleton_hash(try!(to_yaml(variant)), try!(to_yaml(value))); Ok(()) } @@ -184,7 +183,7 @@ impl ser::Serializer for Serializer { Ok(()) } - fn serialize_seq_elt(&mut self, _state: &mut(), elem: T) -> Result<()> + fn serialize_seq_elt(&mut self, _state: &mut (), elem: T) -> Result<()> where T: ser::Serialize, { if let Yaml::Array(ref mut vec) = self.doc { @@ -207,7 +206,7 @@ impl ser::Serializer for Serializer { self.serialize_seq(Some(len)) } - fn serialize_tuple_elt(&mut self, state: &mut(), elem: T) -> Result<()> + fn serialize_tuple_elt(&mut self, state: &mut (), elem: T) -> Result<()> where T: ser::Serialize, { self.serialize_seq_elt(state, elem) @@ -217,12 +216,19 @@ impl ser::Serializer for Serializer { Ok(()) } - fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<()> - { + fn serialize_tuple_struct( + &mut self, + _name: &'static str, + len: usize + ) -> Result<()> { self.serialize_tuple(len) } - fn serialize_tuple_struct_elt(&mut self, state: &mut (), value: V) -> Result<()> + fn serialize_tuple_struct_elt( + &mut self, + state: &mut (), + value: V + ) -> Result<()> where V: ser::Serialize, { self.serialize_tuple_elt(state, value) @@ -237,31 +243,40 @@ impl ser::Serializer for Serializer { _enum: &'static str, _idx: usize, variant: &'static str, - len: usize, + len: usize ) -> Result<&'static str> { try!(self.serialize_tuple(len)); Ok(variant) } - fn serialize_tuple_variant_elt(&mut self, _state: &mut &'static str, v: V) -> Result<()> { + fn serialize_tuple_variant_elt( + &mut self, + _state: &mut &'static str, + v: V + ) -> Result<()> { self.serialize_tuple_elt(&mut (), v) } fn serialize_tuple_variant_end( &mut self, - variant: &'static str, + variant: &'static str ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), + mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } - fn serialize_map(&mut self, _len: Option) -> Result<()> - { + fn serialize_map(&mut self, _len: Option) -> Result<()> { self.doc = Yaml::Hash(yaml::Hash::new()); Ok(()) } - fn serialize_map_elt(&mut self, _state: &mut (), key: K, value: V) -> Result<()> + fn serialize_map_elt( + &mut self, + _state: &mut (), + key: K, + value: V + ) -> Result<()> where K: ser::Serialize, V: ser::Serialize, { @@ -277,12 +292,20 @@ impl ser::Serializer for Serializer { Ok(()) } - fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<()> - { + fn serialize_struct( + &mut self, + _name: &'static str, + len: usize + ) -> Result<()> { self.serialize_map(Some(len)) } - fn serialize_struct_elt(&mut self, state: &mut (), key: &'static str, value: V) -> Result<()> + fn serialize_struct_elt( + &mut self, + state: &mut (), + key: &'static str, + value: V + ) -> Result<()> where V: ser::Serialize, { self.serialize_map_elt(state, key, value) @@ -297,21 +320,27 @@ impl ser::Serializer for Serializer { _enum: &'static str, _idx: usize, variant: &'static str, - len: usize, + len: usize ) -> Result<&'static str> { try!(self.serialize_struct(variant, len)); Ok(variant) } - fn serialize_struct_variant_elt(&mut self, _state: &mut &'static str, field: &'static str, v: V) -> Result<()> { + fn serialize_struct_variant_elt( + &mut self, + _state: &mut &'static str, + field: &'static str, + v: V + ) -> Result<()> { self.serialize_struct_elt(&mut (), field, v) } fn serialize_struct_variant_end( &mut self, - variant: &'static str, + variant: &'static str ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), mem::replace(&mut self.doc, Yaml::Null)); + self.doc = singleton_hash(try!(to_yaml(variant)), + mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } } From c06760fef5bfac4177d6436fb438829485f8883b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 10:26:59 -0700 Subject: [PATCH 09/11] Use serializer state to build Array and Hash --- yaml/src/ser.rs | 109 ++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index e5ae5e8b..9eb91bb2 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -39,13 +39,13 @@ impl Serializer { impl ser::Serializer for Serializer { type Error = Error; - type SeqState = (); - type TupleState = (); - type TupleStructState = (); - type TupleVariantState = &'static str; - type MapState = (); - type StructState = (); - type StructVariantState = &'static str; + type SeqState = yaml::Array; + type TupleState = yaml::Array; + type TupleStructState = yaml::Array; + type TupleVariantState = (&'static str, yaml::Array); + type MapState = yaml::Hash; + type StructState = yaml::Hash; + type StructVariantState = (&'static str, yaml::Hash); fn serialize_bool(&mut self, v: bool) -> Result<()> { self.doc = Yaml::Boolean(v); @@ -174,68 +174,63 @@ impl ser::Serializer for Serializer { value.serialize(self) } - fn serialize_seq(&mut self, len: Option) -> Result<()> { - let vec = match len { + fn serialize_seq(&mut self, len: Option) -> Result { + Ok(match len { None => yaml::Array::new(), Some(len) => yaml::Array::with_capacity(len), - }; - self.doc = Yaml::Array(vec); - Ok(()) + }) } - fn serialize_seq_elt(&mut self, _state: &mut (), elem: T) -> Result<()> + fn serialize_seq_elt(&mut self, state: &mut yaml::Array, elem: T) -> Result<()> where T: ser::Serialize, { - if let Yaml::Array(ref mut vec) = self.doc { - vec.push(try!(to_yaml(elem))); - } else { - panic!("bad call to serialize_seq_elt"); - } + state.push(try!(to_yaml(elem))); Ok(()) } - fn serialize_seq_end(&mut self, _state: ()) -> Result<()> { + fn serialize_seq_end(&mut self, state: yaml::Array) -> Result<()> { + self.doc = Yaml::Array(state); Ok(()) } - fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<()> { + fn serialize_seq_fixed_size(&mut self, len: usize) -> Result { self.serialize_seq(Some(len)) } - fn serialize_tuple(&mut self, len: usize) -> Result<()> { + fn serialize_tuple(&mut self, len: usize) -> Result { self.serialize_seq(Some(len)) } - fn serialize_tuple_elt(&mut self, state: &mut (), elem: T) -> Result<()> + fn serialize_tuple_elt(&mut self, state: &mut yaml::Array, elem: T) -> Result<()> where T: ser::Serialize, { self.serialize_seq_elt(state, elem) } - fn serialize_tuple_end(&mut self, _state: ()) -> Result<()> { - Ok(()) + fn serialize_tuple_end(&mut self, state: yaml::Array) -> Result<()> { + self.serialize_seq_end(state) } fn serialize_tuple_struct( &mut self, _name: &'static str, len: usize - ) -> Result<()> { - self.serialize_tuple(len) + ) -> Result { + self.serialize_seq(Some(len)) } fn serialize_tuple_struct_elt( &mut self, - state: &mut (), + state: &mut yaml::Array, value: V ) -> Result<()> where V: ser::Serialize, { - self.serialize_tuple_elt(state, value) + self.serialize_seq_elt(state, value) } - fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<()> { - Ok(()) + fn serialize_tuple_struct_end(&mut self, state: yaml::Array) -> Result<()> { + self.serialize_seq_end(state) } fn serialize_tuple_variant( @@ -244,51 +239,48 @@ impl ser::Serializer for Serializer { _idx: usize, variant: &'static str, len: usize - ) -> Result<&'static str> { - try!(self.serialize_tuple(len)); - Ok(variant) + ) -> Result<(&'static str, yaml::Array)> { + let state = try!(self.serialize_seq(Some(len))); + Ok((variant, state)) } fn serialize_tuple_variant_elt( &mut self, - _state: &mut &'static str, + state: &mut (&'static str, yaml::Array), v: V ) -> Result<()> { - self.serialize_tuple_elt(&mut (), v) + self.serialize_seq_elt(&mut state.1, v) } fn serialize_tuple_variant_end( &mut self, - variant: &'static str + state: (&'static str, yaml::Array) ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), + try!(self.serialize_seq_end(state.1)); + self.doc = singleton_hash(try!(to_yaml(state.0)), mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } - fn serialize_map(&mut self, _len: Option) -> Result<()> { - self.doc = Yaml::Hash(yaml::Hash::new()); - Ok(()) + fn serialize_map(&mut self, _len: Option) -> Result { + Ok(yaml::Hash::new()) } fn serialize_map_elt( &mut self, - _state: &mut (), + state: &mut yaml::Hash, key: K, value: V ) -> Result<()> where K: ser::Serialize, V: ser::Serialize, { - if let Yaml::Hash(ref mut map) = self.doc { - map.insert(try!(to_yaml(key)), try!(to_yaml(value))); - } else { - panic!("bad call to serialize_map_elt"); - } + state.insert(try!(to_yaml(key)), try!(to_yaml(value))); Ok(()) } - fn serialize_map_end(&mut self, _state: ()) -> Result<()> { + fn serialize_map_end(&mut self, state: yaml::Hash) -> Result<()> { + self.doc = Yaml::Hash(state); Ok(()) } @@ -296,13 +288,13 @@ impl ser::Serializer for Serializer { &mut self, _name: &'static str, len: usize - ) -> Result<()> { + ) -> Result { self.serialize_map(Some(len)) } fn serialize_struct_elt( &mut self, - state: &mut (), + state: &mut yaml::Hash, key: &'static str, value: V ) -> Result<()> @@ -311,8 +303,8 @@ impl ser::Serializer for Serializer { self.serialize_map_elt(state, key, value) } - fn serialize_struct_end(&mut self, _state: ()) -> Result<()> { - Ok(()) + fn serialize_struct_end(&mut self, state: yaml::Hash) -> Result<()> { + self.serialize_map_end(state) } fn serialize_struct_variant( @@ -321,25 +313,26 @@ impl ser::Serializer for Serializer { _idx: usize, variant: &'static str, len: usize - ) -> Result<&'static str> { - try!(self.serialize_struct(variant, len)); - Ok(variant) + ) -> Result<(&'static str, yaml::Hash)> { + let state = try!(self.serialize_map(Some(len))); + Ok((variant, state)) } fn serialize_struct_variant_elt( &mut self, - _state: &mut &'static str, + state: &mut (&'static str, yaml::Hash), field: &'static str, v: V ) -> Result<()> { - self.serialize_struct_elt(&mut (), field, v) + self.serialize_map_elt(&mut state.1, field, v) } fn serialize_struct_variant_end( &mut self, - variant: &'static str + state: (&'static str, yaml::Hash) ) -> Result<()> { - self.doc = singleton_hash(try!(to_yaml(variant)), + try!(self.serialize_map_end(state.1)); + self.doc = singleton_hash(try!(to_yaml(state.0)), mem::replace(&mut self.doc, Yaml::Null)); Ok(()) } From afd41d8be45878d1dfbd04042b784ee8d5321800 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 10:30:20 -0700 Subject: [PATCH 10/11] Be consistent about using `where` clause --- yaml/src/ser.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index 9eb91bb2..c94e4924 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -244,11 +244,13 @@ impl ser::Serializer for Serializer { Ok((variant, state)) } - fn serialize_tuple_variant_elt( + fn serialize_tuple_variant_elt( &mut self, state: &mut (&'static str, yaml::Array), v: V - ) -> Result<()> { + ) -> Result<()> + where V: ser::Serialize, + { self.serialize_seq_elt(&mut state.1, v) } @@ -318,12 +320,14 @@ impl ser::Serializer for Serializer { Ok((variant, state)) } - fn serialize_struct_variant_elt( + fn serialize_struct_variant_elt( &mut self, state: &mut (&'static str, yaml::Hash), field: &'static str, v: V - ) -> Result<()> { + ) -> Result<()> + where V: ser::Serialize, + { self.serialize_map_elt(&mut state.1, field, v) } From f144016758fea707716bb7ca1f4f2f6232454a9a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Jul 2016 10:31:04 -0700 Subject: [PATCH 11/11] Cargo fmt again --- yaml/src/ser.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/yaml/src/ser.rs b/yaml/src/ser.rs index c94e4924..d6111671 100644 --- a/yaml/src/ser.rs +++ b/yaml/src/ser.rs @@ -181,7 +181,11 @@ impl ser::Serializer for Serializer { }) } - fn serialize_seq_elt(&mut self, state: &mut yaml::Array, elem: T) -> Result<()> + fn serialize_seq_elt( + &mut self, + state: &mut yaml::Array, + elem: T + ) -> Result<()> where T: ser::Serialize, { state.push(try!(to_yaml(elem))); @@ -201,7 +205,11 @@ impl ser::Serializer for Serializer { self.serialize_seq(Some(len)) } - fn serialize_tuple_elt(&mut self, state: &mut yaml::Array, elem: T) -> Result<()> + fn serialize_tuple_elt( + &mut self, + state: &mut yaml::Array, + elem: T + ) -> Result<()> where T: ser::Serialize, { self.serialize_seq_elt(state, elem)