X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibserialize%2Fserialize.rs;h=19283ffc43864f29892b39298f7a6166432f792b;hb=6980f82c0d152446506fee4d4a45d8afdf4ad9a4;hp=a5f7b4898ae0e854a98f0e57644fa3ea0a804edb;hpb=b5bea2565e565dd014f768921f63d111ebfe27d3;p=rust.git diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index a5f7b4898ae..19283ffc438 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -6,10 +6,10 @@ use std::any; use std::borrow::Cow; +use std::cell::{Cell, RefCell}; use std::marker::PhantomData; use std::path; use std::rc::Rc; -use std::cell::{Cell, RefCell}; use std::sync::Arc; pub trait Encoder { @@ -37,75 +37,109 @@ pub trait Encoder { // Compound types: fn emit_enum(&mut self, _name: &str, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } - fn emit_enum_variant(&mut self, _v_name: &str, v_id: usize, _len: usize, f: F) - -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> + fn emit_enum_variant( + &mut self, + _v_name: &str, + v_id: usize, + _len: usize, + f: F, + ) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_usize(v_id)?; f(self) } fn emit_enum_variant_arg(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } - fn emit_enum_struct_variant(&mut self, v_name: &str, v_id: usize, len: usize, f: F) - -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> + fn emit_enum_struct_variant( + &mut self, + v_name: &str, + v_id: usize, + len: usize, + f: F, + ) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_enum_variant(v_name, v_id, len, f) } - fn emit_enum_struct_variant_field(&mut self, _f_name: &str, f_idx: usize, f: F) - -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> + fn emit_enum_struct_variant_field( + &mut self, + _f_name: &str, + f_idx: usize, + f: F, + ) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_enum_variant_arg(f_idx, f) } fn emit_struct(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } - fn emit_struct_field(&mut self, _f_name: &str, _f_idx: usize, f: F) - -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> + fn emit_struct_field( + &mut self, + _f_name: &str, + _f_idx: usize, + f: F, + ) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } fn emit_tuple(&mut self, _len: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } fn emit_tuple_arg(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } fn emit_tuple_struct(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_tuple(len, f) } fn emit_tuple_struct_arg(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_tuple_arg(f_idx, f) } // Specialized types: fn emit_option(&mut self, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_enum("Option", f) } @@ -116,39 +150,45 @@ fn emit_option_none(&mut self) -> Result<(), Self::Error> { } fn emit_option_some(&mut self, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_enum_variant("Some", 1, 1, f) } fn emit_seq(&mut self, len: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_usize(len)?; f(self) } fn emit_seq_elt(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } fn emit_map(&mut self, len: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { self.emit_usize(len)?; f(self) } fn emit_map_elt_key(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> - where F: FnOnce(&mut Self) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, { f(self) } @@ -179,115 +219,140 @@ pub trait Decoder { // Compound types: fn read_enum(&mut self, _name: &str, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_enum_variant(&mut self, _names: &[&str], mut f: F) -> Result - where F: FnMut(&mut Self, usize) -> Result + where + F: FnMut(&mut Self, usize) -> Result, { let disr = self.read_usize()?; f(self, disr) } fn read_enum_variant_arg(&mut self, _a_idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_enum_struct_variant(&mut self, names: &[&str], f: F) -> Result - where F: FnMut(&mut Self, usize) -> Result + where + F: FnMut(&mut Self, usize) -> Result, { self.read_enum_variant(names, f) } - fn read_enum_struct_variant_field(&mut self, _f_name: &str, f_idx: usize, f: F) - -> Result where F: FnOnce(&mut Self) -> Result + fn read_enum_struct_variant_field( + &mut self, + _f_name: &str, + f_idx: usize, + f: F, + ) -> Result + where + F: FnOnce(&mut Self) -> Result, { self.read_enum_variant_arg(f_idx, f) } fn read_struct(&mut self, _s_name: &str, _len: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } - fn read_struct_field(&mut self, _f_name: &str, _f_idx: usize, f: F) - -> Result where F: FnOnce(&mut Self) -> Result + fn read_struct_field( + &mut self, + _f_name: &str, + _f_idx: usize, + f: F, + ) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_tuple(&mut self, _len: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_tuple_arg(&mut self, _a_idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_tuple_struct(&mut self, _s_name: &str, len: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { self.read_tuple(len, f) } fn read_tuple_struct_arg(&mut self, a_idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { self.read_tuple_arg(a_idx, f) } // Specialized types: fn read_option(&mut self, mut f: F) -> Result - where F: FnMut(&mut Self, bool) -> Result + where + F: FnMut(&mut Self, bool) -> Result, { self.read_enum("Option", move |this| { - this.read_enum_variant(&["None", "Some"], move |this, idx| { - match idx { - 0 => f(this, false), - 1 => f(this, true), - _ => Err(this.error("read_option: expected 0 for None or 1 for Some")), - } + this.read_enum_variant(&["None", "Some"], move |this, idx| match idx { + 0 => f(this, false), + 1 => f(this, true), + _ => Err(this.error("read_option: expected 0 for None or 1 for Some")), }) }) } fn read_seq(&mut self, f: F) -> Result - where F: FnOnce(&mut Self, usize) -> Result + where + F: FnOnce(&mut Self, usize) -> Result, { let len = self.read_usize()?; f(self, len) } fn read_seq_elt(&mut self, _idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_map(&mut self, f: F) -> Result - where F: FnOnce(&mut Self, usize) -> Result + where + F: FnOnce(&mut Self, usize) -> Result, { let len = self.read_usize()?; f(self, len) } fn read_map_elt_key(&mut self, _idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } fn read_map_elt_val(&mut self, _idx: usize, f: F) -> Result - where F: FnOnce(&mut Self) -> Result + where + F: FnOnce(&mut Self) -> Result, { f(self) } @@ -563,32 +628,32 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } } -impl< T: Decodable> Decodable for Box { +impl Decodable for Box { fn decode(d: &mut D) -> Result, D::Error> { Ok(box Decodable::decode(d)?) } } -impl< T: Decodable> Decodable for Box<[T]> { +impl Decodable for Box<[T]> { fn decode(d: &mut D) -> Result, D::Error> { let v: Vec = Decodable::decode(d)?; Ok(v.into_boxed_slice()) } } -impl Encodable for Rc { +impl Encodable for Rc { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) } } -impl Decodable for Rc { +impl Decodable for Rc { fn decode(d: &mut D) -> Result, D::Error> { Ok(Rc::new(Decodable::decode(d)?)) } } -impl Encodable for [T] { +impl Encodable for [T] { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { @@ -599,7 +664,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } } -impl Encodable for Vec { +impl Encodable for Vec { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { @@ -610,7 +675,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } } -impl Decodable for Vec { +impl Decodable for Vec { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut v = Vec::with_capacity(len); @@ -622,7 +687,10 @@ fn decode(d: &mut D) -> Result, D::Error> { } } -impl<'a, T:Encodable> Encodable for Cow<'a, [T]> where [T]: ToOwned> { +impl<'a, T: Encodable> Encodable for Cow<'a, [T]> +where + [T]: ToOwned>, +{ fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { @@ -633,8 +701,9 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } } -impl Decodable for Cow<'static, [T]> - where [T]: ToOwned> +impl Decodable for Cow<'static, [T]> +where + [T]: ToOwned>, { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { @@ -647,72 +716,45 @@ fn decode(d: &mut D) -> Result, D::Error> { } } - -impl Encodable for Option { +impl Encodable for Option { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_option(|s| { - match *self { - None => s.emit_option_none(), - Some(ref v) => s.emit_option_some(|s| v.encode(s)), - } + s.emit_option(|s| match *self { + None => s.emit_option_none(), + Some(ref v) => s.emit_option_some(|s| v.encode(s)), }) } } -impl Decodable for Option { +impl Decodable for Option { fn decode(d: &mut D) -> Result, D::Error> { - d.read_option(|d, b| { - if b { - Ok(Some(Decodable::decode(d)?)) - } else { - Ok(None) - } - }) + d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) }) } } impl Encodable for Result { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_enum("Result", |s| { - match *self { - Ok(ref v) => { - s.emit_enum_variant("Ok", 0, 1, |s| { - s.emit_enum_variant_arg(0, |s| { - v.encode(s) - }) - }) - } - Err(ref v) => { - s.emit_enum_variant("Err", 1, 1, |s| { - s.emit_enum_variant_arg(0, |s| { - v.encode(s) - }) - }) - } + s.emit_enum("Result", |s| match *self { + Ok(ref v) => { + s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) + } + Err(ref v) => { + s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) } }) } } -impl Decodable for Result { +impl Decodable for Result { fn decode(d: &mut D) -> Result, D::Error> { d.read_enum("Result", |d| { - d.read_enum_variant(&["Ok", "Err"], |d, disr| { - match disr { - 0 => { - Ok(Ok(d.read_enum_variant_arg(0, |d| { - T1::decode(d) - })?)) - } - 1 => { - Ok(Err(d.read_enum_variant_arg(0, |d| { - T2::decode(d) - })?)) - } - _ => { - panic!("Encountered invalid discriminant while \ - decoding `Result`."); - } + d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr { + 0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)), + 1 => Ok(Err(d.read_enum_variant_arg(0, |d| T2::decode(d))?)), + _ => { + panic!( + "Encountered invalid discriminant while \ + decoding `Result`." + ); } }) }) @@ -819,13 +861,13 @@ fn decode(d: &mut D) -> Result, D::Error> { } } -impl Encodable for Arc { +impl Encodable for Arc { fn encode(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) } } -impl Decodable for Arc { +impl Decodable for Arc { fn decode(d: &mut D) -> Result, D::Error> { Ok(Arc::new(Decodable::decode(d)?)) } @@ -848,11 +890,13 @@ pub trait SpecializationError { impl SpecializationError for E { default fn not_found(trait_name: &'static str, method_name: &'static str) -> E { - panic!("missing specialization: `<{} as {}<{}>>::{}` not overridden", - any::type_name::(), - trait_name, - any::type_name::(), - method_name); + panic!( + "missing specialization: `<{} as {}<{}>>::{}` not overridden", + any::type_name::(), + trait_name, + any::type_name::(), + method_name + ); } }