2012-12-10 22:37:21 -06:00
|
|
|
//! Support code for encoding and decoding types.
|
|
|
|
|
|
|
|
/*
|
|
|
|
Core encoding and decoding interfaces.
|
|
|
|
*/
|
|
|
|
|
2016-10-09 17:07:18 -05:00
|
|
|
use std::borrow::Cow;
|
2019-12-22 16:42:04 -06:00
|
|
|
use std::cell::{Cell, RefCell};
|
Improve handling of type bounds in `bit_set.rs`.
Currently, `BitSet` doesn't actually know its own domain size; it just
knows how many words it contains. To improve things, this commit makes
the following changes.
- It changes `BitSet` and `SparseBitSet` to store their own domain size,
and do more precise bounds and same-size checks with it. It also
changes the signature of `BitSet::to_string()` (and puts it within
`impl ToString`) now that the domain size need not be passed in from
outside.
- It uses `derive(RustcDecodable, RustcEncodable)` for `BitSet`. This
required adding code to handle `PhantomData` in `libserialize`.
- As a result, it removes the domain size from `HybridBitSet`, making a
lot of that code nicer.
- Both set_up_to() and clear_above() were overly general, working with
arbitrary sizes when they are only needed for the domain size. The
commit removes the former, degeneralizes the latter, and removes the
(overly general) tests.
- Changes `GrowableBitSet::grow()` to `ensure()`, fixing a bug where a
(1-based) domain size was confused with a (0-based) element index.
- Changes `BitMatrix` to store its row count, and do more precise bounds
checks with it.
- Changes `ty_params` in `select.rs` from a `BitSet` to a
`GrowableBitSet` because it repeatedly failed the new, more precise
bounds checks. (Changing the type was simpler than computing an
accurate domain size.)
- Various other minor improvements.
2018-09-18 02:03:24 -05:00
|
|
|
use std::marker::PhantomData;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::path;
|
2013-11-25 12:35:03 -06:00
|
|
|
use std::rc::Rc;
|
2014-10-25 22:38:27 -05:00
|
|
|
use std::sync::Arc;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2015-01-04 00:24:50 -06:00
|
|
|
pub trait Encoder {
|
|
|
|
type Error;
|
|
|
|
|
2013-05-01 19:54:54 -05:00
|
|
|
// Primitive types:
|
2018-09-11 09:32:41 -05:00
|
|
|
fn emit_unit(&mut self) -> Result<(), Self::Error>;
|
2016-08-24 01:20:51 -05:00
|
|
|
fn emit_usize(&mut self, v: usize) -> Result<(), Self::Error>;
|
2016-08-22 19:56:52 -05:00
|
|
|
fn emit_u128(&mut self, v: u128) -> Result<(), Self::Error>;
|
2015-01-04 00:24:50 -06:00
|
|
|
fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
|
|
|
fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>;
|
|
|
|
fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>;
|
|
|
|
fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>;
|
2016-08-24 01:20:51 -05:00
|
|
|
fn emit_isize(&mut self, v: isize) -> Result<(), Self::Error>;
|
2016-08-22 19:56:52 -05:00
|
|
|
fn emit_i128(&mut self, v: i128) -> Result<(), Self::Error>;
|
2015-01-04 00:24:50 -06:00
|
|
|
fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
|
|
|
fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>;
|
|
|
|
fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>;
|
|
|
|
fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>;
|
|
|
|
fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
|
|
|
fn emit_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
|
|
|
fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>;
|
|
|
|
fn emit_char(&mut self, v: char) -> Result<(), Self::Error>;
|
|
|
|
fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>;
|
2021-03-11 15:06:45 -06:00
|
|
|
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error>;
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
// Compound types:
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn emit_enum<F>(&mut self, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn emit_enum_variant<F>(
|
|
|
|
&mut self,
|
|
|
|
_v_name: &str,
|
|
|
|
v_id: usize,
|
|
|
|
_len: usize,
|
|
|
|
f: F,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
self.emit_usize(v_id)?;
|
|
|
|
f(self)
|
|
|
|
}
|
2014-12-06 13:30:22 -06:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn emit_enum_variant_arg<F>(&mut self, _first: bool, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn emit_struct<F>(&mut self, _no_fields: bool, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn emit_struct_field<F>(&mut self, _f_name: &str, _first: bool, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn emit_tuple<F>(&mut self, _len: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn emit_tuple_arg<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
|
|
|
// Specialized types:
|
|
|
|
fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
2021-06-01 12:12:55 -05:00
|
|
|
self.emit_enum(f)
|
2016-09-01 08:55:33 -05:00
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2018-08-15 01:54:21 -05:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn emit_option_none(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.emit_enum_variant("None", 0, 0, |_| Ok(()))
|
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2015-01-04 00:24:50 -06:00
|
|
|
fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
self.emit_enum_variant("Some", 1, 1, f)
|
|
|
|
}
|
2014-12-06 13:30:22 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
self.emit_usize(len)?;
|
|
|
|
f(self)
|
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2014-12-06 13:30:22 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
self.emit_usize(len)?;
|
|
|
|
f(self)
|
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn emit_map_elt_val<F>(&mut self, f: F) -> Result<(), Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
}
|
2014-12-06 13:30:22 -06:00
|
|
|
|
2015-01-04 00:24:50 -06:00
|
|
|
pub trait Decoder {
|
|
|
|
type Error;
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
// Primitive types:
|
2018-09-11 08:20:09 -05:00
|
|
|
fn read_nil(&mut self) -> Result<(), Self::Error>;
|
2016-08-24 01:20:51 -05:00
|
|
|
fn read_usize(&mut self) -> Result<usize, Self::Error>;
|
2016-08-22 19:56:52 -05:00
|
|
|
fn read_u128(&mut self) -> Result<u128, Self::Error>;
|
2015-01-04 00:24:50 -06:00
|
|
|
fn read_u64(&mut self) -> Result<u64, Self::Error>;
|
|
|
|
fn read_u32(&mut self) -> Result<u32, Self::Error>;
|
|
|
|
fn read_u16(&mut self) -> Result<u16, Self::Error>;
|
|
|
|
fn read_u8(&mut self) -> Result<u8, Self::Error>;
|
2016-08-24 01:20:51 -05:00
|
|
|
fn read_isize(&mut self) -> Result<isize, Self::Error>;
|
2016-08-22 19:56:52 -05:00
|
|
|
fn read_i128(&mut self) -> Result<i128, Self::Error>;
|
2015-01-04 00:24:50 -06:00
|
|
|
fn read_i64(&mut self) -> Result<i64, Self::Error>;
|
|
|
|
fn read_i32(&mut self) -> Result<i32, Self::Error>;
|
|
|
|
fn read_i16(&mut self) -> Result<i16, Self::Error>;
|
|
|
|
fn read_i8(&mut self) -> Result<i8, Self::Error>;
|
|
|
|
fn read_bool(&mut self) -> Result<bool, Self::Error>;
|
|
|
|
fn read_f64(&mut self) -> Result<f64, Self::Error>;
|
|
|
|
fn read_f32(&mut self) -> Result<f32, Self::Error>;
|
|
|
|
fn read_char(&mut self) -> Result<char, Self::Error>;
|
2019-02-06 20:42:43 -06:00
|
|
|
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>;
|
2021-03-25 05:43:03 -05:00
|
|
|
fn read_raw_bytes_into(&mut self, s: &mut [u8]) -> Result<(), Self::Error>;
|
2013-05-01 19:54:54 -05:00
|
|
|
|
|
|
|
// Compound types:
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_enum<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2018-07-27 07:41:31 -05:00
|
|
|
fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
let disr = self.read_usize()?;
|
|
|
|
f(self, disr)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_enum_variant_arg<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_struct<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_struct_field<T, F>(&mut self, _f_name: &str, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2016-09-01 08:55:33 -05:00
|
|
|
fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_tuple_arg<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
2013-05-01 19:54:54 -05:00
|
|
|
// Specialized types:
|
2016-09-01 08:55:33 -05:00
|
|
|
fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnMut(&mut Self, bool) -> Result<T, Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
2021-06-01 12:12:55 -05:00
|
|
|
self.read_enum(move |this| {
|
2019-12-22 16:42:04 -06:00
|
|
|
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")),
|
2016-09-01 08:55:33 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
|
|
|
fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
let len = self.read_usize()?;
|
|
|
|
f(self, len)
|
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_seq_elt<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2015-01-04 00:24:50 -06:00
|
|
|
|
|
|
|
fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
|
2016-09-01 08:55:33 -05:00
|
|
|
{
|
|
|
|
let len = self.read_usize()?;
|
|
|
|
f(self, len)
|
|
|
|
}
|
2018-07-27 07:41:31 -05:00
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_map_elt_key<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:12:24 -06:00
|
|
|
#[inline]
|
2021-06-01 12:12:55 -05:00
|
|
|
fn read_map_elt_val<T, F>(&mut self, f: F) -> Result<T, Self::Error>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
|
2018-07-27 07:41:31 -05:00
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
2014-07-30 21:35:32 -05:00
|
|
|
|
|
|
|
// Failure
|
2015-01-04 00:24:50 -06:00
|
|
|
fn error(&mut self, err: &str) -> Self::Error;
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:32:46 -05:00
|
|
|
/// Trait for types that can be serialized
|
|
|
|
///
|
|
|
|
/// This can be implemented using the `Encodable`, `TyEncodable` and
|
|
|
|
/// `MetadataEncodable` macros.
|
|
|
|
///
|
|
|
|
/// * `Encodable` should be used in crates that don't depend on
|
2020-06-29 15:09:54 -05:00
|
|
|
/// `rustc_middle`.
|
|
|
|
/// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
|
|
|
|
/// `rustc_metadata::rmeta::Lazy`.
|
2020-06-22 13:32:46 -05:00
|
|
|
/// * `TyEncodable` should be used for types that are only serialized in crate
|
2020-06-29 15:09:54 -05:00
|
|
|
/// metadata or the incremental cache. This is most types in `rustc_middle`.
|
2020-06-11 09:49:57 -05:00
|
|
|
pub trait Encodable<S: Encoder> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:32:46 -05:00
|
|
|
/// Trait for types that can be deserialized
|
|
|
|
///
|
|
|
|
/// This can be implemented using the `Decodable`, `TyDecodable` and
|
|
|
|
/// `MetadataDecodable` macros.
|
|
|
|
///
|
|
|
|
/// * `Decodable` should be used in crates that don't depend on
|
2020-06-29 15:09:54 -05:00
|
|
|
/// `rustc_middle`.
|
|
|
|
/// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
|
|
|
|
/// `rustc_metadata::rmeta::Lazy`.
|
2020-06-22 13:32:46 -05:00
|
|
|
/// * `TyDecodable` should be used for types that are only serialized in crate
|
2020-06-29 15:09:54 -05:00
|
|
|
/// metadata or the incremental cache. This is most types in `rustc_middle`.
|
2020-06-11 09:49:57 -05:00
|
|
|
pub trait Decodable<D: Decoder>: Sized {
|
|
|
|
fn decode(d: &mut D) -> Result<Self, D::Error>;
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:09:54 -05:00
|
|
|
macro_rules! direct_serialize_impls {
|
|
|
|
($($ty:ident $emit_method:ident $read_method:ident),*) => {
|
|
|
|
$(
|
|
|
|
impl<S: Encoder> Encodable<S> for $ty {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
s.$emit_method(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Decoder> Decodable<D> for $ty {
|
|
|
|
fn decode(d: &mut D) -> Result<$ty, D::Error> {
|
|
|
|
d.$read_method()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
direct_serialize_impls! {
|
|
|
|
usize emit_usize read_usize,
|
|
|
|
u8 emit_u8 read_u8,
|
|
|
|
u16 emit_u16 read_u16,
|
|
|
|
u32 emit_u32 read_u32,
|
|
|
|
u64 emit_u64 read_u64,
|
|
|
|
u128 emit_u128 read_u128,
|
|
|
|
isize emit_isize read_isize,
|
|
|
|
i8 emit_i8 read_i8,
|
|
|
|
i16 emit_i16 read_i16,
|
|
|
|
i32 emit_i32 read_i32,
|
|
|
|
i64 emit_i64 read_i64,
|
|
|
|
i128 emit_i128 read_i128,
|
|
|
|
f32 emit_f32 read_f32,
|
|
|
|
f64 emit_f64 read_f64,
|
|
|
|
bool emit_bool read_bool,
|
|
|
|
char emit_char read_char
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2018-08-23 06:46:53 -05:00
|
|
|
s.emit_u32(self.get())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
|
|
|
|
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
2018-08-23 06:46:53 -05:00
|
|
|
d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for str {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2014-11-06 10:25:09 -06:00
|
|
|
s.emit_str(self)
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for &str {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
s.emit_str(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Encoder> Encodable<S> for String {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-18 13:48:57 -06:00
|
|
|
s.emit_str(&self[..])
|
2014-04-30 17:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder> Decodable<D> for String {
|
|
|
|
fn decode(d: &mut D) -> Result<String, D::Error> {
|
2016-10-09 17:07:18 -05:00
|
|
|
Ok(d.read_str()?.into_owned())
|
2014-04-30 17:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for () {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2018-09-11 09:32:41 -05:00
|
|
|
s.emit_unit()
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder> Decodable<D> for () {
|
|
|
|
fn decode(d: &mut D) -> Result<(), D::Error> {
|
2018-09-11 08:20:09 -05:00
|
|
|
d.read_nil()
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
Improve handling of type bounds in `bit_set.rs`.
Currently, `BitSet` doesn't actually know its own domain size; it just
knows how many words it contains. To improve things, this commit makes
the following changes.
- It changes `BitSet` and `SparseBitSet` to store their own domain size,
and do more precise bounds and same-size checks with it. It also
changes the signature of `BitSet::to_string()` (and puts it within
`impl ToString`) now that the domain size need not be passed in from
outside.
- It uses `derive(RustcDecodable, RustcEncodable)` for `BitSet`. This
required adding code to handle `PhantomData` in `libserialize`.
- As a result, it removes the domain size from `HybridBitSet`, making a
lot of that code nicer.
- Both set_up_to() and clear_above() were overly general, working with
arbitrary sizes when they are only needed for the domain size. The
commit removes the former, degeneralizes the latter, and removes the
(overly general) tests.
- Changes `GrowableBitSet::grow()` to `ensure()`, fixing a bug where a
(1-based) domain size was confused with a (0-based) element index.
- Changes `BitMatrix` to store its row count, and do more precise bounds
checks with it.
- Changes `ty_params` in `select.rs` from a `BitSet` to a
`GrowableBitSet` because it repeatedly failed the new, more precise
bounds checks. (Changing the type was simpler than computing an
accurate domain size.)
- Various other minor improvements.
2018-09-18 02:03:24 -05:00
|
|
|
s.emit_unit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<PhantomData<T>, D::Error> {
|
Improve handling of type bounds in `bit_set.rs`.
Currently, `BitSet` doesn't actually know its own domain size; it just
knows how many words it contains. To improve things, this commit makes
the following changes.
- It changes `BitSet` and `SparseBitSet` to store their own domain size,
and do more precise bounds and same-size checks with it. It also
changes the signature of `BitSet::to_string()` (and puts it within
`impl ToString`) now that the domain size need not be passed in from
outside.
- It uses `derive(RustcDecodable, RustcEncodable)` for `BitSet`. This
required adding code to handle `PhantomData` in `libserialize`.
- As a result, it removes the domain size from `HybridBitSet`, making a
lot of that code nicer.
- Both set_up_to() and clear_above() were overly general, working with
arbitrary sizes when they are only needed for the domain size. The
commit removes the former, degeneralizes the latter, and removes the
(overly general) tests.
- Changes `GrowableBitSet::grow()` to `ensure()`, fixing a bug where a
(1-based) domain size was confused with a (0-based) element index.
- Changes `BitMatrix` to store its row count, and do more precise bounds
checks with it.
- Changes `ty_params` in `select.rs` from a `BitSet` to a
`GrowableBitSet` because it repeatedly failed the new, more precise
bounds checks. (Changing the type was simpler than computing an
accurate domain size.)
- Various other minor improvements.
2018-09-18 02:03:24 -05:00
|
|
|
d.read_nil()?;
|
|
|
|
Ok(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
|
|
|
|
fn decode(d: &mut D) -> Result<Box<[T]>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let v: Vec<T> = Decodable::decode(d)?;
|
2014-11-06 10:25:09 -06:00
|
|
|
Ok(v.into_boxed_slice())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2014-03-21 00:10:44 -05:00
|
|
|
(**self).encode(s)
|
2013-11-25 12:35:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<Rc<T>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(Rc::new(Decodable::decode(d)?))
|
2013-11-25 12:35:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
|
2020-12-16 21:03:31 -06:00
|
|
|
default fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2013-11-20 17:46:49 -06:00
|
|
|
s.emit_seq(self.len(), |s| {
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, e) in self.iter().enumerate() {
|
2016-03-22 22:01:37 -05:00
|
|
|
s.emit_seq_elt(i, |s| e.encode(s))?
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2020-09-26 07:55:42 -05:00
|
|
|
let slice: &[T] = self;
|
|
|
|
slice.encode(s)
|
2014-02-18 23:36:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
|
2020-12-16 23:03:45 -06:00
|
|
|
default fn decode(d: &mut D) -> Result<Vec<T>, D::Error> {
|
2014-02-18 23:36:51 -06:00
|
|
|
d.read_seq(|d, len| {
|
2014-03-18 12:58:26 -05:00
|
|
|
let mut v = Vec::with_capacity(len);
|
2021-06-01 12:12:55 -05:00
|
|
|
for _ in 0..len {
|
|
|
|
v.push(d.read_seq_elt(|d| Decodable::decode(d))?);
|
2014-03-18 12:58:26 -05:00
|
|
|
}
|
|
|
|
Ok(v)
|
2014-02-18 23:36:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 07:55:42 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
|
2020-06-11 09:49:57 -05:00
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2020-09-26 07:55:42 -05:00
|
|
|
let slice: &[T] = self;
|
|
|
|
slice.encode(s)
|
2020-03-31 00:17:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 07:55:42 -05:00
|
|
|
impl<D: Decoder, const N: usize> Decodable<D> for [u8; N] {
|
|
|
|
fn decode(d: &mut D) -> Result<[u8; N], D::Error> {
|
2020-03-31 00:17:15 -05:00
|
|
|
d.read_seq(|d, len| {
|
2020-09-26 07:55:42 -05:00
|
|
|
assert!(len == N);
|
|
|
|
let mut v = [0u8; N];
|
2020-03-31 00:17:15 -05:00
|
|
|
for i in 0..len {
|
2021-06-01 12:12:55 -05:00
|
|
|
v[i] = d.read_seq_elt(|d| Decodable::decode(d))?;
|
2020-03-31 00:17:15 -05:00
|
|
|
}
|
|
|
|
Ok(v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
[T]: ToOwned<Owned = Vec<T>>,
|
|
|
|
{
|
2020-06-11 09:49:57 -05:00
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2020-09-26 07:55:42 -05:00
|
|
|
let slice: &[T] = self;
|
|
|
|
slice.encode(s)
|
2017-02-01 22:44:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
[T]: ToOwned<Owned = Vec<T>>,
|
2018-08-17 01:59:55 -05:00
|
|
|
{
|
2020-06-11 09:49:57 -05:00
|
|
|
fn decode(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> {
|
2020-12-16 23:03:45 -06:00
|
|
|
let v: Vec<T> = Decodable::decode(d)?;
|
|
|
|
Ok(Cow::Owned(v))
|
2017-02-01 22:44:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2019-12-22 16:42:04 -06:00
|
|
|
s.emit_option(|s| match *self {
|
|
|
|
None => s.emit_option_none(),
|
|
|
|
Some(ref v) => s.emit_option_some(|s| v.encode(s)),
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<Option<T>, D::Error> {
|
2019-12-22 16:42:04 -06:00
|
|
|
d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) })
|
2017-12-20 09:37:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2021-06-01 12:12:55 -05:00
|
|
|
s.emit_enum(|s| match *self {
|
2019-12-22 16:42:04 -06:00
|
|
|
Ok(ref v) => {
|
2021-06-01 12:12:55 -05:00
|
|
|
s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(true, |s| v.encode(s)))
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
Err(ref v) => {
|
2021-06-01 12:12:55 -05:00
|
|
|
s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(true, |s| v.encode(s)))
|
2017-12-20 09:37:29 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
|
|
|
|
fn decode(d: &mut D) -> Result<Result<T1, T2>, D::Error> {
|
2021-06-01 12:12:55 -05:00
|
|
|
d.read_enum(|d| {
|
2019-12-22 16:42:04 -06:00
|
|
|
d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr {
|
2021-06-01 12:12:55 -05:00
|
|
|
0 => Ok(Ok(d.read_enum_variant_arg(|d| T1::decode(d))?)),
|
|
|
|
1 => Ok(Err(d.read_enum_variant_arg(|d| T2::decode(d))?)),
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => {
|
|
|
|
panic!(
|
|
|
|
"Encountered invalid discriminant while \
|
|
|
|
decoding `Result`."
|
|
|
|
);
|
2017-12-20 09:37:29 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2012-12-10 22:37:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! peel {
|
|
|
|
($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
|
|
|
|
}
|
2014-03-22 23:58:51 -05:00
|
|
|
|
2019-04-01 04:16:03 -05:00
|
|
|
/// Evaluates to the number of tokens passed to it.
|
|
|
|
///
|
|
|
|
/// Logarithmic counting: every one or two recursive expansions, the number of
|
|
|
|
/// tokens to count is divided by two, instead of being reduced by one.
|
|
|
|
/// Therefore, the recursion depth is the binary logarithm of the number of
|
|
|
|
/// tokens to count, and the expanded tree is likewise very small.
|
|
|
|
macro_rules! count {
|
|
|
|
() => (0usize);
|
|
|
|
($one:tt) => (1usize);
|
|
|
|
($($pairs:tt $_p:tt)*) => (count!($($pairs)*) << 1usize);
|
|
|
|
($odd:tt $($rest:tt)*) => (count!($($rest)*) | 1usize);
|
libserialize: tuple-arity should be provided to `Decoder::read_tuple`
Currently `Decoder` implementations are not provided the tuple arity as
a parameter to `read_tuple`. This forces all encoder/decoder combos to
serialize the arity along with the elements. Tuple-arity is always known
statically at the decode site, because it is part of the type of the
tuple, so it could instead be provided as an argument to `read_tuple`,
as it is to `read_struct`.
The upside to this is that serialized tuples could become smaller in
encoder/decoder implementations which choose not to serialize type
(arity) information. For example, @TyOverby's
[binary-encode](https://github.com/TyOverby/binary-encode) format is
currently forced to serialize the tuple-arity along with every tuple,
despite the information being statically known at the decode site.
A downside to this change is that the tuple-arity of serialized tuples
can no longer be automatically checked during deserialization. However,
for formats which do serialize the tuple-arity, either explicitly (rbml)
or implicitly (json), this check can be added to the `read_tuple` method.
The signature of `Deserialize::read_tuple` and
`Deserialize::read_tuple_struct` are changed, and thus binary
backwards-compatibility is broken. This change does *not* force
serialization formats to change, and thus does not break decoding values
serialized prior to this change.
[breaking-change]
2014-09-27 16:19:19 -05:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! tuple {
|
2014-03-22 23:58:51 -05:00
|
|
|
() => ();
|
|
|
|
( $($name:ident,)+ ) => (
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
|
2014-07-18 07:45:17 -05:00
|
|
|
#[allow(non_snake_case)]
|
2020-06-11 09:49:57 -05:00
|
|
|
fn decode(d: &mut D) -> Result<($($name,)+), D::Error> {
|
2019-05-29 13:05:43 -05:00
|
|
|
let len: usize = count!($($name)+);
|
libserialize: tuple-arity should be provided to `Decoder::read_tuple`
Currently `Decoder` implementations are not provided the tuple arity as
a parameter to `read_tuple`. This forces all encoder/decoder combos to
serialize the arity along with the elements. Tuple-arity is always known
statically at the decode site, because it is part of the type of the
tuple, so it could instead be provided as an argument to `read_tuple`,
as it is to `read_struct`.
The upside to this is that serialized tuples could become smaller in
encoder/decoder implementations which choose not to serialize type
(arity) information. For example, @TyOverby's
[binary-encode](https://github.com/TyOverby/binary-encode) format is
currently forced to serialize the tuple-arity along with every tuple,
despite the information being statically known at the decode site.
A downside to this change is that the tuple-arity of serialized tuples
can no longer be automatically checked during deserialization. However,
for formats which do serialize the tuple-arity, either explicitly (rbml)
or implicitly (json), this check can be added to the `read_tuple` method.
The signature of `Deserialize::read_tuple` and
`Deserialize::read_tuple_struct` are changed, and thus binary
backwards-compatibility is broken. This change does *not* force
serialization formats to change, and thus does not break decoding values
serialized prior to this change.
[breaking-change]
2014-09-27 16:19:19 -05:00
|
|
|
d.read_tuple(len, |d| {
|
2021-06-01 12:12:55 -05:00
|
|
|
let ret = ($(d.read_tuple_arg(|d| -> Result<$name, D::Error> {
|
2014-03-22 23:58:51 -05:00
|
|
|
Decodable::decode(d)
|
2019-05-29 13:05:43 -05:00
|
|
|
})?,)+);
|
2015-09-07 17:36:29 -05:00
|
|
|
Ok(ret)
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
|
2014-07-18 07:45:17 -05:00
|
|
|
#[allow(non_snake_case)]
|
2020-06-11 09:49:57 -05:00
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2019-05-29 13:05:43 -05:00
|
|
|
let ($(ref $name,)+) = *self;
|
2014-03-22 23:58:51 -05:00
|
|
|
let mut n = 0;
|
2019-05-29 13:05:43 -05:00
|
|
|
$(let $name = $name; n += 1;)+
|
2014-03-22 23:58:51 -05:00
|
|
|
s.emit_tuple(n, |s| {
|
|
|
|
let mut i = 0;
|
2019-05-29 13:05:43 -05:00
|
|
|
$(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s))?;)+
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 13:05:43 -05:00
|
|
|
peel! { $($name,)+ }
|
2014-03-22 23:58:51 -05:00
|
|
|
)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2014-03-22 23:58:51 -05:00
|
|
|
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
2013-05-01 19:54:54 -05:00
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for path::Path {
|
|
|
|
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
2015-02-26 23:00:43 -06:00
|
|
|
self.to_str().unwrap().encode(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for path::PathBuf {
|
|
|
|
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
2019-05-01 21:05:58 -05:00
|
|
|
path::Path::encode(self, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder> Decodable<D> for path::PathBuf {
|
|
|
|
fn decode(d: &mut D) -> Result<path::PathBuf, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let bytes: String = Decodable::decode(d)?;
|
2015-03-18 11:14:54 -05:00
|
|
|
Ok(path::PathBuf::from(bytes))
|
2015-02-26 23:00:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2014-07-04 07:24:50 -05:00
|
|
|
self.get().encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<Cell<T>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(Cell::new(Decodable::decode(d)?))
|
2014-07-04 07:24:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: #15036
|
|
|
|
// Should use `try_borrow`, returning a
|
|
|
|
// `encoder.error("attempting to Encode borrowed RefCell")`
|
|
|
|
// from `encode` when `try_borrow` returns `None`.
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2014-07-04 07:24:50 -05:00
|
|
|
self.borrow().encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<RefCell<T>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(RefCell::new(Decodable::decode(d)?))
|
2014-07-04 07:24:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2014-10-25 22:38:27 -05:00
|
|
|
(**self).encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<Arc<T>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(Arc::new(Decodable::decode(d)?))
|
2014-10-25 22:38:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
2020-06-01 12:58:18 -05:00
|
|
|
(**self).encode(s)
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<Box<T>, D::Error> {
|
2021-08-04 21:53:51 -05:00
|
|
|
Ok(Box::new(Decodable::decode(d)?))
|
2020-06-01 12:58:18 -05:00
|
|
|
}
|
|
|
|
}
|