fn read_str(&mut self) -> Cow<'_, str>;
fn read_raw_bytes_into(&mut self, s: &mut [u8]);
- // Compound types:
- #[inline]
- fn read_enum<T, F>(&mut self, f: F) -> T
- where
- F: FnOnce(&mut Self) -> T,
- {
- f(self)
- }
-
#[inline]
- fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> T
+ fn read_enum_variant<T, F>(&mut self, mut f: F) -> T
where
F: FnMut(&mut Self, usize) -> T,
{
f(self, disr)
}
- #[inline]
- fn read_enum_variant_arg<T, F>(&mut self, f: F) -> T
- where
- F: FnOnce(&mut Self) -> T,
- {
- f(self)
- }
-
- #[inline]
- fn read_struct<T, F>(&mut self, f: F) -> T
- where
- F: FnOnce(&mut Self) -> T,
- {
- f(self)
- }
-
- #[inline]
- fn read_struct_field<T, F>(&mut self, _f_name: &str, f: F) -> T
- where
- F: FnOnce(&mut Self) -> T,
- {
- f(self)
- }
-
- #[inline]
- fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> T
- where
- F: FnOnce(&mut Self) -> T,
- {
- f(self)
- }
-
#[inline]
fn read_tuple_arg<T, F>(&mut self, f: F) -> T
where
where
F: FnMut(&mut Self, bool) -> T,
{
- self.read_enum(move |this| {
- this.read_enum_variant(&["None", "Some"], move |this, idx| match idx {
- 0 => f(this, false),
- 1 => f(this, true),
- _ => panic!("read_option: expected 0 for None or 1 for Some"),
- })
+ self.read_enum_variant(move |this, idx| match idx {
+ 0 => f(this, false),
+ 1 => f(this, true),
+ _ => panic!("read_option: expected 0 for None or 1 for Some"),
})
}
impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
fn decode(d: &mut D) -> Result<T1, T2> {
- d.read_enum(|d| {
- d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr {
- 0 => Ok(d.read_enum_variant_arg(|d| T1::decode(d))),
- 1 => Err(d.read_enum_variant_arg(|d| T2::decode(d))),
- _ => panic!("Encountered invalid discriminant while decoding `Result`."),
- })
+ d.read_enum_variant(|d, disr| match disr {
+ 0 => Ok(T1::decode(d)),
+ 1 => Err(T2::decode(d)),
+ _ => panic!("Encountered invalid discriminant while decoding `Result`."),
})
}
}
impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
#[allow(non_snake_case)]
fn decode(d: &mut D) -> ($($name,)+) {
- let len: usize = count!($($name)+);
- d.read_tuple(len, |d| {
- let ret = ($(d.read_tuple_arg(|d| -> $name {
- Decodable::decode(d)
- }),)+);
- ret
- })
+ ($(d.read_tuple_arg(|d| -> $name {
+ Decodable::decode(d)
+ }),)+)
}
}
impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
#[allow(non_snake_case)]
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
let ($(ref $name,)+) = *self;
- let mut n = 0;
- $(let $name = $name; n += 1;)+
- s.emit_tuple(n, |s| {
+ let len: usize = count!($($name)+);
+ s.emit_tuple(len, |s| {
let mut i = 0;
$(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s))?;)+
Ok(())