Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 6, 2024
1 parent 18a4387 commit 4a8db85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
28 changes: 18 additions & 10 deletions crates/sol-macro-expander/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,23 +714,31 @@ impl CallLikeExpander<'_> {
Self::SELECTORS.binary_search(&selector).is_ok()
}

#[inline]
#[inline(never)]
#[allow(unsafe_code, non_snake_case)]
fn abi_decode_raw(
selector: [u8; 4],
data: &[u8],
validate: bool
)-> alloy_sol_types::Result<Self> {
match selector {
#(<#sorted_types as alloy_sol_types::#trait_>::SELECTOR =>
<#sorted_types as alloy_sol_types::#trait_>::abi_decode_raw(data, validate)
.map(Self::#sorted_variants),
)*
s => ::core::result::Result::Err(alloy_sol_types::Error::unknown_selector(
static DECODE_SHIMS: &[fn(&[u8], bool) -> alloy_sol_types::Result<#name>] = &[
#({
fn #sorted_variants(data: &[u8], validate: bool) -> alloy_sol_types::Result<#name> {
<#sorted_types as alloy_sol_types::#trait_>::abi_decode_raw(data, validate)
.map(#name::#sorted_variants)
}
#sorted_variants
}),*
];

let Ok(idx) = Self::SELECTORS.binary_search(&selector) else {
return Err(alloy_sol_types::Error::unknown_selector(
<Self as alloy_sol_types::SolInterface>::NAME,
s,
)),
}
selector,
));
};
// `SELECTORS` and `DECODE_SHIMS` have the same length and are sorted in the same order.
DECODE_SHIMS[idx](data, validate)
}

#[inline]
Expand Down
52 changes: 13 additions & 39 deletions crates/sol-macro-expander/src/expand/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,11 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, enumm: &ItemEnum) -> Result<TokenStream>
let uint8 = quote!(alloy_sol_types::sol_data::Uint<8>);
let uint8_st = quote!(<#uint8 as alloy_sol_types::SolType>);

let index_to_variant: Vec<_> = variants
.iter()
.enumerate()
.map(|(idx, variant)| {
let ident = &variant.ident;
let idx = idx as u8;
quote! { #idx => ::core::result::Result::Ok(Self::#ident), }
})
.collect();

let variant_to_index: Vec<_> = variants
.iter()
.enumerate()
.map(|(idx, variant)| {
let ident = &variant.ident;
let idx = idx as u8;
quote! { Self::#ident => &#idx, }
})
.collect();
let index_to_variant = variants.iter().enumerate().map(|(idx, variant)| {
let ident = &variant.ident;
let idx = idx as u8;
quote! { #idx => ::core::result::Result::Ok(Self::#ident), }
});

let doc = docs.then(|| mk_doc(format!("```solidity\n{enumm}\n```")));
let tokens = quote! {
Expand Down Expand Up @@ -119,12 +105,12 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, enumm: &ItemEnum) -> Result<TokenStream>
type Error = alloy_sol_types::Error;

#[inline]
fn try_from(v: u8) -> alloy_sol_types::Result<Self> {
match v {
fn try_from(value: u8) -> alloy_sol_types::Result<Self> {
match value {
#(#index_to_variant)*
_ => ::core::result::Result::Err(alloy_sol_types::Error::InvalidEnumValue {
value => ::core::result::Result::Err(alloy_sol_types::Error::InvalidEnumValue {
name: #name_s,
value: v,
value,
max: #max,
})
}
Expand All @@ -145,7 +131,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, enumm: &ItemEnum) -> Result<TokenStream>

#[inline]
fn stv_eip712_data_word(&self) -> alloy_sol_types::Word {
#uint8_st::eip712_data_word(self.as_u8())
#uint8_st::eip712_data_word(&(*self as u8))
}

#[inline]
Expand Down Expand Up @@ -188,36 +174,24 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, enumm: &ItemEnum) -> Result<TokenStream>
impl alloy_sol_types::EventTopic for #name {
#[inline]
fn topic_preimage_length(rust: &Self::RustType) -> usize {
<#uint8 as alloy_sol_types::EventTopic>::topic_preimage_length(rust.as_u8())
<#uint8 as alloy_sol_types::EventTopic>::topic_preimage_length(&(*rust as u8))
}

#[inline]
fn encode_topic_preimage(rust: &Self::RustType, out: &mut alloy_sol_types::private::Vec<u8>) {
<#uint8 as alloy_sol_types::EventTopic>::encode_topic_preimage(rust.as_u8(), out);
<#uint8 as alloy_sol_types::EventTopic>::encode_topic_preimage(&(*rust as u8), out);
}

#[inline]
fn encode_topic(rust: &Self::RustType) -> alloy_sol_types::abi::token::WordToken {
<#uint8 as alloy_sol_types::EventTopic>::encode_topic(rust.as_u8())
<#uint8 as alloy_sol_types::EventTopic>::encode_topic(&(*rust as u8))
}
}

#[automatically_derived]
impl alloy_sol_types::SolEnum for #name {
const COUNT: usize = #count;
}

#[automatically_derived]
impl #name {
#[allow(clippy::inline_always)]
#[inline(always)]
const fn as_u8(&self) -> &u8 {
match self {
#(#variant_to_index)*
Self::__Invalid => &u8::MAX,
}
}
}
};
};
Ok(tokens)
Expand Down

0 comments on commit 4a8db85

Please sign in to comment.