From 9960d5af2d853613ea8a7d26cf2d000358d09226 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 28 Jan 2017 17:09:10 +0100 Subject: Update to serde 0.9.0 (fixes #7) --- src/ser/part.rs | 311 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 src/ser/part.rs (limited to 'src/ser/part.rs') diff --git a/src/ser/part.rs b/src/ser/part.rs new file mode 100644 index 0000000..a865450 --- /dev/null +++ b/src/ser/part.rs @@ -0,0 +1,311 @@ +use ser::Error; +use serde::ser; +use std::str; + +pub struct PartSerializer { + sink: S, +} + +impl PartSerializer { + pub fn new(sink: S) -> Self { + PartSerializer { sink: sink } + } +} + +pub trait Sink: Sized { + type Ok; + + fn serialize_bool(self, value: bool) -> Result; + + fn serialize_static_str(self, + value: &'static str) + -> Result; + + fn serialize_str(self, value: &str) -> Result; + fn serialize_string(self, value: String) -> Result; + fn serialize_none(self) -> Result; + + fn serialize_some + (self, + value: &T) + -> Result; + + fn unsupported(self) -> Error; +} + +impl ser::Serializer for PartSerializer { + type Ok = S::Ok; + type Error = Error; + type SerializeSeq = Self; + type SerializeTuple = Self; + type SerializeTupleStruct = Self; + type SerializeTupleVariant = Self; + type SerializeMap = Self; + type SerializeStruct = Self; + type SerializeStructVariant = Self; + + fn serialize_bool(self, v: bool) -> Result { + self.sink.serialize_bool(v) + } + + fn serialize_i8(self, v: i8) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_i16(self, v: i16) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_i32(self, v: i32) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_i64(self, v: i64) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_u8(self, v: u8) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_u16(self, v: u16) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_u32(self, v: u32) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_u64(self, v: u64) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_f32(self, v: f32) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_f64(self, v: f64) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_char(self, v: char) -> Result { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_str(self, value: &str) -> Result { + self.sink.serialize_str(value) + } + + fn serialize_bytes(self, value: &[u8]) -> Result { + match str::from_utf8(value) { + Ok(value) => self.sink.serialize_str(value), + Err(err) => Err(Error::Utf8(err)), + } + } + + fn serialize_unit(self) -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_unit_struct(self, name: &'static str) -> Result { + self.sink.serialize_static_str(name.into()) + } + + fn serialize_unit_variant(self, + _name: &'static str, + _variant_index: usize, + variant: &'static str) + -> Result { + self.sink.serialize_static_str(variant.into()) + } + + fn serialize_newtype_struct + (self, + _name: &'static str, + value: &T) + -> Result { + value.serialize(self) + } + + fn serialize_newtype_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: &T) + -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_none(self) -> Result { + self.sink.serialize_none() + } + + fn serialize_some(self, + value: &T) + -> Result { + self.sink.serialize_some(value) + } + + fn serialize_seq(self, _len: Option) -> Result { + Err(self.sink.unsupported()) + } + + + fn serialize_seq_fixed_size(self, _len: usize) -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_tuple_struct(self, + _name: &'static str, + _len: usize) + -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_tuple_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_map(self, _len: Option) -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_struct(self, + _name: &'static str, + _len: usize) + -> Result { + Err(self.sink.unsupported()) + } + + fn serialize_struct_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { + Err(self.sink.unsupported()) + } +} + +impl ser::SerializeSeq for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeTuple for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeTupleStruct for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeTupleVariant for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeMap for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_key(&mut self, + _key: &T) + -> Result<(), Error> { + unreachable!() + } + + fn serialize_value(&mut self, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeStruct for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} + +impl ser::SerializeStructVariant for PartSerializer { + type Ok = S::Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + unreachable!() + } + + fn end(self) -> Result { + unreachable!() + } +} -- cgit v1.2.3 From e140c2b5c5bb5abab7fcc6a9d4540aaa3bf566a6 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 28 Jan 2017 23:52:52 +0100 Subject: Use void for the unreachable serializers --- Cargo.toml | 1 + src/lib.rs | 1 + src/ser/mod.rs | 36 ++++++------ src/ser/pair.rs | 146 +++++++---------------------------------------- src/ser/part.rs | 174 ++++++++++++-------------------------------------------- src/ser/void.rs | 122 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+), 282 deletions(-) create mode 100644 src/ser/void.rs (limited to 'src/ser/part.rs') diff --git a/Cargo.toml b/Cargo.toml index b9c18f0..a68819a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ test = false [dependencies] serde = "0.9.2" url = "1.0.0" +void = "1.0.2" diff --git a/src/lib.rs b/src/lib.rs index bacb67f..0c1a99e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #[macro_use] extern crate serde; extern crate url; +extern crate void; pub mod de; pub mod ser; diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 400846f..50355fd 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -4,12 +4,12 @@ mod key; mod pair; mod part; mod value; +mod void; use serde::ser; use std::borrow::Cow; use std::error; use std::fmt; -use std::marker::PhantomData; use std::str; use url::form_urlencoded::Serializer as UrlEncodedSerializer; use url::form_urlencoded::Target as UrlEncodedTarget; @@ -102,21 +102,21 @@ pub struct SeqSerializer<'output, Target: 'output + UrlEncodedTarget> { /// /// Never instantiated, tuples are not supported at top-level. pub struct TupleSerializer<'output, T: 'output + UrlEncodedTarget> { - _marker: PhantomData<&'output T>, + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer>, } /// Tuple struct serializer. /// /// Never instantiated, tuple structs are not supported. pub struct TupleStructSerializer<'output, T: 'output + UrlEncodedTarget> { - _marker: PhantomData<&'output T>, + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer>, } /// Tuple variant serializer. /// /// Never instantiated, tuple variants are not supported. pub struct TupleVariantSerializer<'output, T: 'output + UrlEncodedTarget> { - _marker: PhantomData<&'output T>, + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer>, } /// Map serializer. @@ -134,7 +134,7 @@ pub struct StructSerializer<'output, Target: 'output + UrlEncodedTarget> { /// /// Never instantiated, struct variants are not supported. pub struct StructVariantSerializer<'output, T: 'output + UrlEncodedTarget> { - _marker: PhantomData<&'output T>, + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer>, } impl<'output, Target> ser::Serializer for Serializer<'output, Target> @@ -368,13 +368,13 @@ impl<'output, Target> ser::SerializeTuple for TupleSerializer<'output, Target> type Error = Error; fn serialize_element(&mut self, - _value: &T) + value: &T) -> Result<(), Error> { - unreachable!() + self.inner.serialize_element(value) } fn end(self) -> Result { - unreachable!() + self.inner.end() } } @@ -386,13 +386,13 @@ impl<'output, Target> ser::SerializeTupleStruct type Error = Error; fn serialize_field(&mut self, - _value: &T) + value: &T) -> Result<(), Error> { - unreachable!() + self.inner.serialize_field(value) } fn end(self) -> Result { - unreachable!() + self.inner.end() } } @@ -404,13 +404,13 @@ impl<'output, Target> ser::SerializeTupleVariant type Error = Error; fn serialize_field(&mut self, - _value: &T) + value: &T) -> Result<(), Error> { - unreachable!() + self.inner.serialize_field(value) } fn end(self) -> Result { - unreachable!() + self.inner.end() } } @@ -488,14 +488,14 @@ impl<'output, Target> ser::SerializeStructVariant type Error = Error; fn serialize_field(&mut self, - _key: &'static str, - _value: &T) + key: &'static str, + value: &T) -> Result<(), Error> { - unreachable!() + self.inner.serialize_field(key, value) } fn end(self) -> Result { - unreachable!() + self.inner.end() } } diff --git a/src/ser/pair.rs b/src/ser/pair.rs index 3d91637..743b3dd 100644 --- a/src/ser/pair.rs +++ b/src/ser/pair.rs @@ -2,6 +2,7 @@ use ser::Error; use ser::key::KeySink; use ser::part::PartSerializer; use ser::value::ValueSink; +use ser::void::VoidSerializer; use serde::ser; use std::borrow::Cow; use std::mem; @@ -29,13 +30,13 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> { type Ok = (); type Error = Error; - type SerializeSeq = Self; + type SerializeSeq = VoidSerializer<()>; type SerializeTuple = Self; - type SerializeTupleStruct = Self; - type SerializeTupleVariant = Self; - type SerializeMap = Self; - type SerializeStruct = Self; - type SerializeStructVariant = Self; + type SerializeTupleStruct = VoidSerializer<()>; + type SerializeTupleVariant = VoidSerializer<()>; + type SerializeMap = VoidSerializer<()>; + type SerializeStruct = VoidSerializer<()>; + type SerializeStructVariant = VoidSerializer<()>; fn serialize_bool(self, _v: bool) -> Result<(), Error> { Err(Error::unsupported_pair()) @@ -137,11 +138,15 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> value.serialize(self) } - fn serialize_seq(self, _len: Option) -> Result { + fn serialize_seq(self, + _len: Option) + -> Result, Error> { Err(Error::unsupported_pair()) } - fn serialize_seq_fixed_size(self, _len: usize) -> Result { + fn serialize_seq_fixed_size(self, + _len: usize) + -> Result, Error> { Err(Error::unsupported_pair()) } @@ -156,7 +161,7 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> fn serialize_tuple_struct(self, _name: &'static str, _len: usize) - -> Result { + -> Result, Error> { Err(Error::unsupported_pair()) } @@ -165,18 +170,20 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> _variant_index: usize, _variant: &'static str, _len: usize) - -> Result { + -> Result, Error> { Err(Error::unsupported_pair()) } - fn serialize_map(self, _len: Option) -> Result { + fn serialize_map(self, + _len: Option) + -> Result, Error> { Err(Error::unsupported_pair()) } fn serialize_struct(self, _name: &'static str, _len: usize) - -> Result { + -> Result, Error> { Err(Error::unsupported_pair()) } @@ -185,28 +192,11 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> _variant_index: usize, _variant: &'static str, _len: usize) - -> Result { + -> Result, Error> { Err(Error::unsupported_pair()) } } -impl<'target, Target> ser::SerializeSeq for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_element(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target> where Target: 'target + UrlEncodedTarget, { @@ -251,102 +241,6 @@ impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target> } } -impl<'target, Target> ser::SerializeTupleStruct - for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_field(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - -impl<'target, Target> ser::SerializeTupleVariant - for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_field(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - -impl<'target, Target> ser::SerializeMap for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_key(&mut self, - _key: &T) - -> Result<(), Error> { - unreachable!() - } - - fn serialize_value(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - -impl<'target, Target> ser::SerializeStruct for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_field(&mut self, - _key: &'static str, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - -impl<'target, Target> ser::SerializeStructVariant - for PairSerializer<'target, Target> - where Target: 'target + UrlEncodedTarget, -{ - type Ok = (); - type Error = Error; - - fn serialize_field(&mut self, - _key: &'static str, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result<(), Error> { - unreachable!() - } -} - enum PairState { WaitingForKey, WaitingForValue { key: Cow<'static, str> }, diff --git a/src/ser/part.rs b/src/ser/part.rs index a865450..097504e 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -1,4 +1,5 @@ use ser::Error; +use ser::void::VoidSerializer; use serde::ser; use std::str; @@ -36,13 +37,13 @@ pub trait Sink: Sized { impl ser::Serializer for PartSerializer { type Ok = S::Ok; type Error = Error; - type SerializeSeq = Self; - type SerializeTuple = Self; - type SerializeTupleStruct = Self; - type SerializeTupleVariant = Self; - type SerializeMap = Self; - type SerializeStruct = Self; - type SerializeStructVariant = Self; + type SerializeSeq = VoidSerializer; + type SerializeTuple = VoidSerializer; + type SerializeTupleStruct = VoidSerializer; + type SerializeTupleVariant = VoidSerializer; + type SerializeMap = VoidSerializer; + type SerializeStruct = VoidSerializer; + type SerializeStructVariant = VoidSerializer; fn serialize_bool(self, v: bool) -> Result { self.sink.serialize_bool(v) @@ -147,165 +148,62 @@ impl ser::Serializer for PartSerializer { self.sink.serialize_some(value) } - fn serialize_seq(self, _len: Option) -> Result { + fn serialize_seq(self, + _len: Option) + -> Result { Err(self.sink.unsupported()) } - fn serialize_seq_fixed_size(self, _len: usize) -> Result { + fn serialize_seq_fixed_size(self, + _len: usize) + -> Result { Err(self.sink.unsupported()) } - fn serialize_tuple(self, _len: usize) -> Result { + fn serialize_tuple(self, + _len: usize) + -> Result { Err(self.sink.unsupported()) } fn serialize_tuple_struct(self, _name: &'static str, _len: usize) - -> Result { + -> Result { Err(self.sink.unsupported()) } - fn serialize_tuple_variant(self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { Err(self.sink.unsupported()) } - fn serialize_map(self, _len: Option) -> Result { + fn serialize_map(self, + _len: Option) + -> Result { Err(self.sink.unsupported()) } fn serialize_struct(self, _name: &'static str, _len: usize) - -> Result { + -> Result { Err(self.sink.unsupported()) } - fn serialize_struct_variant(self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_struct_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { Err(self.sink.unsupported()) } } - -impl ser::SerializeSeq for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_element(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeTuple for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_element(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeTupleStruct for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_field(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeTupleVariant for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_field(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeMap for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_key(&mut self, - _key: &T) - -> Result<(), Error> { - unreachable!() - } - - fn serialize_value(&mut self, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeStruct for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_field(&mut self, - _key: &'static str, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - -impl ser::SerializeStructVariant for PartSerializer { - type Ok = S::Ok; - type Error = Error; - - fn serialize_field(&mut self, - _key: &'static str, - _value: &T) - -> Result<(), Error> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} diff --git a/src/ser/void.rs b/src/ser/void.rs new file mode 100644 index 0000000..4765bb7 --- /dev/null +++ b/src/ser/void.rs @@ -0,0 +1,122 @@ +use ser::Error; +use serde::ser; +use std::marker::PhantomData; +use void; + +pub struct VoidSerializer { + void: void::Void, + _marker: PhantomData, +} + +impl ser::SerializeSeq for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTuple for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTupleStruct for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTupleVariant for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeMap for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_key(&mut self, + _key: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn serialize_value(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeStruct for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeStructVariant for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} -- cgit v1.2.3 From 1d88bbb26b9caaa90993845042e21af444e9399a Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 28 Jan 2017 23:56:15 +0100 Subject: Use itoa to not allocate a String to serialize an integer --- Cargo.toml | 1 + src/lib.rs | 1 + src/ser/part.rs | 28 ++++++++++++++++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) (limited to 'src/ser/part.rs') diff --git a/Cargo.toml b/Cargo.toml index a68819a..e7865a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ keywords = ["serde", "serialization", "urlencoded"] test = false [dependencies] +itoa = "0.3.0" serde = "0.9.2" url = "1.0.0" void = "1.0.2" diff --git a/src/lib.rs b/src/lib.rs index 0c1a99e..7a3be26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ //! `x-www-form-urlencoded` meets Serde +extern crate itoa; #[macro_use] extern crate serde; extern crate url; diff --git a/src/ser/part.rs b/src/ser/part.rs index 097504e..33811f4 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -1,3 +1,4 @@ +use itoa; use ser::Error; use ser::void::VoidSerializer; use serde::ser; @@ -50,35 +51,35 @@ impl ser::Serializer for PartSerializer { } fn serialize_i8(self, v: i8) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_i16(self, v: i16) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_i32(self, v: i32) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_i64(self, v: i64) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_u8(self, v: u8) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_u16(self, v: u16) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_u32(self, v: u32) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_u64(self, v: u64) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_integer(v) } fn serialize_f32(self, v: f32) -> Result { @@ -207,3 +208,14 @@ impl ser::Serializer for PartSerializer { Err(self.sink.unsupported()) } } + +impl PartSerializer { + fn serialize_integer(self, value: I) -> Result + where I: itoa::Integer + { + let mut buf = [b'\0'; 20]; + let len = itoa::write(&mut buf[..], value).unwrap(); + let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; + ser::Serializer::serialize_str(self, part) + } +} -- cgit v1.2.3 From 5625c254e55c767a1ec0235219c232f53dcf9e96 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sun, 29 Jan 2017 00:14:57 +0100 Subject: Use dtoa to not allocate a String to serialize a float --- Cargo.toml | 1 + src/lib.rs | 1 + src/ser/part.rs | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/ser/part.rs') diff --git a/Cargo.toml b/Cargo.toml index e7865a4..8566cc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ keywords = ["serde", "serialization", "urlencoded"] test = false [dependencies] +dtoa = "0.4.0" itoa = "0.3.0" serde = "0.9.2" url = "1.0.0" diff --git a/src/lib.rs b/src/lib.rs index 7a3be26..8fefd77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ //! `x-www-form-urlencoded` meets Serde extern crate itoa; +extern crate dtoa; #[macro_use] extern crate serde; extern crate url; diff --git a/src/ser/part.rs b/src/ser/part.rs index 33811f4..049bacd 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -1,3 +1,4 @@ +use dtoa; use itoa; use ser::Error; use ser::void::VoidSerializer; @@ -83,11 +84,11 @@ impl ser::Serializer for PartSerializer { } fn serialize_f32(self, v: f32) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_floating(v) } fn serialize_f64(self, v: f64) -> Result { - self.sink.serialize_string(v.to_string()) + self.serialize_floating(v) } fn serialize_char(self, v: char) -> Result { @@ -218,4 +219,13 @@ impl PartSerializer { let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; ser::Serializer::serialize_str(self, part) } + + fn serialize_floating(self, value: F) -> Result + where F: dtoa::Floating + { + let mut buf = [b'\0'; 24]; + let len = dtoa::write(&mut buf[..], value).unwrap(); + let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; + ser::Serializer::serialize_str(self, part) + } } -- cgit v1.2.3 From f5b1ae88282adfc2fe2253e4cd51c6ba5eca0c54 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sun, 29 Jan 2017 00:17:54 +0100 Subject: Do not forbid bool keys anymore Floats are supported so... --- src/ser/key.rs | 4 ---- src/ser/part.rs | 4 +--- src/ser/value.rs | 4 ---- 3 files changed, 1 insertion(+), 11 deletions(-) (limited to 'src/ser/part.rs') diff --git a/src/ser/key.rs b/src/ser/key.rs index 149c8f6..6cbbff4 100644 --- a/src/ser/key.rs +++ b/src/ser/key.rs @@ -46,10 +46,6 @@ impl Sink for KeySink { type Ok = Ok; - fn serialize_bool(self, _value: bool) -> Result { - Err(self.unsupported()) - } - fn serialize_static_str(self, value: &'static str) -> Result { diff --git a/src/ser/part.rs b/src/ser/part.rs index 049bacd..e720314 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -18,8 +18,6 @@ impl PartSerializer { pub trait Sink: Sized { type Ok; - fn serialize_bool(self, value: bool) -> Result; - fn serialize_static_str(self, value: &'static str) -> Result; @@ -48,7 +46,7 @@ impl ser::Serializer for PartSerializer { type SerializeStructVariant = VoidSerializer; fn serialize_bool(self, v: bool) -> Result { - self.sink.serialize_bool(v) + self.sink.serialize_static_str(if v { "true" } else { "false" }) } fn serialize_i8(self, v: i8) -> Result { diff --git a/src/ser/value.rs b/src/ser/value.rs index 34ea7d0..e4e1f2c 100644 --- a/src/ser/value.rs +++ b/src/ser/value.rs @@ -30,10 +30,6 @@ impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> { type Ok = (); - fn serialize_bool(self, value: bool) -> Result { - self.serialize_static_str(if value { "true" } else { "false" }) - } - fn serialize_str(self, value: &str) -> Result<(), Error> { self.urlencoder.append_pair(self.key, value); Ok(()) -- cgit v1.2.3