Inline many methods of Encoder

They aren't overridden anyway
This commit is contained in:
bjorn3 2021-06-04 18:23:50 +02:00
parent 5cd29225a5
commit 22e8d5f80e
6 changed files with 250 additions and 384 deletions

View File

@ -146,10 +146,9 @@ fn encodable_body(
.map(|binding| {
let bind_ident = &binding.binding;
let result = quote! {
match ::rustc_serialize::Encoder::emit_struct_field(
match ::rustc_serialize::Encodable::<#encoder_ty>::encode(
#bind_ident,
__encoder,
|__encoder|
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
) {
::std::result::Result::Ok(()) => (),
::std::result::Result::Err(__err)
@ -161,9 +160,7 @@ fn encodable_body(
.collect::<TokenStream>()
});
quote! {
::rustc_serialize::Encoder::emit_struct(__encoder, |__encoder| {
::std::result::Result::Ok(match *self { #encode_inner })
})
}
}
_ => {
@ -175,10 +172,9 @@ fn encodable_body(
.map(|binding| {
let bind_ident = &binding.binding;
let result = quote! {
match ::rustc_serialize::Encoder::emit_enum_variant_arg(
match ::rustc_serialize::Encodable::<#encoder_ty>::encode(
#bind_ident,
__encoder,
|__encoder|
::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder),
) {
::std::result::Result::Ok(()) => (),
::std::result::Result::Err(__err)
@ -208,11 +204,9 @@ fn encodable_body(
result
});
quote! {
::rustc_serialize::Encoder::emit_enum(__encoder, |__encoder| {
match *self {
#encode_inner
}
})
}
}
};

View File

@ -25,12 +25,11 @@ impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for LinkedList<T> {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?;
e.encode(s)?;
}
Ok(())
})
}
}
@ -43,12 +42,11 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?;
e.encode(s)?;
}
Ok(())
})
}
}
@ -65,13 +63,12 @@ where
V: Encodable<S>,
{
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
e.emit_usize(self.len())?;
for (key, val) in self.iter() {
e.emit_map_elt_key(|e| key.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
key.encode(e)?;
val.encode(e)?;
}
Ok(())
})
}
}
@ -97,12 +94,11 @@ where
T: Encodable<S> + PartialEq + Ord,
{
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?;
e.encode(s)?;
}
Ok(())
})
}
}
@ -127,13 +123,12 @@ where
S: BuildHasher,
{
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
e.emit_usize(self.len())?;
for (key, val) in self.iter() {
e.emit_map_elt_key(|e| key.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
key.encode(e)?;
val.encode(e)?;
}
Ok(())
})
}
}
@ -162,12 +157,11 @@ where
S: BuildHasher,
{
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?;
e.encode(s)?;
}
Ok(())
})
}
}
@ -194,13 +188,12 @@ where
S: BuildHasher,
{
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
e.emit_usize(self.len())?;
for (key, val) in self.iter() {
e.emit_map_elt_key(|e| key.encode(e))?;
e.emit_map_elt_val(|e| val.encode(e))?;
key.encode(e)?;
val.encode(e)?;
}
Ok(())
})
}
}
@ -229,12 +222,11 @@ where
S: BuildHasher,
{
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?;
e.encode(s)?;
}
Ok(())
})
}
}

View File

@ -35,15 +35,7 @@ pub trait Encoder {
fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>;
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error>;
// Compound types:
#[inline]
fn emit_enum<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
// Convenience for the derive macro:
fn emit_enum_variant<F>(&mut self, v_id: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
@ -62,106 +54,6 @@ pub trait Encoder {
fn emit_fieldless_enum_variant<const ID: usize>(&mut self) -> Result<(), Self::Error> {
self.emit_usize(ID)
}
#[inline]
fn emit_enum_variant_arg<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_struct<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_struct_field<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_tuple<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_tuple_arg<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
// Specialized types:
fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_enum(f)
}
#[inline]
fn emit_option_none(&mut self) -> Result<(), Self::Error> {
self.emit_enum_variant(0, |_| Ok(()))
}
fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_enum_variant(1, f)
}
fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_usize(len)?;
f(self)
}
#[inline]
fn emit_seq_elt<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
self.emit_usize(len)?;
f(self)
}
#[inline]
fn emit_map_elt_key<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
#[inline]
fn emit_map_elt_val<F>(&mut self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self) -> Result<(), Self::Error>,
{
f(self)
}
}
// Note: all the methods in this trait are infallible, which may be surprising.
@ -353,12 +245,11 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
default fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
s.emit_usize(self.len())?;
for e in self.iter() {
s.emit_seq_elt(|s| e.encode(s))?
e.encode(s)?
}
Ok(())
})
}
}
@ -441,10 +332,10 @@ impl<'a, D: Decoder> Decodable<D> for Cow<'a, str> {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
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)),
})
match *self {
None => s.emit_enum_variant(0, |_| Ok(())),
Some(ref v) => s.emit_enum_variant(1, |s| v.encode(s)),
}
}
}
@ -460,10 +351,10 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
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(|s| match *self {
Ok(ref v) => s.emit_enum_variant(0, |s| s.emit_enum_variant_arg(|s| v.encode(s))),
Err(ref v) => s.emit_enum_variant(1, |s| s.emit_enum_variant_arg(|s| v.encode(s))),
})
match *self {
Ok(ref v) => s.emit_enum_variant(0, |s| v.encode(s)),
Err(ref v) => s.emit_enum_variant(1, |s| v.encode(s)),
}
}
}
@ -493,10 +384,8 @@ macro_rules! tuple {
#[allow(non_snake_case)]
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
let ($(ref $name,)+) = *self;
s.emit_tuple(|s| {
$(s.emit_tuple_arg(|s| $name.encode(s))?;)+
$($name.encode(s)?;)+
Ok(())
})
}
}
peel! { $($name,)+ }

View File

@ -307,11 +307,8 @@ impl DefId {
impl<E: Encoder> Encodable<E> for DefId {
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_struct(|s| {
s.emit_struct_field(|s| self.krate.encode(s))?;
s.emit_struct_field(|s| self.index.encode(s))
})
self.krate.encode(s)?;
self.index.encode(s)
}
}

View File

@ -196,10 +196,10 @@ impl Hash for RealFileName {
// an added assert statement
impl<S: Encoder> Encodable<S> for RealFileName {
fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
encoder.emit_enum(|encoder| match *self {
match *self {
RealFileName::LocalPath(ref local_path) => encoder.emit_enum_variant(0, |encoder| {
Ok({
encoder.emit_enum_variant_arg(|encoder| local_path.encode(encoder))?;
local_path.encode(encoder)?;
})
}),
@ -208,11 +208,11 @@ impl<S: Encoder> Encodable<S> for RealFileName {
// For privacy and build reproducibility, we must not embed host-dependant path in artifacts
// if they have been remapped by --remap-path-prefix
assert!(local_path.is_none());
encoder.emit_enum_variant_arg(|encoder| local_path.encode(encoder))?;
encoder.emit_enum_variant_arg(|encoder| virtual_name.encode(encoder))?;
local_path.encode(encoder)?;
virtual_name.encode(encoder)?;
Ok(())
}),
})
}
}
}
@ -949,10 +949,8 @@ impl Default for Span {
impl<E: Encoder> Encodable<E> for Span {
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
let span = self.data();
s.emit_struct(|s| {
s.emit_struct_field(|s| span.lo.encode(s))?;
s.emit_struct_field(|s| span.hi.encode(s))
})
span.lo.encode(s)?;
span.hi.encode(s)
}
}
impl<D: Decoder> Decodable<D> for Span {
@ -1301,12 +1299,11 @@ pub struct SourceFile {
impl<S: Encoder> Encodable<S> for SourceFile {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct(|s| {
s.emit_struct_field(|s| self.name.encode(s))?;
s.emit_struct_field(|s| self.src_hash.encode(s))?;
s.emit_struct_field(|s| self.start_pos.encode(s))?;
s.emit_struct_field(|s| self.end_pos.encode(s))?;
s.emit_struct_field(|s| {
self.name.encode(s)?;
self.src_hash.encode(s)?;
self.start_pos.encode(s)?;
self.end_pos.encode(s)?;
// We are always in `Lines` form by the time we reach here.
assert!(self.lines.borrow().is_lines());
self.lines(|lines| {
@ -1366,14 +1363,13 @@ impl<S: Encoder> Encodable<S> for SourceFile {
s.emit_raw_bytes(&raw_diffs)?;
}
Ok(())
})
})?;
s.emit_struct_field(|s| self.multibyte_chars.encode(s))?;
s.emit_struct_field(|s| self.non_narrow_chars.encode(s))?;
s.emit_struct_field(|s| self.name_hash.encode(s))?;
s.emit_struct_field(|s| self.normalized_pos.encode(s))?;
s.emit_struct_field(|s| self.cnum.encode(s))
})
self.multibyte_chars.encode(s)?;
self.non_narrow_chars.encode(s)?;
self.name_hash.encode(s)?;
self.normalized_pos.encode(s)?;
self.cnum.encode(s)
}
}

View File

@ -716,118 +716,116 @@ where
I::AllocId: Encodable<E>,
{
fn encode(&self, e: &mut E) -> Result<(), <E as rustc_serialize::Encoder>::Error> {
rustc_serialize::Encoder::emit_enum(e, |e| {
let disc = discriminant(self);
match self {
Bool => e.emit_enum_variant(disc, |_| Ok(())),
Char => e.emit_enum_variant(disc, |_| Ok(())),
Int(i) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| i.encode(e))?;
i.encode(e)?;
Ok(())
}),
Uint(u) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| u.encode(e))?;
u.encode(e)?;
Ok(())
}),
Float(f) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| f.encode(e))?;
f.encode(e)?;
Ok(())
}),
Adt(adt, substs) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| adt.encode(e))?;
e.emit_enum_variant_arg(|e| substs.encode(e))?;
adt.encode(e)?;
substs.encode(e)?;
Ok(())
}),
Foreign(def_id) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| def_id.encode(e))?;
def_id.encode(e)?;
Ok(())
}),
Str => e.emit_enum_variant(disc, |_| Ok(())),
Array(t, c) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| t.encode(e))?;
e.emit_enum_variant_arg(|e| c.encode(e))?;
t.encode(e)?;
c.encode(e)?;
Ok(())
}),
Slice(t) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| t.encode(e))?;
t.encode(e)?;
Ok(())
}),
RawPtr(tam) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| tam.encode(e))?;
tam.encode(e)?;
Ok(())
}),
Ref(r, t, m) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| r.encode(e))?;
e.emit_enum_variant_arg(|e| t.encode(e))?;
e.emit_enum_variant_arg(|e| m.encode(e))?;
r.encode(e)?;
t.encode(e)?;
m.encode(e)?;
Ok(())
}),
FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| def_id.encode(e))?;
e.emit_enum_variant_arg(|e| substs.encode(e))?;
def_id.encode(e)?;
substs.encode(e)?;
Ok(())
}),
FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| polyfnsig.encode(e))?;
polyfnsig.encode(e)?;
Ok(())
}),
Dynamic(l, r) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| l.encode(e))?;
e.emit_enum_variant_arg(|e| r.encode(e))?;
l.encode(e)?;
r.encode(e)?;
Ok(())
}),
Closure(def_id, substs) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| def_id.encode(e))?;
e.emit_enum_variant_arg(|e| substs.encode(e))?;
def_id.encode(e)?;
substs.encode(e)?;
Ok(())
}),
Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| def_id.encode(e))?;
e.emit_enum_variant_arg(|e| substs.encode(e))?;
e.emit_enum_variant_arg(|e| m.encode(e))?;
def_id.encode(e)?;
substs.encode(e)?;
m.encode(e)?;
Ok(())
}),
GeneratorWitness(b) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| b.encode(e))?;
b.encode(e)?;
Ok(())
}),
Never => e.emit_enum_variant(disc, |_| Ok(())),
Tuple(substs) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| substs.encode(e))?;
substs.encode(e)?;
Ok(())
}),
Projection(p) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| p.encode(e))?;
p.encode(e)?;
Ok(())
}),
Opaque(def_id, substs) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| def_id.encode(e))?;
e.emit_enum_variant_arg(|e| substs.encode(e))?;
def_id.encode(e)?;
substs.encode(e)?;
Ok(())
}),
Param(p) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| p.encode(e))?;
p.encode(e)?;
Ok(())
}),
Bound(d, b) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| d.encode(e))?;
e.emit_enum_variant_arg(|e| b.encode(e))?;
d.encode(e)?;
b.encode(e)?;
Ok(())
}),
Placeholder(p) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| p.encode(e))?;
p.encode(e)?;
Ok(())
}),
Infer(i) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| i.encode(e))?;
i.encode(e)?;
Ok(())
}),
Error(d) => e.emit_enum_variant(disc, |e| {
e.emit_enum_variant_arg(|e| d.encode(e))?;
d.encode(e)?;
Ok(())
}),
}
})
}
}