diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 685621af5..68df1c83c 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -453,9 +453,9 @@ impl Env { use crate::{ auth, testutils::{ - budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ContractFunctionSet, - EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, Snapshot, - StellarAssetContract, StellarAssetIssuer, + budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ConstructorArgs, + ContractFunctionSet, EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, + Snapshot, StellarAssetContract, StellarAssetIssuer, }, Bytes, BytesN, }; @@ -622,7 +622,7 @@ impl Env { contract_id: impl Into>, contract: T, ) -> Address { - self.register_contract_with_constructor(contract_id, contract, crate::vec![&self]) + self.register_contract_with_constructor(contract_id, contract, ()) } /// Register a contract with the [Env] for testing. @@ -665,11 +665,15 @@ impl Env { /// None, Contract, (123_u32,).into_val(&env)); /// } /// ``` - pub fn register_contract_with_constructor<'a, T: ContractFunctionSet + 'static>( + pub fn register_contract_with_constructor< + 'a, + T: ContractFunctionSet + 'static, + A: ConstructorArgs, + >( &self, contract_id: impl Into>, contract: T, - constructor_args: Vec, + constructor_args: A, ) -> Address { struct InternalContractFunctionSet(pub(crate) T); impl internal::ContractFunctionSet for InternalContractFunctionSet { @@ -703,7 +707,7 @@ impl Env { .register_test_contract_with_constructor( contract_id.to_object(), Rc::new(InternalContractFunctionSet(contract)), - constructor_args.to_object(), + constructor_args.into_val(self).to_object(), ) .unwrap(); contract_id diff --git a/soroban-sdk/src/testutils.rs b/soroban-sdk/src/testutils.rs index 7df726f64..61b7e0ef3 100644 --- a/soroban-sdk/src/testutils.rs +++ b/soroban-sdk/src/testutils.rs @@ -18,11 +18,42 @@ use soroban_env_host::TryIntoVal; pub mod storage; -use crate::{xdr, Env, Val, Vec}; +use crate::{xdr, Env, IntoVal, Val, Vec}; use soroban_ledger_snapshot::LedgerSnapshot; pub use crate::env::EnvTestConfig; +pub trait ConstructorArgs: IntoVal> {} + +impl ConstructorArgs for Vec {} + +macro_rules! impl_constructor_args_for_tuple { + ( $($typ:ident $idx:tt)* ) => { + impl<$($typ),*> ConstructorArgs for ($($typ,)*) + where + $($typ: IntoVal),* + { + } + }; +} + +// 0 topics +impl ConstructorArgs for () {} +// 1-13 topics +impl_constructor_args_for_tuple! { T0 0 } +impl_constructor_args_for_tuple! { T0 0 T1 1 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 } +impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 T12 12 } + #[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub struct Snapshot {