typarameterize Serializer

This commit is contained in:
Erick Tryzelaar 2014-08-31 20:44:19 -07:00
parent dbbb016ada
commit 3a6eb83da6
4 changed files with 187 additions and 232 deletions

View File

@ -72,15 +72,10 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
let trait_def = TraitDef {
span: sp,
attributes: vec!(),
path: Path::new(vec!("serde", "ser", "Serializable")),
/*
path: Path::new_(vec!("serde", "ser", "Serializable"), None,
vec!(box Literal(Path::new_local("__S")),
box Literal(Path::new_local("__E"))), true),
*/
additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(),
/*
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__S", None, vec!(Path::new_(
@ -88,32 +83,10 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
vec!(box Literal(Path::new_local("__E"))), true))),
("__E", None, vec!()))
},
*/
methods: vec!(
MethodDef {
name: "serialize",
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(
(
"__S",
None,
vec!(
Path::new_(
vec!("serde", "ser", "Serializer"),
None,
vec!(box Literal(Path::new_local("__E"))),
true
)
),
),
(
"__E",
None,
vec!(),
),
)
},
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec!(Ptr(box Literal(Path::new_local("__S")),
Borrowed(None, MutMutable))),

View File

@ -35,12 +35,9 @@ enum HttpProtocol {
HTTP11,
}
impl ser::Serializable for HttpProtocol {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for HttpProtocol {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -67,12 +64,9 @@ enum HttpMethod {
PATCH,
}
impl ser::Serializable for HttpMethod {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for HttpMethod {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -92,12 +86,9 @@ enum CacheStatus {
Hit,
}
impl ser::Serializable for CacheStatus {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for CacheStatus {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -126,12 +117,9 @@ enum OriginProtocol {
HTTPS,
}
impl ser::Serializable for OriginProtocol {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for OriginProtocol {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -152,12 +140,9 @@ enum ZonePlan {
ENT,
}
impl ser::Serializable for ZonePlan {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for ZonePlan {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -429,12 +414,9 @@ enum Country {
ZW,
}
impl ser::Serializable for Country {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Country {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_uint(*self as uint)
}
}
@ -799,12 +781,10 @@ fn bench_serializer_mem_writer(b: &mut Bencher) {
b.iter(|| {
//let _json = json::to_str(&log).unwrap();
let mut wr = MemWriter::with_capacity(1024);
{
let mut serializer = json::Serializer::new(wr.by_ref());
log.serialize(&mut serializer).unwrap();
}
let _json = wr.unwrap();
let wr = MemWriter::with_capacity(1024);
let mut serializer = json::Serializer::new(wr);
log.serialize(&mut serializer).unwrap();
let _json = serializer.unwrap().unwrap();
});
}
@ -816,12 +796,10 @@ fn bench_serializer_my_mem_writer0(b: &mut Bencher) {
b.iter(|| {
//let _json = json::to_str(&log).unwrap();
let mut wr = MyMemWriter0::with_capacity(1024);
{
let mut serializer = json::Serializer::new(wr.by_ref());
log.serialize(&mut serializer).unwrap();
}
let _json = wr.unwrap();
let wr = MyMemWriter0::with_capacity(1024);
let mut serializer = json::Serializer::new(wr);
log.serialize(&mut serializer).unwrap();
let _json = serializer.unwrap().unwrap();
});
}
@ -833,12 +811,10 @@ fn bench_serializer_my_mem_writer1(b: &mut Bencher) {
b.iter(|| {
//let _json = json::to_str(&log).unwrap();
let mut wr = MyMemWriter1::with_capacity(1024);
{
let mut serializer = json::Serializer::new(wr.by_ref());
log.serialize(&mut serializer).unwrap();
}
let _json = wr.unwrap();
let wr = MyMemWriter1::with_capacity(1024);
let mut serializer = json::Serializer::new(wr);
log.serialize(&mut serializer).unwrap();
let _json = serializer.unwrap().unwrap();
});
}
@ -850,12 +826,10 @@ fn bench_serializer_my_mem_writer2(b: &mut Bencher) {
b.iter(|| {
//let _json = json::to_str(&log).unwrap();
let mut wr = MyMemWriter2::with_capacity(1024);
{
let mut serializer = json::Serializer::new(wr.by_ref());
log.serialize(&mut serializer).unwrap();
}
let _json = wr.unwrap();
let wr = MyMemWriter2::with_capacity(1024);
let mut serializer = json::Serializer::new(wr);
log.serialize(&mut serializer).unwrap();
let _json = serializer.unwrap().unwrap();
});
}
@ -867,12 +841,10 @@ fn bench_serializer_my_mem_writer3(b: &mut Bencher) {
b.iter(|| {
//let _json = json::to_str(&log).unwrap();
let mut wr = MyMemWriter3::with_capacity(1024);
{
let mut serializer = json::Serializer::new(wr.by_ref());
log.serialize(&mut serializer).unwrap();
}
let _json = wr.unwrap();
let wr = MyMemWriter3::with_capacity(1024);
let mut serializer = json::Serializer::new(wr);
log.serialize(&mut serializer).unwrap();
let _json = serializer.unwrap().unwrap();
});
}
@ -1256,10 +1228,10 @@ fn bench_manual_my_mem_writer3_escape(b: &mut Bencher) {
});
}
fn direct<W: Writer>(mut wr: W, log: &Log) {
fn direct<W: Writer>(wr: W, log: &Log) {
use ser::Serializer;
let mut serializer = json::Serializer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr);
serializer.serialize_struct_start("Log", 12).unwrap();
serializer.serialize_struct_elt("timestamp", &log.timestamp).unwrap();

View File

@ -489,12 +489,9 @@ impl fmt::Show for Json {
}
}
impl ser::Serializable for Json {
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Json {
#[inline]
fn serialize<
S: ser::Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
match *self {
Null => {
().serialize(s)
@ -913,6 +910,11 @@ impl<W: Writer> Serializer<W> {
first: true,
}
}
/// Unwrap the Writer from the Serializer.
pub fn unwrap(self) -> W {
self.wr
}
}
impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
@ -1008,7 +1010,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_tuple_elt<
T: Serializable
T: Serializable<Serializer<W>, io::IoError>
>(&mut self, value: &T) -> IoResult<()> {
if self.first {
self.first = false;
@ -1031,7 +1033,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_struct_elt<
T: Serializable
T: Serializable<Serializer<W>, io::IoError>
>(&mut self, name: &str, value: &T) -> IoResult<()> {
if self.first {
self.first = false;
@ -1058,7 +1060,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_enum_elt<
T: Serializable
T: Serializable<Serializer<W>, io::IoError>
>(&mut self, value: &T) -> IoResult<()> {
if self.first {
self.first = false;
@ -1075,7 +1077,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_option<
T: Serializable
T: Serializable<Serializer<W>, io::IoError>
>(&mut self, v: &Option<T>) -> IoResult<()> {
match *v {
Some(ref v) => {
@ -1089,7 +1091,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_seq<
T: Serializable,
T: Serializable<Serializer<W>, io::IoError>,
Iter: Iterator<T>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("["));
@ -1108,8 +1110,8 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
#[inline]
fn serialize_map<
K: Serializable,
V: Serializable,
K: Serializable<Serializer<W>, io::IoError>,
V: Serializable<Serializer<W>, io::IoError>,
Iter: Iterator<(K, V)>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("{"));
@ -1147,6 +1149,11 @@ impl<W: Writer> PrettySerializer<W> {
}
}
/// Unwrap the Writer from the Serializer.
pub fn unwrap(self) -> W {
self.wr
}
#[inline]
fn serialize_sep(&mut self) -> IoResult<()> {
if self.first {
@ -1267,7 +1274,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_tuple_elt<
T: Serializable
T: Serializable<PrettySerializer<W>, io::IoError>
>(&mut self, value: &T) -> IoResult<()> {
try!(self.serialize_sep());
value.serialize(self)
@ -1286,7 +1293,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_struct_elt<
T: Serializable
T: Serializable<PrettySerializer<W>, io::IoError>
>(&mut self, name: &str, value: &T) -> IoResult<()> {
try!(self.serialize_sep());
try!(self.serialize_str(name));
@ -1311,7 +1318,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_enum_elt<
T: Serializable
T: Serializable<PrettySerializer<W>, io::IoError>
>(&mut self, value: &T) -> IoResult<()> {
try!(self.serialize_sep());
value.serialize(self)
@ -1325,7 +1332,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_option<
T: Serializable
T: Serializable<PrettySerializer<W>, io::IoError>
>(&mut self, v: &Option<T>) -> IoResult<()> {
match *v {
Some(ref v) => {
@ -1339,7 +1346,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_seq<
T: Serializable,
T: Serializable<PrettySerializer<W>, io::IoError>,
Iter: Iterator<T>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("["));
@ -1355,8 +1362,8 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
#[inline]
fn serialize_map<
K: Serializable,
V: Serializable,
K: Serializable<PrettySerializer<W>, io::IoError>,
V: Serializable<PrettySerializer<W>, io::IoError>,
Iter: Iterator<(K, V)>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("{"));
@ -1375,36 +1382,40 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
/// Encode the specified struct into a json `[u8]` buffer.
#[inline]
pub fn to_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
let mut wr = MemWriter::with_capacity(1024);
{
let mut serializer = Serializer::new(wr.by_ref());
// We are writing to a MemWriter, which doesn't fail. So we can ignore
// the error.
value.serialize(&mut serializer).unwrap();
}
wr.unwrap()
pub fn to_vec<
T: ser::Serializable<Serializer<MemWriter>, io::IoError>
>(value: &T) -> Vec<u8> {
let wr = MemWriter::with_capacity(1024);
let mut serializer = Serializer::new(wr);
// We are writing to a MemWriter, which doesn't fail. So we can ignore
// the error.
value.serialize(&mut serializer).unwrap();
serializer.unwrap().unwrap()
}
/// Encode the specified struct into a json `String` buffer.
#[inline]
pub fn to_string<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
pub fn to_string<
T: ser::Serializable<Serializer<MemWriter>, io::IoError>
>(value: &T) -> Result<String, Vec<u8>> {
let buf = to_vec(value);
String::from_utf8(buf)
}
/// Encode the specified struct into a json `[u8]` buffer.
pub fn to_pretty_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
let mut wr = MemWriter::new();
{
let mut serializer = PrettySerializer::new(wr.by_ref());
value.serialize(&mut serializer).unwrap();
}
wr.unwrap()
pub fn to_pretty_vec<
T: ser::Serializable<PrettySerializer<MemWriter>, io::IoError>
>(value: &T) -> Vec<u8> {
let wr = MemWriter::new();
let mut serializer = PrettySerializer::new(wr);
value.serialize(&mut serializer).unwrap();
serializer.unwrap().unwrap()
}
/// Encode the specified struct into a json `String` buffer.
pub fn to_pretty_string<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
pub fn to_pretty_string<
T: ser::Serializable<PrettySerializer<MemWriter>, io::IoError>
>(value: &T) -> Result<String, Vec<u8>> {
let buf = to_pretty_vec(value);
String::from_utf8(buf)
}
@ -2302,6 +2313,7 @@ impl<A:ToJson> ToJson for Option<A> {
#[cfg(test)]
mod tests {
use std::fmt::Show;
use std::io;
use std::str;
use std::collections::TreeMap;
@ -2321,7 +2333,7 @@ mod tests {
SyntaxError,
};
use de;
use ser::Serializable;
use ser::{Serializable, Serializer};
use ser;
macro_rules! treemap {
@ -2400,7 +2412,7 @@ mod tests {
}
fn test_encode_ok<
T: PartialEq + Show + ToJson + ser::Serializable
T: PartialEq + Show + ToJson + ser::Serializable<super::Serializer<io::MemWriter>, io::IoError>
>(errors: &[(T, &str)]) {
for &(ref value, out) in errors.iter() {
let out = out.to_string();
@ -2414,7 +2426,7 @@ mod tests {
}
fn test_pretty_encode_ok<
T: PartialEq + Show + ToJson + ser::Serializable
T: PartialEq + Show + ToJson + ser::Serializable<super::PrettySerializer<io::MemWriter>, io::IoError>
>(errors: &[(T, &str)]) {
for &(ref value, out) in errors.iter() {
let out = out.to_string();
@ -3460,7 +3472,7 @@ mod tests {
"{\"a\": 3}",
box [
(ObjectStart, box []),
(NumberValue(3.0), box [Key("a")]),
(F64Value(3.0), box [Key("a")]),
(ObjectEnd, box []),
]
);
@ -3477,7 +3489,7 @@ mod tests {
"{\"a\" : 1.0 ,\"b\": [ true ]}",
box [
(ObjectStart, box []),
(NumberValue(1.0), box [Key("a")]),
(F64Value(1.0), box [Key("a")]),
(ListStart, box [Key("b")]),
(BooleanValue(true),box [Key("b"), Index(0)]),
(ListEnd, box [Key("b")]),
@ -3495,7 +3507,7 @@ mod tests {
}"#,
~[
(ObjectStart, ~[]),
(NumberValue(1.0), ~[Key("a")]),
(F64Value(1.0), ~[Key("a")]),
(ListStart, ~[Key("b")]),
(BooleanValue(true), ~[Key("b"), Index(0)]),
(StringValue("foo\nbar".to_string()), ~[Key("b"), Index(1)]),
@ -3554,8 +3566,8 @@ mod tests {
"[3, 1]",
box [
(ListStart, box []),
(NumberValue(3.0), box [Index(0)]),
(NumberValue(1.0), box [Index(1)]),
(F64Value(3.0), box [Index(0)]),
(F64Value(1.0), box [Index(1)]),
(ListEnd, box []),
]
);
@ -3563,8 +3575,8 @@ mod tests {
"\n[3, 2]\n",
box [
(ListStart, box []),
(NumberValue(3.0), box [Index(0)]),
(NumberValue(2.0), box [Index(1)]),
(F64Value(3.0), box [Index(0)]),
(F64Value(2.0), box [Index(1)]),
(ListEnd, box []),
]
);
@ -3572,10 +3584,10 @@ mod tests {
"[2, [4, 1]]",
box [
(ListStart, box []),
(NumberValue(2.0), box [Index(0)]),
(F64Value(2.0), box [Index(0)]),
(ListStart, box [Index(1)]),
(NumberValue(4.0), box [Index(1), Index(0)]),
(NumberValue(1.0), box [Index(1), Index(1)]),
(F64Value(4.0), box [Index(1), Index(0)]),
(F64Value(1.0), box [Index(1), Index(1)]),
(ListEnd, box [Index(1)]),
(ListEnd, box []),
]
@ -3731,12 +3743,12 @@ mod bench {
list.push(json::Object(treemap!(
"a".to_string() => json::Boolean(true),
"b".to_string() => json::Null,
"c".to_string() => json::Number(3.1415),
"c".to_string() => json::F64(3.1415),
"d".to_string() => json::String("Hello world".to_string()),
"e".to_string() => json::List(vec!(
json::Number(1f64),
json::Number(2f64),
json::Number(3f64)
json::U64(1),
json::U64(2),
json::U64(3)
))
)));
}
@ -3832,7 +3844,7 @@ mod bench {
assert_eq!(parser.next(), Some(json::NullValue));
assert_eq!(parser.stack().top(), Some(json::Key("b")));
assert_eq!(parser.next(), Some(json::NumberValue(3.1415)));
assert_eq!(parser.next(), Some(json::F64Value(3.1415)));
assert_eq!(parser.stack().top(), Some(json::Key("c")));
assert_eq!(parser.next(), Some(json::StringValue("Hello world".to_string())));
@ -3840,9 +3852,9 @@ mod bench {
assert_eq!(parser.next(), Some(json::ListStart));
assert_eq!(parser.stack().top(), Some(json::Key("e")));
assert_eq!(parser.next(), Some(json::NumberValue(1f64)));
assert_eq!(parser.next(), Some(json::NumberValue(2f64)));
assert_eq!(parser.next(), Some(json::NumberValue(3f64)));
assert_eq!(parser.next(), Some(json::U64Value(1)));
assert_eq!(parser.next(), Some(json::U64Value(2)));
assert_eq!(parser.next(), Some(json::U64Value(3)));
assert_eq!(parser.next(), Some(json::ListEnd));
assert_eq!(parser.next(), Some(json::ObjectEnd));

View File

@ -79,47 +79,52 @@ pub trait Serializer<E> {
fn serialize_str(&mut self, v: &str) -> Result<(), E>;
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>;
fn serialize_tuple_elt<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
fn serialize_tuple_elt<
T: Serializable<Self, E>
>(&mut self, v: &T) -> Result<(), E>;
fn serialize_tuple_end(&mut self) -> Result<(), E>;
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>;
fn serialize_struct_elt<T: Serializable>(&mut self, name: &str, v: &T) -> Result<(), E>;
fn serialize_struct_elt<
T: Serializable<Self, E>
>(&mut self, name: &str, v: &T) -> Result<(), E>;
fn serialize_struct_end(&mut self) -> Result<(), E>;
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>;
fn serialize_enum_elt<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
fn serialize_enum_elt<
T: Serializable<Self, E>
>(&mut self, v: &T) -> Result<(), E>;
fn serialize_enum_end(&mut self) -> Result<(), E>;
fn serialize_option<T: Serializable>(&mut self, v: &Option<T>) -> Result<(), E>;
fn serialize_option<
T: Serializable<Self, E>
>(&mut self, v: &Option<T>) -> Result<(), E>;
fn serialize_seq<
T: Serializable,
T: Serializable<Self, E>,
Iter: Iterator<T>
>(&mut self, iter: Iter) -> Result<(), E>;
fn serialize_map<
K: Serializable,
V: Serializable,
K: Serializable<Self, E>,
V: Serializable<Self, E>,
Iter: Iterator<(K, V)>
>(&mut self, iter: Iter) -> Result<(), E>;
}
//////////////////////////////////////////////////////////////////////////////
pub trait Serializable {
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E>;
pub trait Serializable<S: Serializer<E>, E> {
fn serialize(&self, s: &mut S) -> Result<(), E>;
}
//////////////////////////////////////////////////////////////////////////////
macro_rules! impl_serializable {
($ty:ty, $method:ident) => {
impl Serializable for $ty {
impl<S: Serializer<E>, E> Serializable<S, E> for $ty {
#[inline]
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.$method(*self)
}
}
@ -143,22 +148,16 @@ impl_serializable!(char, serialize_char)
//////////////////////////////////////////////////////////////////////////////
impl<'a> Serializable for &'a str {
impl<'a, S: Serializer<E>, E> Serializable<S, E> for &'a str {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_str(*self)
}
}
impl Serializable for String {
impl<S: Serializer<E>, E> Serializable<S, E> for String {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
self.as_slice().serialize(s)
}
}
@ -167,9 +166,14 @@ impl Serializable for String {
macro_rules! impl_serializable_box {
($ty:ty) => {
impl<'a, T: Serializable> Serializable for $ty {
impl<
'a,
S: Serializer<E>,
E,
T: Serializable<S, E>
> Serializable<S, E> for $ty {
#[inline]
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
(**self).serialize(s)
}
}
@ -181,9 +185,13 @@ impl_serializable_box!(Box<T>)
impl_serializable_box!(Gc<T>)
impl_serializable_box!(Rc<T>)
impl<T: Serializable + Send + Sync> Serializable for Arc<T> {
impl<
S: Serializer<E>,
E,
T: Serializable<S, E> + Send + Sync
> Serializable<S, E> for Arc<T> {
#[inline]
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
(**self).serialize(s)
}
}
@ -191,13 +199,12 @@ impl<T: Serializable + Send + Sync> Serializable for Arc<T> {
//////////////////////////////////////////////////////////////////////////////
impl<
T: Serializable
> Serializable for Option<T> {
S: Serializer<E>,
E,
T: Serializable<S, E>
> Serializable<S, E> for Option<T> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_option(self)
}
}
@ -205,13 +212,12 @@ impl<
//////////////////////////////////////////////////////////////////////////////
impl<
T: Serializable
> Serializable for Vec<T> {
S: Serializer<E>,
E,
T: Serializable<S, E>
> Serializable<S, E> for Vec<T> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_seq(self.iter())
}
}
@ -219,27 +225,25 @@ impl<
//////////////////////////////////////////////////////////////////////////////
impl<
K: Serializable + Eq + Hash,
V: Serializable
> Serializable for HashMap<K, V> {
S: Serializer<E>,
E,
K: Serializable<S, E> + Eq + Hash,
V: Serializable<S, E>
> Serializable<S, E> for HashMap<K, V> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_map(self.iter())
}
}
impl<
K: Serializable + Ord,
V: Serializable
> Serializable for TreeMap<K, V> {
S: Serializer<E>,
E,
K: Serializable<S, E> + Ord,
V: Serializable<S, E>
> Serializable<S, E> for TreeMap<K, V> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_map(self.iter())
}
}
@ -247,25 +251,23 @@ impl<
//////////////////////////////////////////////////////////////////////////////
impl<
T: Serializable + Eq + Hash
> Serializable for HashSet<T> {
S: Serializer<E>,
E,
T: Serializable<S, E> + Eq + Hash
> Serializable<S, E> for HashSet<T> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_seq(self.iter())
}
}
impl<
T: Serializable + Ord
> Serializable for TreeSet<T> {
S: Serializer<E>,
E,
T: Serializable<S, E> + Ord
> Serializable<S, E> for TreeSet<T> {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_seq(self.iter())
}
}
@ -278,26 +280,22 @@ macro_rules! peel {
macro_rules! impl_serialize_tuple {
() => {
impl Serializable for () {
impl<S: Serializer<E>, E> Serializable<S, E> for () {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_null()
}
}
};
( $($name:ident,)+ ) => {
impl<
$($name:Serializable),*
> Serializable for ($($name,)*) {
S: Serializer<E>,
E,
$($name:Serializable<S, E>),+
> Serializable<S, E> for ($($name,)+) {
#[inline]
#[allow(uppercase_variables)]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
fn serialize(&self, s: &mut S) -> Result<(), E> {
// FIXME: how can we count macro args?
let mut len = 0;
$({ let $name = 1; len += $name; })*;
@ -495,7 +493,7 @@ mod tests {
}
fn serialize_tuple_elt<
T: Serializable
T: Serializable<AssertSerializer<Iter>, Error>
>(&mut self, value: &T) -> Result<(), Error> {
try!(self.serialize(TupleSep));
value.serialize(self)
@ -510,7 +508,7 @@ mod tests {
}
fn serialize_struct_elt<
T: Serializable
T: Serializable<AssertSerializer<Iter>, Error>
>(&mut self, name: &str, value: &T) -> Result<(), Error> {
try!(self.serialize(StructSep(name)));
value.serialize(self)
@ -525,7 +523,7 @@ mod tests {
}
fn serialize_enum_elt<
T: Serializable
T: Serializable<AssertSerializer<Iter>, Error>
>(&mut self, value: &T) -> Result<(), Error> {
try!(self.serialize(EnumSep));
value.serialize(self)
@ -536,7 +534,7 @@ mod tests {
}
fn serialize_option<
T: Serializable
T: Serializable<AssertSerializer<Iter>, Error>
>(&mut self, v: &Option<T>) -> Result<(), Error> {
match *v {
Some(ref v) => {
@ -550,7 +548,7 @@ mod tests {
}
fn serialize_seq<
T: Serializable,
T: Serializable<AssertSerializer<Iter>, Error>,
SeqIter: Iterator<T>
>(&mut self, mut iter: SeqIter) -> Result<(), Error> {
let (len, _) = iter.size_hint();
@ -562,8 +560,8 @@ mod tests {
}
fn serialize_map<
K: Serializable,
V: Serializable,
K: Serializable<AssertSerializer<Iter>, Error>,
V: Serializable<AssertSerializer<Iter>, Error>,
MapIter: Iterator<(K, V)>
>(&mut self, mut iter: MapIter) -> Result<(), Error> {
let (len, _) = iter.size_hint();