Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions soroban-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn contractspecfn(metadata: TokenStream, input: TokenStream) -> TokenStream

let derived: Result<proc_macro2::TokenStream, proc_macro2::TokenStream> = methods
.iter()
.map(|m| derive_fn_spec(&args.name, m.ident, m.attrs, m.inputs, m.output, export))
.map(|m| derive_fn_spec(&args.name, &m.ident, &m.attrs, &m.inputs, &m.output, export))
.collect();

match derived {
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
}
.unwrap_or_else(|| "Client".to_string());

let pub_methods: Vec<_> = syn_ext::impl_pub_methods(&imp).collect();
let pub_methods: Vec<_> = syn_ext::impl_pub_methods(&imp);
let derived: Result<proc_macro2::TokenStream, proc_macro2::TokenStream> = pub_methods
.iter()
.map(|m| {
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
crate_path,
ty,
trait_ident,
pub_methods.into_iter(),
pub_methods.iter(),
);
output.extend(quote! { #cfs });
output.into()
Expand Down
40 changes: 21 additions & 19 deletions soroban-sdk-macros/src/syn_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ use syn::{
/// methods that are inherently implemented this is methods that have a pub
/// visibility keyword. For methods that are implementing a trait the pub is
/// assumed and so all methods are returned.
pub fn impl_pub_methods(imp: &ItemImpl) -> impl Iterator<Item = &ImplItemFn> {
pub fn impl_pub_methods(imp: &ItemImpl) -> Vec<ImplItemFn> {
imp.items
.iter()
.filter_map(|i| match i {
ImplItem::Fn(m) => Some(m),
ImplItem::Fn(m) => Some(m.clone()),
_ => None,
})
.filter(|m| imp.trait_.is_some() || matches!(m.vis, Visibility::Public(_)))
.collect()
}

/// Gets methods from the trait.
Expand Down Expand Up @@ -161,22 +162,23 @@ impl HasFnsItem {
}
}

pub fn fns(&self) -> Vec<Fn<'_>> {
pub fn fns(&self) -> Vec<Fn> {
match self {
HasFnsItem::Trait(t) => trait_methods(t)
.map(|m| Fn {
ident: &m.sig.ident,
attrs: &m.attrs,
inputs: &m.sig.inputs,
output: &m.sig.output,
ident: m.sig.ident.clone(),
attrs: m.attrs.clone(),
inputs: m.sig.inputs.clone(),
output: m.sig.output.clone(),
})
.collect(),
HasFnsItem::Impl(i) => impl_pub_methods(i)
.iter()
.map(|m| Fn {
ident: &m.sig.ident,
attrs: &m.attrs,
inputs: &m.sig.inputs,
output: &m.sig.output,
ident: m.sig.ident.clone(),
attrs: m.attrs.clone(),
inputs: m.sig.inputs.clone(),
output: m.sig.output.clone(),
})
.collect(),
}
Expand Down Expand Up @@ -210,16 +212,16 @@ impl ToTokens for HasFnsItem {
}
}

pub struct Fn<'a> {
pub ident: &'a Ident,
pub attrs: &'a [Attribute],
pub inputs: &'a Punctuated<FnArg, Comma>,
pub output: &'a ReturnType,
pub struct Fn {
pub ident: Ident,
pub attrs: Vec<Attribute>,
pub inputs: Punctuated<FnArg, Comma>,
pub output: ReturnType,
}

impl<'a> Fn<'a> {
impl Fn {
pub fn output(&self) -> Type {
let t = match self.output {
let t = match &self.output {
ReturnType::Default => quote!(()),
ReturnType::Type(_, typ) => match unpack_result(typ) {
Some((t, _)) => quote!(#t),
Expand All @@ -229,7 +231,7 @@ impl<'a> Fn<'a> {
Type::Verbatim(t)
}
pub fn try_output(&self, crate_path: &Path) -> Type {
let (t, e) = match self.output {
let (t, e) = match &self.output {
ReturnType::Default => (quote!(()), quote!(#crate_path::Error)),
ReturnType::Type(_, typ) => match unpack_result(typ) {
Some((t, e)) => (quote!(#t), quote!(#e)),
Expand Down