Skip to content

Commit

Permalink
Support calling ctors with tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Sep 18, 2024
1 parent 6ede50a commit 6ac6935
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
18 changes: 11 additions & 7 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -622,7 +622,7 @@ impl Env {
contract_id: impl Into<Option<&'a Address>>,
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.
Expand Down Expand Up @@ -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<Option<&'a Address>>,
contract: T,
constructor_args: Vec<Val>,
constructor_args: A,
) -> Address {
struct InternalContractFunctionSet<T: ContractFunctionSet>(pub(crate) T);
impl<T: ContractFunctionSet> internal::ContractFunctionSet for InternalContractFunctionSet<T> {
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Env, Vec<Val>> {}

impl<T> ConstructorArgs for Vec<T> {}

macro_rules! impl_constructor_args_for_tuple {
( $($typ:ident $idx:tt)* ) => {
impl<$($typ),*> ConstructorArgs for ($($typ,)*)
where
$($typ: IntoVal<Env, Val>),*
{
}
};
}

// 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 {
Expand Down

0 comments on commit 6ac6935

Please sign in to comment.