Remove unused functions and arguments from rustc_serialize

This commit is contained in:
bjorn3 2021-06-01 19:12:55 +02:00
parent 7f9ab0300c
commit a2c4affe86
8 changed files with 144 additions and 350 deletions

View File

@ -216,7 +216,7 @@ macro_rules! encode_fields {
$(
$enc.emit_struct_field(
stringify!($name),
idx,
idx == 0,
|enc| $name.encode(enc),
)?;
idx += 1;
@ -229,7 +229,7 @@ macro_rules! encode_fields {
// Special-case encoder to skip tool_metadata if not set
impl<E: Encoder> Encodable<E> for Diagnostic {
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_struct("diagnostic", 7, |s| {
s.emit_struct(false, |s| {
let mut idx = 0;
idx = encode_fields!(

View File

@ -43,12 +43,9 @@ fn decodable_body(
let decode_body = match s.variants() {
[vi] => {
let construct = vi.construct(|field, index| decode_field(field, index, true));
let n_fields = vi.ast().fields.len();
quote! {
::rustc_serialize::Decoder::read_struct(
__decoder,
#ty_name,
#n_fields,
|__decoder| { ::std::result::Result::Ok(#construct) },
)
}
@ -77,7 +74,6 @@ fn decodable_body(
quote! {
::rustc_serialize::Decoder::read_enum(
__decoder,
#ty_name,
|__decoder| {
::rustc_serialize::Decoder::read_enum_variant(
__decoder,
@ -128,7 +124,7 @@ fn decode_field(field: &syn::Field, index: usize, is_struct: bool) -> proc_macro
quote! {
match ::rustc_serialize::Decoder::#decode_method(
__decoder, #opt_field_name #index, #decode_inner_method) {
__decoder, #opt_field_name #decode_inner_method) {
::std::result::Result::Ok(__res) => __res,
::std::result::Result::Err(__err) => return ::std::result::Result::Err(__err),
}
@ -183,7 +179,6 @@ fn encodable_body(
}
});
let ty_name = s.ast().ident.to_string();
let encode_body = match s.variants() {
[_] => {
let mut field_idx = 0usize;
@ -197,11 +192,12 @@ fn encodable_body(
.ident
.as_ref()
.map_or_else(|| field_idx.to_string(), |i| i.to_string());
let first = field_idx == 0;
let result = quote! {
match ::rustc_serialize::Encoder::emit_struct_field(
__encoder,
#field_name,
#field_idx,
#first,
|__encoder|
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
) {
@ -215,8 +211,9 @@ fn encodable_body(
})
.collect::<TokenStream>()
});
let no_fields = field_idx == 0;
quote! {
::rustc_serialize::Encoder::emit_struct(__encoder, #ty_name, #field_idx, |__encoder| {
::rustc_serialize::Encoder::emit_struct(__encoder, #no_fields, |__encoder| {
::std::result::Result::Ok(match *self { #encode_inner })
})
}
@ -232,10 +229,11 @@ fn encodable_body(
.iter()
.map(|binding| {
let bind_ident = &binding.binding;
let first = field_idx == 0;
let result = quote! {
match ::rustc_serialize::Encoder::emit_enum_variant_arg(
__encoder,
#field_idx,
#first,
|__encoder|
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
) {
@ -260,7 +258,7 @@ fn encodable_body(
result
});
quote! {
::rustc_serialize::Encoder::emit_enum(__encoder, #ty_name, |__encoder| {
::rustc_serialize::Encoder::emit_enum(__encoder, |__encoder| {
match *self {
#encode_inner
}

View File

@ -122,21 +122,21 @@ fn decode(d: &mut opaque::Decoder<'a>) -> Result<SerializedDepGraph<K>, String>
let mut edge_list_data = Vec::with_capacity(edge_count);
for _index in 0..node_count {
d.read_struct("NodeInfo", 3, |d| {
let dep_node: DepNode<K> = d.read_struct_field("node", 0, Decodable::decode)?;
d.read_struct(|d| {
let dep_node: DepNode<K> = d.read_struct_field("node", Decodable::decode)?;
let _i: SerializedDepNodeIndex = nodes.push(dep_node);
debug_assert_eq!(_i.index(), _index);
let fingerprint: Fingerprint =
d.read_struct_field("fingerprint", 1, Decodable::decode)?;
d.read_struct_field("fingerprint", Decodable::decode)?;
let _i: SerializedDepNodeIndex = fingerprints.push(fingerprint);
debug_assert_eq!(_i.index(), _index);
d.read_struct_field("edges", 2, |d| {
d.read_struct_field("edges", |d| {
d.read_seq(|d, len| {
let start = edge_list_data.len().try_into().unwrap();
for e in 0..len {
let edge = d.read_seq_elt(e, Decodable::decode)?;
for _ in 0..len {
let edge = d.read_seq_elt(Decodable::decode)?;
edge_list_data.push(edge);
}
let end = edge_list_data.len().try_into().unwrap();

View File

@ -21,8 +21,8 @@ fn decode(d: &mut D) -> Result<SmallVec<A>, D::Error> {
d.read_seq(|d, len| {
let mut vec = SmallVec::with_capacity(len);
// FIXME(#48994) - could just be collected into a Result<SmallVec, D::Error>
for i in 0..len {
vec.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
vec.push(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(vec)
})
@ -44,8 +44,8 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> {
fn decode(d: &mut D) -> Result<LinkedList<T>, D::Error> {
d.read_seq(|d, len| {
let mut list = LinkedList::new();
for i in 0..len {
list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
list.push_back(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(list)
})
@ -67,8 +67,8 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
fn decode(d: &mut D) -> Result<VecDeque<T>, D::Error> {
d.read_seq(|d, len| {
let mut deque: VecDeque<T> = VecDeque::with_capacity(len);
for i in 0..len {
deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
deque.push_back(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(deque)
})
@ -84,7 +84,7 @@ fn encode(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
}
Ok(())
})
@ -99,9 +99,9 @@ impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
fn decode(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
d.read_map(|d, len| {
let mut map = BTreeMap::new();
for i in 0..len {
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
for _ in 0..len {
let key = d.read_map_elt_key(|d| Decodable::decode(d))?;
let val = d.read_map_elt_val(|d| Decodable::decode(d))?;
map.insert(key, val);
}
Ok(map)
@ -130,8 +130,8 @@ impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
fn decode(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
d.read_seq(|d, len| {
let mut set = BTreeSet::new();
for i in 0..len {
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
set.insert(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(set)
})
@ -148,7 +148,7 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
}
Ok(())
})
@ -165,9 +165,9 @@ fn decode(d: &mut D) -> Result<HashMap<K, V, S>, D::Error> {
d.read_map(|d, len| {
let state = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, state);
for i in 0..len {
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
for _ in 0..len {
let key = d.read_map_elt_key(|d| Decodable::decode(d))?;
let val = d.read_map_elt_val(|d| Decodable::decode(d))?;
map.insert(key, val);
}
Ok(map)
@ -209,8 +209,8 @@ fn decode(d: &mut D) -> Result<HashSet<T, S>, D::Error> {
d.read_seq(|d, len| {
let state = Default::default();
let mut set = HashSet::with_capacity_and_hasher(len, state);
for i in 0..len {
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
set.insert(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(set)
})
@ -227,7 +227,7 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
}
Ok(())
})
@ -244,9 +244,9 @@ fn decode(d: &mut D) -> Result<indexmap::IndexMap<K, V, S>, D::Error> {
d.read_map(|d, len| {
let state = Default::default();
let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
for i in 0..len {
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
for _ in 0..len {
let key = d.read_map_elt_key(|d| Decodable::decode(d))?;
let val = d.read_map_elt_val(|d| Decodable::decode(d))?;
map.insert(key, val);
}
Ok(map)
@ -278,8 +278,8 @@ fn decode(d: &mut D) -> Result<indexmap::IndexSet<T, S>, D::Error> {
d.read_seq(|d, len| {
let state = Default::default();
let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state);
for i in 0..len {
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
set.insert(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(set)
})

View File

@ -560,7 +560,7 @@ fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> {
Ok(())
}
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
fn emit_enum<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
@ -589,46 +589,20 @@ fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) ->
}
}
fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
fn emit_enum_variant_arg<F>(&mut self, first: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
if idx != 0 {
if !first {
write!(self.writer, ",")?;
}
f(self)
}
fn emit_enum_struct_variant<F>(
&mut self,
name: &str,
id: usize,
cnt: usize,
f: F,
) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_enum_variant(name, id, cnt, f)
}
fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_enum_variant_arg(idx, f)
}
fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
fn emit_struct<F>(&mut self, _: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
@ -641,14 +615,14 @@ fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
Ok(())
}
fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
fn emit_struct_field<F>(&mut self, name: &str, first: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
if idx != 0 {
if !first {
write!(self.writer, ",")?;
}
escape_str(self.writer, name)?;
@ -675,25 +649,6 @@ fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
self.emit_seq_elt(idx, f)
}
fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_seq(len, f)
}
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_seq_elt(idx, f)
}
fn emit_option<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
@ -774,7 +729,7 @@ fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
Ok(())
}
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
fn emit_map_elt_val<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
@ -892,7 +847,7 @@ fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> {
Ok(())
}
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
fn emit_enum<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
@ -930,54 +885,28 @@ fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) ->
}
}
fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
fn emit_enum_variant_arg<F>(&mut self, first: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
if idx != 0 {
if !first {
writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
f(self)
}
fn emit_enum_struct_variant<F>(
&mut self,
name: &str,
id: usize,
cnt: usize,
f: F,
) -> EncodeResult
fn emit_struct<F>(&mut self, no_fields: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_enum_variant(name, id, cnt, f)
}
fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_enum_variant_arg(idx, f)
}
fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
if len == 0 {
if no_fields {
write!(self.writer, "{{}}")?;
} else {
write!(self.writer, "{{")?;
@ -991,14 +920,14 @@ fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
Ok(())
}
fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
fn emit_struct_field<F>(&mut self, name: &str, first: bool, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
if idx == 0 {
if first {
writeln!(self.writer)?;
} else {
writeln!(self.writer, ",")?;
@ -1028,25 +957,6 @@ fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
self.emit_seq_elt(idx, f)
}
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_seq(len, f)
}
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if self.is_emitting_map_key {
return Err(EncoderError::BadHashmapKey);
}
self.emit_seq_elt(idx, f)
}
fn emit_option<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
@ -1149,7 +1059,7 @@ fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
Ok(())
}
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
fn emit_map_elt_val<F>(&mut self, f: F) -> EncodeResult
where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
@ -2373,7 +2283,7 @@ fn read_raw_bytes_into(&mut self, s: &mut [u8]) -> Result<(), Self::Error> {
Ok(())
}
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
fn read_enum<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
@ -2410,33 +2320,14 @@ fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<
f(self, idx)
}
fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
fn read_enum_variant_arg<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
where
F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
{
self.read_enum_variant(names, f)
}
fn read_enum_struct_variant_field<T, F>(
&mut self,
_name: &str,
idx: usize,
f: F,
) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_enum_variant_arg(idx, f)
}
fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
fn read_struct<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
@ -2445,7 +2336,7 @@ fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<
Ok(value)
}
fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
fn read_struct_field<T, F>(&mut self, name: &str, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
@ -2483,25 +2374,11 @@ fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
})
}
fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
fn read_tuple_arg<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_seq_elt(idx, f)
}
fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_tuple(len, f)
}
fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_tuple_arg(idx, f)
self.read_seq_elt(f)
}
fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
@ -2527,7 +2404,7 @@ fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
f(self, len)
}
fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
fn read_seq_elt<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
@ -2547,14 +2424,14 @@ fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
f(self, len)
}
fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
fn read_map_elt_key<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
fn read_map_elt_val<T, F>(&mut self, f: F) -> DecodeResult<T>
where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{

View File

@ -37,7 +37,7 @@ pub trait Encoder {
// Compound types:
#[inline]
fn emit_enum<F>(&mut self, _name: &str, f: F) -> Result<(), Self::Error>
fn emit_enum<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
@ -59,40 +59,7 @@ fn emit_enum_variant<F>(
}
#[inline]
fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
fn emit_enum_struct_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>,
{
self.emit_enum_variant(v_name, v_id, len, f)
}
fn emit_enum_struct_variant_field<F>(
&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)
}
#[inline]
fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error>
fn emit_enum_variant_arg<F>(&mut self, _first: bool, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
@ -100,12 +67,15 @@ fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self:
}
#[inline]
fn emit_struct_field<F>(
&mut self,
_f_name: &str,
_f_idx: usize,
f: F,
) -> Result<(), Self::Error>
fn emit_struct<F>(&mut self, _no_fields: bool, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_struct_field<F>(&mut self, _f_name: &str, _first: bool, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
@ -128,26 +98,12 @@ fn emit_tuple_arg<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
f(self)
}
fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_tuple(len, f)
}
fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_tuple_arg(f_idx, f)
}
// Specialized types:
fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_enum("Option", f)
self.emit_enum(f)
}
#[inline]
@ -195,7 +151,7 @@ fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
}
#[inline]
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
fn emit_map_elt_val<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
@ -229,7 +185,7 @@ pub trait Decoder {
// Compound types:
#[inline]
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>
fn read_enum<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -246,34 +202,7 @@ fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> Result<T, Se
}
#[inline]
fn read_enum_variant_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
f(self)
}
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, Self::Error>
where
F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
{
self.read_enum_variant(names, f)
}
fn read_enum_struct_variant_field<T, F>(
&mut self,
_f_name: &str,
f_idx: usize,
f: F,
) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
self.read_enum_variant_arg(f_idx, f)
}
#[inline]
fn read_struct<T, F>(&mut self, _s_name: &str, _len: usize, f: F) -> Result<T, Self::Error>
fn read_enum_variant_arg<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -281,12 +210,15 @@ fn read_struct<T, F>(&mut self, _s_name: &str, _len: usize, f: F) -> Result<T, S
}
#[inline]
fn read_struct_field<T, F>(
&mut self,
_f_name: &str,
_f_idx: usize,
f: F,
) -> Result<T, Self::Error>
fn read_struct<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
f(self)
}
#[inline]
fn read_struct_field<T, F>(&mut self, _f_name: &str, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -302,33 +234,19 @@ fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> Result<T, Self::Error>
}
#[inline]
fn read_tuple_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
fn read_tuple_arg<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
f(self)
}
fn read_tuple_struct<T, F>(&mut self, _s_name: &str, len: usize, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
self.read_tuple(len, f)
}
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
self.read_tuple_arg(a_idx, f)
}
// Specialized types:
fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error>
where
F: FnMut(&mut Self, bool) -> Result<T, Self::Error>,
{
self.read_enum("Option", move |this| {
self.read_enum(move |this| {
this.read_enum_variant(&["None", "Some"], move |this, idx| match idx {
0 => f(this, false),
1 => f(this, true),
@ -346,7 +264,7 @@ fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
}
#[inline]
fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
fn read_seq_elt<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -362,7 +280,7 @@ fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
}
#[inline]
fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
fn read_map_elt_key<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -370,7 +288,7 @@ fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error
}
#[inline]
fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
fn read_map_elt_val<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where
F: FnOnce(&mut Self) -> Result<T, Self::Error>,
{
@ -550,8 +468,8 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
default fn decode(d: &mut D) -> Result<Vec<T>, D::Error> {
d.read_seq(|d, len| {
let mut v = Vec::with_capacity(len);
for i in 0..len {
v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
for _ in 0..len {
v.push(d.read_seq_elt(|d| Decodable::decode(d))?);
}
Ok(v)
})
@ -571,7 +489,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> {
assert!(len == N);
let mut v = [0u8; N];
for i in 0..len {
v[i] = d.read_seq_elt(i, |d| Decodable::decode(d))?;
v[i] = d.read_seq_elt(|d| Decodable::decode(d))?;
}
Ok(v)
})
@ -615,12 +533,12 @@ fn decode(d: &mut D) -> Result<Option<T>, D::Error> {
impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_enum("Result", |s| match *self {
s.emit_enum(|s| match *self {
Ok(ref v) => {
s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s)))
s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(true, |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_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(true, |s| v.encode(s)))
}
})
}
@ -628,10 +546,10 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> {
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> {
d.read_enum("Result", |d| {
d.read_enum(|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))?)),
0 => Ok(Ok(d.read_enum_variant_arg(|d| T1::decode(d))?)),
1 => Ok(Err(d.read_enum_variant_arg(|d| T2::decode(d))?)),
_ => {
panic!(
"Encountered invalid discriminant while \
@ -668,8 +586,7 @@ impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
fn decode(d: &mut D) -> Result<($($name,)+), D::Error> {
let len: usize = count!($($name)+);
d.read_tuple(len, |d| {
let mut i = 0;
let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name, D::Error> {
let ret = ($(d.read_tuple_arg(|d| -> Result<$name, D::Error> {
Decodable::decode(d)
})?,)+);
Ok(ret)

View File

@ -295,20 +295,20 @@ pub fn is_top_level_module(self) -> bool {
impl<E: Encoder> Encodable<E> for DefId {
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_struct("DefId", 2, |s| {
s.emit_struct_field("krate", 0, |s| self.krate.encode(s))?;
s.emit_struct(false, |s| {
s.emit_struct_field("krate", true, |s| self.krate.encode(s))?;
s.emit_struct_field("index", 1, |s| self.index.encode(s))
s.emit_struct_field("index", false, |s| self.index.encode(s))
})
}
}
impl<D: Decoder> Decodable<D> for DefId {
default fn decode(d: &mut D) -> Result<DefId, D::Error> {
d.read_struct("DefId", 2, |d| {
d.read_struct(|d| {
Ok(DefId {
krate: d.read_struct_field("krate", 0, Decodable::decode)?,
index: d.read_struct_field("index", 1, Decodable::decode)?,
krate: d.read_struct_field("krate", Decodable::decode)?,
index: d.read_struct_field("index", Decodable::decode)?,
})
})
}

View File

@ -146,11 +146,12 @@ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// an added assert statement
impl<S: Encoder> Encodable<S> for RealFileName {
fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
encoder.emit_enum("RealFileName", |encoder| match *self {
encoder.emit_enum(|encoder| match *self {
RealFileName::LocalPath(ref local_path) => {
encoder.emit_enum_variant("LocalPath", 0, 1, |encoder| {
Ok({
encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?;
encoder
.emit_enum_variant_arg(true, |encoder| local_path.encode(encoder))?;
})
})
}
@ -161,8 +162,10 @@ fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
// if they have been remapped by --remap-path-prefix
assert!(local_path.is_none());
Ok({
encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?;
encoder.emit_enum_variant_arg(1, |encoder| virtual_name.encode(encoder))?;
encoder
.emit_enum_variant_arg(true, |encoder| local_path.encode(encoder))?;
encoder
.emit_enum_variant_arg(false, |encoder| virtual_name.encode(encoder))?;
})
}),
})
@ -827,17 +830,17 @@ fn default() -> Self {
impl<E: Encoder> Encodable<E> for Span {
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
let span = self.data();
s.emit_struct("Span", 2, |s| {
s.emit_struct_field("lo", 0, |s| span.lo.encode(s))?;
s.emit_struct_field("hi", 1, |s| span.hi.encode(s))
s.emit_struct(false, |s| {
s.emit_struct_field("lo", true, |s| span.lo.encode(s))?;
s.emit_struct_field("hi", false, |s| span.hi.encode(s))
})
}
}
impl<D: Decoder> Decodable<D> for Span {
default fn decode(s: &mut D) -> Result<Span, D::Error> {
s.read_struct("Span", 2, |d| {
let lo = d.read_struct_field("lo", 0, Decodable::decode)?;
let hi = d.read_struct_field("hi", 1, Decodable::decode)?;
s.read_struct(|d| {
let lo = d.read_struct_field("lo", Decodable::decode)?;
let hi = d.read_struct_field("hi", Decodable::decode)?;
Ok(Span::new(lo, hi, SyntaxContext::root()))
})
@ -1234,12 +1237,12 @@ pub struct SourceFile {
impl<S: Encoder> Encodable<S> for SourceFile {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct("SourceFile", 8, |s| {
s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?;
s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
s.emit_struct_field("lines", 5, |s| {
s.emit_struct(false, |s| {
s.emit_struct_field("name", true, |s| self.name.encode(s))?;
s.emit_struct_field("src_hash", false, |s| self.src_hash.encode(s))?;
s.emit_struct_field("start_pos", false, |s| self.start_pos.encode(s))?;
s.emit_struct_field("end_pos", false, |s| self.end_pos.encode(s))?;
s.emit_struct_field("lines", false, |s| {
let lines = &self.lines[..];
// Store the length.
s.emit_u32(lines.len() as u32)?;
@ -1297,25 +1300,24 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> {
Ok(())
})?;
s.emit_struct_field("multibyte_chars", 6, |s| self.multibyte_chars.encode(s))?;
s.emit_struct_field("non_narrow_chars", 7, |s| self.non_narrow_chars.encode(s))?;
s.emit_struct_field("name_hash", 8, |s| self.name_hash.encode(s))?;
s.emit_struct_field("normalized_pos", 9, |s| self.normalized_pos.encode(s))?;
s.emit_struct_field("cnum", 10, |s| self.cnum.encode(s))
s.emit_struct_field("multibyte_chars", false, |s| self.multibyte_chars.encode(s))?;
s.emit_struct_field("non_narrow_chars", false, |s| self.non_narrow_chars.encode(s))?;
s.emit_struct_field("name_hash", false, |s| self.name_hash.encode(s))?;
s.emit_struct_field("normalized_pos", false, |s| self.normalized_pos.encode(s))?;
s.emit_struct_field("cnum", false, |s| self.cnum.encode(s))
})
}
}
impl<D: Decoder> Decodable<D> for SourceFile {
fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
d.read_struct("SourceFile", 8, |d| {
let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
d.read_struct(|d| {
let name: FileName = d.read_struct_field("name", |d| Decodable::decode(d))?;
let src_hash: SourceFileHash =
d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
let start_pos: BytePos =
d.read_struct_field("start_pos", 3, |d| Decodable::decode(d))?;
let end_pos: BytePos = d.read_struct_field("end_pos", 4, |d| Decodable::decode(d))?;
let lines: Vec<BytePos> = d.read_struct_field("lines", 5, |d| {
d.read_struct_field("src_hash", |d| Decodable::decode(d))?;
let start_pos: BytePos = d.read_struct_field("start_pos", |d| Decodable::decode(d))?;
let end_pos: BytePos = d.read_struct_field("end_pos", |d| Decodable::decode(d))?;
let lines: Vec<BytePos> = d.read_struct_field("lines", |d| {
let num_lines: u32 = Decodable::decode(d)?;
let mut lines = Vec::with_capacity(num_lines as usize);
@ -1344,13 +1346,13 @@ fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
Ok(lines)
})?;
let multibyte_chars: Vec<MultiByteChar> =
d.read_struct_field("multibyte_chars", 6, |d| Decodable::decode(d))?;
d.read_struct_field("multibyte_chars", |d| Decodable::decode(d))?;
let non_narrow_chars: Vec<NonNarrowChar> =
d.read_struct_field("non_narrow_chars", 7, |d| Decodable::decode(d))?;
let name_hash: u128 = d.read_struct_field("name_hash", 8, |d| Decodable::decode(d))?;
d.read_struct_field("non_narrow_chars", |d| Decodable::decode(d))?;
let name_hash: u128 = d.read_struct_field("name_hash", |d| Decodable::decode(d))?;
let normalized_pos: Vec<NormalizedPos> =
d.read_struct_field("normalized_pos", 9, |d| Decodable::decode(d))?;
let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?;
d.read_struct_field("normalized_pos", |d| Decodable::decode(d))?;
let cnum: CrateNum = d.read_struct_field("cnum", |d| Decodable::decode(d))?;
Ok(SourceFile {
name,
start_pos,