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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions soroban-sdk-macros/src/syn_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syn::{
};
use syn::{
spanned::Spanned, token::And, Error, FnArg, Ident, ImplItem, ImplItemFn, ItemImpl, ItemTrait,
Lifetime, Pat, PatType, TraitItem, TraitItemFn, Type, TypeReference, Visibility,
Lifetime, Pat, PatIdent, PatType, TraitItem, TraitItemFn, Type, TypeReference, Visibility,
};

/// Gets methods from the implementation that have public visibility. For
Expand Down Expand Up @@ -48,6 +48,26 @@ pub fn fn_arg_ident(arg: &FnArg) -> Result<Ident, Error> {
))
}

/// Modifies a Pat removing any 'mut' on an Ident.
pub fn pat_unwrap_mut(p: Pat) -> Pat {
match p {
Pat::Ident(PatIdent {
attrs,
by_ref,
mutability: Some(_),
ident,
subpat,
}) => Pat::Ident(PatIdent {
attrs,
by_ref,
mutability: None,
ident,
subpat,
}),
_ => p,
}
}

/// Returns a clone of the type from the FnArg.
pub fn fn_arg_ref_type(arg: &FnArg, lifetime: Option<&Lifetime>) -> Result<Type, Error> {
if let FnArg::Typed(pat_type) = arg {
Expand All @@ -70,13 +90,13 @@ pub fn fn_arg_ref_type(arg: &FnArg, lifetime: Option<&Lifetime>) -> Result<Type,
}

/// Returns a clone of FnArg with the type as a reference if the arg is a typed
/// arg and its type is not already a reference.
/// arg and its type is not already a reference. Mutability from the ident is stripped.
pub fn fn_arg_make_ref(arg: &FnArg, lifetime: Option<&Lifetime>) -> FnArg {
if let FnArg::Typed(pat_type) = arg {
if !matches!(*pat_type.ty, Type::Reference(_)) {
return FnArg::Typed(PatType {
attrs: pat_type.attrs.clone(),
pat: pat_type.pat.clone(),
pat: Box::new(pat_unwrap_mut(*pat_type.pat.clone())),
colon_token: pat_type.colon_token,
ty: Box::new(Type::Reference(TypeReference {
and_token: And::default(),
Expand All @@ -90,12 +110,14 @@ pub fn fn_arg_make_ref(arg: &FnArg, lifetime: Option<&Lifetime>) -> FnArg {
arg.clone()
}

/// Returns a clone of FnArg with the type as an Into if the arg is a typed
/// arg. Mutability from the ident is stripped.
pub fn fn_arg_make_into(arg: &FnArg) -> FnArg {
if let FnArg::Typed(pat_type) = arg {
let ty = &pat_type.ty;
return FnArg::Typed(PatType {
attrs: pat_type.attrs.clone(),
pat: pat_type.pat.clone(),
pat: Box::new(pat_unwrap_mut(*pat_type.pat.clone())),
colon_token: pat_type.colon_token,
ty: Box::new(syn::parse_quote! { impl Into<#ty> }),
});
Expand Down
8 changes: 8 additions & 0 deletions soroban-sdk/src/tests/contract_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ impl Contract {
pub fn add_with_unused_arg(_e: &Env, a: i32, _b: i32) -> i32 {
a + 2
}

pub fn add_with_mut_arg(_e: &Env, a: i32, mut b: i32) -> i32 {
b *= 1;
a + b
}
}

#[contract]
Expand All @@ -38,6 +43,9 @@ fn test_functional() {
let b = 12i32;
let c = ContractClient::new(&e, &contract_id).add(&a, &b);
assert_eq!(c, 22);

let c = ContractClient::new(&e, &contract_id).add_with_mut_arg(&a, &b);
assert_eq!(c, 22);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mux_id": 0
},
"auth": [
[],
[],
[]
],
Expand Down
Loading