Skip to content

Commit

Permalink
Simple derive macro for better struct support
Browse files Browse the repository at this point in the history
  • Loading branch information
rory-ocl committed Aug 28, 2024
1 parent 8d1b45f commit cdee8ce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stylus-proc/src/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

pub fn derive_abi_type(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
let name = &input.ident;
let name_str = name.to_string();
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

quote! {
impl #impl_generics stylus_sdk::abi::AbiType for #name #ty_generics #where_clause {
type SolType = Self;
const ABI: stylus_sdk::abi::ConstString = stylus_sdk::abi::ConstString::new(#name_str);
}
}
.into()
}
18 changes: 18 additions & 0 deletions stylus-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ macro_rules! error {
}};
}

mod abi;
mod calls;
mod methods;
mod storage;
Expand Down Expand Up @@ -565,3 +566,20 @@ pub fn public(attr: TokenStream, input: TokenStream) -> TokenStream {
pub fn external(attr: TokenStream, input: TokenStream) -> TokenStream {
public(attr, input)
}

/// Implements the AbiType for arbitrary structs, allowing them to be used in external method
/// return types and parameters. This derive is intended to be used within the
/// [alloy_sol_types::sol] macro.
///
/// ```ignore
/// sol! {
/// #[derive(AbiType)]
/// pub struct Foo {
/// u256 bar;
/// }
/// }
/// ```
#[proc_macro_derive(AbiType)]
pub fn derive_abi_type(input: TokenStream) -> TokenStream {
abi::derive_abi_type(input)
}

0 comments on commit cdee8ce

Please sign in to comment.