-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ContractArgs for building function args (#1382)
### What Add ContractArgs for building function args. ### Usage ```diff -let contract_id = env.register(Contract, (100_u32, 1000_i64)); +let contract_id = env.register(Contract, ContractArgs::__constructor(&100_u32, &1000_i64)); ``` ### Diff of generated code for constructor test vector ```diff @@ -7,6 +7,8 @@ extern crate core; extern crate compiler_builtins as _; use soroban_sdk::{contract, contractimpl, contracttype, Env}; pub struct Contract; +///ContractArgs is a type for building arg lists for functions defined in "Contract". +pub struct ContractArgs; ///ContractClient is a client for calling the contract defined in "Contract". pub struct ContractClient<'a> { pub env: soroban_sdk::Env, @@ -210,6 +212,17 @@ impl<'a> ContractClient<'a> { res } } +impl ContractArgs { + pub fn __constructor<'i>( + init_key: &'i u32, + init_value: &'i i64, + ) -> (&'i u32, &'i i64) { + (init_key, init_value) + } + pub fn get_data<'i>(key: &'i DataKey) -> (&'i DataKey,) { + (key,) + } +} #[doc(hidden)] pub mod ____constructor { use super::*; ``` ### Why To provide a way to make specifying args as concrete. Dependent on: - stellar/rs-soroban-env#1479 Close #1348 ### Note This is still very much an experiment.
- Loading branch information
1 parent
b7b8255
commit e59dcc6
Showing
13 changed files
with
217 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use itertools::MultiUnzip; | ||
use proc_macro2::{Span, TokenStream}; | ||
use quote::{format_ident, quote}; | ||
use syn::{Error, FnArg, Lifetime, Type, TypePath, TypeReference}; | ||
|
||
use crate::syn_ext; | ||
|
||
pub fn derive_args_type(ty: &str, name: &str) -> TokenStream { | ||
let ty_str = quote!(#ty).to_string(); | ||
let args_doc = | ||
format!("{name} is a type for building arg lists for functions defined in {ty_str}."); | ||
let args_ident = format_ident!("{name}"); | ||
quote! { | ||
#[doc = #args_doc] | ||
pub struct #args_ident; | ||
} | ||
} | ||
|
||
pub fn derive_args_impl(name: &str, fns: &[syn_ext::Fn]) -> TokenStream { | ||
// Map the traits methods to methods for the Args. | ||
let mut errors = Vec::<Error>::new(); | ||
let fns: Vec<_> = fns | ||
.iter() | ||
.map(|f| { | ||
let fn_ident = &f.ident; | ||
|
||
// Check for the Env argument. | ||
let env_input = f.inputs.first().and_then(|a| match a { | ||
FnArg::Typed(pat_type) => { | ||
let mut ty = &*pat_type.ty; | ||
if let Type::Reference(TypeReference { elem, .. }) = ty { | ||
ty = elem; | ||
} | ||
if let Type::Path(TypePath { | ||
path: syn::Path { segments, .. }, | ||
.. | ||
}) = ty | ||
{ | ||
if segments.last().map_or(false, |s| s.ident == "Env") { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
FnArg::Receiver(_) => None, | ||
}); | ||
|
||
// Map all remaining inputs. | ||
let fn_input_lifetime = Lifetime::new("'i", Span::call_site()); | ||
let (fn_input_names, fn_input_types, fn_input_fn_args): (Vec<_>, Vec<_>, Vec<_>) = f | ||
.inputs | ||
.iter() | ||
.skip(if env_input.is_some() { 1 } else { 0 }) | ||
.map(|t| { | ||
let ident = match syn_ext::fn_arg_ident(t) { | ||
Ok(ident) => ident, | ||
Err(e) => { | ||
errors.push(e); | ||
format_ident!("_") | ||
} | ||
}; | ||
let ty = match syn_ext::fn_arg_ref_type(t, Some(&fn_input_lifetime)) { | ||
Ok(ty) => Some(ty), | ||
Err(e) => { | ||
errors.push(e); | ||
None | ||
} | ||
}; | ||
( | ||
ident, | ||
ty, | ||
syn_ext::fn_arg_make_ref(t, Some(&fn_input_lifetime)), | ||
) | ||
}) | ||
.multiunzip(); | ||
|
||
quote! { | ||
#[inline(always)] | ||
pub fn #fn_ident<#fn_input_lifetime>(#(#fn_input_fn_args),*) | ||
-> (#(#fn_input_types,)*) | ||
{ | ||
(#(#fn_input_names,)*) | ||
} | ||
} | ||
}) | ||
.collect(); | ||
|
||
// If errors have occurred, render them instead. | ||
if !errors.is_empty() { | ||
let compile_errors = errors.iter().map(Error::to_compile_error); | ||
return quote! { #(#compile_errors)* }; | ||
} | ||
|
||
// Render the Client. | ||
let args_ident = format_ident!("{}", name); | ||
quote! { | ||
impl #args_ident { | ||
#(#fns)* | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.