Work-around for shadowing of variant names with assoc const names in libproc_macro/bridge/rpc.rs.

This commit is contained in:
Alexander Regueiro 2018-12-07 02:12:49 +00:00
parent 6a2a7edc0b
commit edabad64b0

View File

@ -71,15 +71,18 @@ fn decode(r: &mut Reader, s: &mut S) -> Self {
(enum $name:ident $(<$($T:ident),+>)* { $($variant:ident $(($field:ident))*),* $(,)* }) => { (enum $name:ident $(<$($T:ident),+>)* { $($variant:ident $(($field:ident))*),* $(,)* }) => {
impl<S, $($($T: Encode<S>),+)*> Encode<S> for $name $(<$($T),+>)* { impl<S, $($($T: Encode<S>),+)*> Encode<S> for $name $(<$($T),+>)* {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {
// HACK(eddyb) `Tag` enum duplicated between the // HACK(eddyb): `Tag` enum duplicated between the
// two impls as there's no other place to stash it. // two impls as there's no other place to stash it.
#[repr(u8)] enum Tag { $($variant),* }
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* } mod tag {
#[repr(u8)] enum Tag { $($variant),* }
$(pub const $variant: u8 = Tag::$variant as u8;)*
}
match self { match self {
$($name::$variant $(($field))* => { $($name::$variant $(($field))* => {
<Tag>::$variant.encode(w, s); tag::$variant.encode(w, s);
$($field.encode(w, s);)* $($field.encode(w, s);)*
})* })*
} }
@ -90,14 +93,17 @@ impl<S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)*> DecodeMut<'a, '_, S>
for $name $(<$($T),+>)* for $name $(<$($T),+>)*
{ {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
// HACK(eddyb) `Tag` enum duplicated between the // HACK(eddyb): `Tag` enum duplicated between the
// two impls as there's no other place to stash it. // two impls as there's no other place to stash it.
#[repr(u8)] enum Tag { $($variant),* }
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* } mod tag {
#[repr(u8)] enum Tag { $($variant),* }
$(pub const $variant: u8 = Tag::$variant as u8;)*
}
match u8::decode(r, s) { match u8::decode(r, s) {
$(<Tag>::$variant => { $(tag::$variant => {
$(let $field = DecodeMut::decode(r, s);)* $(let $field = DecodeMut::decode(r, s);)*
$name::$variant $(($field))* $name::$variant $(($field))*
})* })*