Skip to content

Commit

Permalink
change template to take reference for call
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Jul 24, 2024
1 parent 19567a2 commit 66df8a9
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion rust/candid_parser/src/bindings/rust_agent.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Result<T> = std::result::Result<T, ic_agent::AgentError>;
pub struct {{PascalCase service_name}}<'a>(pub Principal, pub &'a ic_agent::Agent);
impl<'a> {{PascalCase service_name}}<'a> {
{{#each methods}}
pub async fn {{this.name}}(&self{{#each this.args}}, {{this.0}}: {{this.1}}{{/each}}) -> Result<{{vec_to_arity this.rets}}> {
pub async fn {{this.name}}(&self{{#each this.args}}, {{this.0}}: &{{this.1}}{{/each}}) -> Result<{{vec_to_arity this.rets}}> {
let args = Encode!({{#each this.args}}&{{this.0}}{{#unless @last}},{{/unless}}{{/each}})?;
let bytes = self.1.{{#if (eq this.mode "update")}}update{{else}}query{{/if}}(&self.0, "{{escape_debug this.original_name}}").with_arg(args).{{#if (eq this.mode "update")}}call_and_wait{{else}}call{{/if}}().await?;
Ok(Decode!(&bytes{{#each this.rets}}, {{this}}{{/each}})?)
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/bindings/rust_call.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ic_cdk::api::call::CallResult as Result;
pub struct {{PascalCase service_name}}(pub Principal);
impl {{PascalCase service_name}} {
{{#each methods}}
pub async fn {{this.name}}(&self{{#each this.args}}, {{this.0}}: {{this.1}}{{/each}}) -> Result<({{#each this.rets}}{{this}},{{/each}})> {
pub async fn {{this.name}}(&self{{#each this.args}}, {{this.0}}: &{{this.1}}{{/each}}) -> Result<({{#each this.rets}}{{this}},{{/each}})> {
ic_cdk::call(self.0, "{{escape_debug this.original_name}}", ({{#each this.args}}{{this.0}},{{/each}})).await
}
{{/each}}
Expand Down
8 changes: 4 additions & 4 deletions rust/candid_parser/tests/assets/ok/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub struct O(pub Option<Box<O>>);

pub struct Service(pub Principal);
impl Service {
pub async fn f(&self, arg0: candid::Nat) -> Result<(H,)> {
pub async fn f(&self, arg0: &candid::Nat) -> Result<(H,)> {
ic_cdk::call(self.0, "f", (arg0,)).await
}
pub async fn g(&self, arg0: i8) -> Result<(i8,)> {
pub async fn g(&self, arg0: &i8) -> Result<(i8,)> {
ic_cdk::call(self.0, "g", (arg0,)).await
}
pub async fn h(&self, arg0: i8) -> Result<(i8,)> {
pub async fn h(&self, arg0: &i8) -> Result<(i8,)> {
ic_cdk::call(self.0, "h", (arg0,)).await
}
pub async fn o(&self, arg0: O) -> Result<(O,)> {
pub async fn o(&self, arg0: &O) -> Result<(O,)> {
ic_cdk::call(self.0, "o", (arg0,)).await
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/tests/assets/ok/cyclic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type X = Y;

pub struct Service(pub Principal);
impl Service {
pub async fn f(&self, arg0: A, arg1: B, arg2: C, arg3: X, arg4: Y, arg5: Z) -> Result<()> {
pub async fn f(&self, arg0: &A, arg1: &B, arg2: &C, arg3: &X, arg4: &Y, arg5: &Z) -> Result<()> {
ic_cdk::call(self.0, "f", (arg0,arg1,arg2,arg3,arg4,arg5,)).await
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/candid_parser/tests/assets/ok/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub enum HRet { #[serde(rename="a")] A(Box<T>), #[serde(rename="b")] B{} }

pub struct Service(pub Principal);
impl Service {
pub async fn f(&self, arg0: FArg) -> Result<(FRet,)> {
pub async fn f(&self, arg0: &FArg) -> Result<(FRet,)> {
ic_cdk::call(self.0, "f", (arg0,)).await
}
pub async fn g(&self, arg0: T) -> Result<(GRet,)> {
pub async fn g(&self, arg0: &T) -> Result<(GRet,)> {
ic_cdk::call(self.0, "g", (arg0,)).await
}
pub async fn h(&self, arg0: (T,candid::Empty,)) -> Result<(HRet,)> {
pub async fn h(&self, arg0: &(T,candid::Empty,)) -> Result<(HRet,)> {
ic_cdk::call(self.0, "h", (arg0,)).await
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/tests/assets/ok/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct T {

pub struct Service(pub Principal);
impl Service {
pub async fn _2635468193_(&self, arg0: T) -> Result<()> {
pub async fn _2635468193_(&self, arg0: &T) -> Result<()> {
ic_cdk::call(self.0, "\n\'\"\'\'\"\"\r\t", (arg0,)).await
}
}
Expand Down
16 changes: 8 additions & 8 deletions rust/candid_parser/tests/assets/ok/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,28 @@ pub(crate) enum Error { #[serde(rename="a")] A, #[serde(rename="b")] B }

pub struct Service(pub Principal);
impl Service {
pub async fn bbbbb(&self, arg0: B) -> Result<()> {
pub async fn bbbbb(&self, arg0: &B) -> Result<()> {
ic_cdk::call(self.0, "bbbbb", (arg0,)).await
}
pub async fn f(&self, arg0: S) -> Result<()> {
pub async fn f(&self, arg0: &S) -> Result<()> {
ic_cdk::call(self.0, "f", (arg0,)).await
}
pub async fn f_1(&self, arg0: List, arg1: serde_bytes::ByteBuf, arg2: Option<bool>) -> Result<()> {
pub async fn f_1(&self, arg0: &List, arg1: &serde_bytes::ByteBuf, arg2: &Option<bool>) -> Result<()> {
ic_cdk::call(self.0, "f1", (arg0,arg1,arg2,)).await
}
pub async fn g(&self, arg0: List) -> Result<(B,Tree,Stream,)> {
pub async fn g(&self, arg0: &List) -> Result<(B,Tree,Stream,)> {
ic_cdk::call(self.0, "g", (arg0,)).await
}
pub async fn G11(&self, id: CanisterId, list: MyList, is_okay: Option<MyList>, arg3: Nested) -> Result<(i128,Broker,NestedRes,)> {
pub async fn G11(&self, id: &CanisterId, list: &MyList, is_okay: &Option<MyList>, arg3: &Nested) -> Result<(i128,Broker,NestedRes,)> {
ic_cdk::call(self.0, "g1", (id,list,is_okay,arg3,)).await
}
pub async fn h(&self, arg0: Vec<Option<String>>, arg1: HArg1, arg2: Option<MyList>) -> Result<(HRet,)> {
pub async fn h(&self, arg0: &Vec<Option<String>>, arg1: &HArg1, arg2: &Option<MyList>) -> Result<(HRet,)> {
ic_cdk::call(self.0, "h", (arg0,arg1,arg2,)).await
}
pub async fn i(&self, arg0: MyList, arg1: FArg1) -> Result<(Option<MyList>,Res,)> {
pub async fn i(&self, arg0: &MyList, arg1: &FArg1) -> Result<(Option<MyList>,Res,)> {
ic_cdk::call(self.0, "i", (arg0,arg1,)).await
}
pub async fn x(&self, arg0: A, arg1: B) -> Result<(Option<A>,Option<B>,std::result::Result<XRet2Ok, Error>,)> {
pub async fn x(&self, arg0: &A, arg1: &B) -> Result<(Option<A>,Option<B>,std::result::Result<XRet2Ok, Error>,)> {
ic_cdk::call(self.0, "x", (arg0,arg1,)).await
}
}
Expand Down
14 changes: 7 additions & 7 deletions rust/candid_parser/tests/assets/ok/fieldnat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ pub struct FooRet { pub _2_: candid::Int, pub _2: candid::Int }

pub struct Service(pub Principal);
impl Service {
pub async fn bab(&self, arg0: candid::Int, arg1: candid::Nat) -> Result<()> {
pub async fn bab(&self, arg0: &candid::Int, arg1: &candid::Nat) -> Result<()> {
ic_cdk::call(self.0, "bab", (arg0,arg1,)).await
}
pub async fn bar(&self, arg0: BarArg) -> Result<(BarRet,)> {
pub async fn bar(&self, arg0: &BarArg) -> Result<(BarRet,)> {
ic_cdk::call(self.0, "bar", (arg0,)).await
}
pub async fn bas(&self, arg0: (candid::Int,candid::Int,)) -> Result<((String,candid::Nat,),)> {
pub async fn bas(&self, arg0: &(candid::Int,candid::Int,)) -> Result<((String,candid::Nat,),)> {
ic_cdk::call(self.0, "bas", (arg0,)).await
}
pub async fn baz(&self, arg0: BazArg) -> Result<(BazRet,)> {
pub async fn baz(&self, arg0: &BazArg) -> Result<(BazRet,)> {
ic_cdk::call(self.0, "baz", (arg0,)).await
}
pub async fn bba(&self, arg0: Tuple) -> Result<(NonTuple,)> {
pub async fn bba(&self, arg0: &Tuple) -> Result<(NonTuple,)> {
ic_cdk::call(self.0, "bba", (arg0,)).await
}
pub async fn bib(&self, arg0: (candid::Int,)) -> Result<(BibRet,)> {
pub async fn bib(&self, arg0: &(candid::Int,)) -> Result<(BibRet,)> {
ic_cdk::call(self.0, "bib", (arg0,)).await
}
pub async fn foo(&self, arg0: FooArg) -> Result<(FooRet,)> {
pub async fn foo(&self, arg0: &FooArg) -> Result<(FooRet,)> {
ic_cdk::call(self.0, "foo", (arg0,)).await
}
}
Expand Down
20 changes: 10 additions & 10 deletions rust/candid_parser/tests/assets/ok/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ impl Service {
pub async fn oneway(&self) -> Result<()> {
ic_cdk::call(self.0, "Oneway", ()).await
}
pub async fn f(&self, arg0: O) -> Result<(O,)> {
pub async fn f(&self, arg0: &O) -> Result<(O,)> {
ic_cdk::call(self.0, "f_", (arg0,)).await
}
pub async fn field(&self, arg0: FieldArg) -> Result<(FieldRet,)> {
pub async fn field(&self, arg0: &FieldArg) -> Result<(FieldRet,)> {
ic_cdk::call(self.0, "field", (arg0,)).await
}
pub async fn fieldnat(&self, arg0: FieldnatArg) -> Result<((candid::Int,),)> {
pub async fn fieldnat(&self, arg0: &FieldnatArg) -> Result<((candid::Int,),)> {
ic_cdk::call(self.0, "fieldnat", (arg0,)).await
}
pub async fn oneway(&self, arg0: u8) -> Result<()> {
pub async fn oneway(&self, arg0: &u8) -> Result<()> {
ic_cdk::call(self.0, "oneway", (arg0,)).await
}
pub async fn oneway(&self, arg0: u8) -> Result<()> {
pub async fn oneway(&self, arg0: &u8) -> Result<()> {
ic_cdk::call(self.0, "oneway_", (arg0,)).await
}
pub async fn query(&self, arg0: serde_bytes::ByteBuf) -> Result<(serde_bytes::ByteBuf,)> {
pub async fn query(&self, arg0: &serde_bytes::ByteBuf) -> Result<(serde_bytes::ByteBuf,)> {
ic_cdk::call(self.0, "query", (arg0,)).await
}
pub async fn r#return(&self, arg0: O) -> Result<(O,)> {
pub async fn r#return(&self, arg0: &O) -> Result<(O,)> {
ic_cdk::call(self.0, "return", (arg0,)).await
}
pub async fn service(&self, arg0: Return) -> Result<()> {
pub async fn service(&self, arg0: &Return) -> Result<()> {
ic_cdk::call(self.0, "service", (arg0,)).await
}
pub async fn tuple(&self, arg0: (candid::Int,serde_bytes::ByteBuf,String,)) -> Result<((candid::Int,u8,),)> {
pub async fn tuple(&self, arg0: &(candid::Int,serde_bytes::ByteBuf,String,)) -> Result<((candid::Int,u8,),)> {
ic_cdk::call(self.0, "tuple", (arg0,)).await
}
pub async fn variant(&self, arg0: VariantArg) -> Result<()> {
pub async fn variant(&self, arg0: &VariantArg) -> Result<()> {
ic_cdk::call(self.0, "variant", (arg0,)).await
}
}
Expand Down
36 changes: 18 additions & 18 deletions rust/candid_parser/tests/assets/ok/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,67 +205,67 @@ pub struct UpdateSettingsArg {

pub struct Service<'a>(pub Principal, pub &'a ic_agent::Agent);
impl<'a> Service<'a> {
pub async fn bitcoin_get_balance(&self, arg0: GetBalanceRequest) -> Result<Satoshi> {
pub async fn bitcoin_get_balance(&self, arg0: &GetBalanceRequest) -> Result<Satoshi> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "bitcoin_get_balance").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, Satoshi)?)
}
pub async fn bitcoin_get_current_fee_percentiles(&self, arg0: GetCurrentFeePercentilesRequest) -> Result<Vec<MillisatoshiPerByte>> {
pub async fn bitcoin_get_current_fee_percentiles(&self, arg0: &GetCurrentFeePercentilesRequest) -> Result<Vec<MillisatoshiPerByte>> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "bitcoin_get_current_fee_percentiles").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, Vec<MillisatoshiPerByte>)?)
}
pub async fn bitcoin_get_utxos(&self, arg0: GetUtxosRequest) -> Result<GetUtxosResponse> {
pub async fn bitcoin_get_utxos(&self, arg0: &GetUtxosRequest) -> Result<GetUtxosResponse> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "bitcoin_get_utxos").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, GetUtxosResponse)?)
}
pub async fn bitcoin_send_transaction(&self, arg0: SendTransactionRequest) -> Result<()> {
pub async fn bitcoin_send_transaction(&self, arg0: &SendTransactionRequest) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "bitcoin_send_transaction").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn canister_status(&self, arg0: CanisterStatusArg) -> Result<CanisterStatusRet> {
pub async fn canister_status(&self, arg0: &CanisterStatusArg) -> Result<CanisterStatusRet> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "canister_status").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, CanisterStatusRet)?)
}
pub async fn create_canister(&self, arg0: CreateCanisterArg) -> Result<CreateCanisterRet> {
pub async fn create_canister(&self, arg0: &CreateCanisterArg) -> Result<CreateCanisterRet> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "create_canister").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, CreateCanisterRet)?)
}
pub async fn delete_canister(&self, arg0: DeleteCanisterArg) -> Result<()> {
pub async fn delete_canister(&self, arg0: &DeleteCanisterArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "delete_canister").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn deposit_cycles(&self, arg0: DepositCyclesArg) -> Result<()> {
pub async fn deposit_cycles(&self, arg0: &DepositCyclesArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "deposit_cycles").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn ecdsa_public_key(&self, arg0: EcdsaPublicKeyArg) -> Result<EcdsaPublicKeyRet> {
pub async fn ecdsa_public_key(&self, arg0: &EcdsaPublicKeyArg) -> Result<EcdsaPublicKeyRet> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "ecdsa_public_key").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, EcdsaPublicKeyRet)?)
}
pub async fn http_request(&self, arg0: HttpRequestArg) -> Result<HttpResponse> {
pub async fn http_request(&self, arg0: &HttpRequestArg) -> Result<HttpResponse> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "http_request").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, HttpResponse)?)
}
pub async fn install_code(&self, arg0: InstallCodeArg) -> Result<()> {
pub async fn install_code(&self, arg0: &InstallCodeArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "install_code").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn provisional_create_canister_with_cycles(&self, arg0: ProvisionalCreateCanisterWithCyclesArg) -> Result<ProvisionalCreateCanisterWithCyclesRet> {
pub async fn provisional_create_canister_with_cycles(&self, arg0: &ProvisionalCreateCanisterWithCyclesArg) -> Result<ProvisionalCreateCanisterWithCyclesRet> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "provisional_create_canister_with_cycles").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, ProvisionalCreateCanisterWithCyclesRet)?)
}
pub async fn provisional_top_up_canister(&self, arg0: ProvisionalTopUpCanisterArg) -> Result<()> {
pub async fn provisional_top_up_canister(&self, arg0: &ProvisionalTopUpCanisterArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "provisional_top_up_canister").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
Expand All @@ -275,27 +275,27 @@ impl<'a> Service<'a> {
let bytes = self.1.update(&self.0, "raw_rand").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, serde_bytes::ByteBuf)?)
}
pub async fn sign_with_ecdsa(&self, arg0: SignWithEcdsaArg) -> Result<SignWithEcdsaRet> {
pub async fn sign_with_ecdsa(&self, arg0: &SignWithEcdsaArg) -> Result<SignWithEcdsaRet> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "sign_with_ecdsa").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes, SignWithEcdsaRet)?)
}
pub async fn start_canister(&self, arg0: StartCanisterArg) -> Result<()> {
pub async fn start_canister(&self, arg0: &StartCanisterArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "start_canister").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn stop_canister(&self, arg0: StopCanisterArg) -> Result<()> {
pub async fn stop_canister(&self, arg0: &StopCanisterArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "stop_canister").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn uninstall_code(&self, arg0: UninstallCodeArg) -> Result<()> {
pub async fn uninstall_code(&self, arg0: &UninstallCodeArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "uninstall_code").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
}
pub async fn update_settings(&self, arg0: UpdateSettingsArg) -> Result<()> {
pub async fn update_settings(&self, arg0: &UpdateSettingsArg) -> Result<()> {
let args = Encode!(&arg0)?;
let bytes = self.1.update(&self.0, "update_settings").with_arg(args).call_and_wait().await?;
Ok(Decode!(&bytes)?)
Expand Down
4 changes: 2 additions & 2 deletions rust/candid_parser/tests/assets/ok/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ candid::define_service!(pub S : {

pub struct Service(pub Principal);
impl Service {
pub async fn f(&self, arg0: S) -> Result<()> {
pub async fn f(&self, arg0: &S) -> Result<()> {
ic_cdk::call(self.0, "f", (arg0,)).await
}
pub async fn g(&self, arg0: List) -> Result<(B,Tree,Stream,)> {
pub async fn g(&self, arg0: &List) -> Result<(B,Tree,Stream,)> {
ic_cdk::call(self.0, "g", (arg0,)).await
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/candid_parser/tests/assets/ok/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ pub enum B {

pub struct Service(pub Principal);
impl Service {
pub async fn _0_(&self, arg0: candid::Nat) -> Result<(candid::Nat,)> {
pub async fn _0_(&self, arg0: &candid::Nat) -> Result<(candid::Nat,)> {
ic_cdk::call(self.0, "", (arg0,)).await
}
pub async fn _356566390_(&self) -> Result<()> {
ic_cdk::call(self.0, "✈️ 🚗 ⛱️ ", ()).await
}
pub async fn _3300066460_(&self, arg0: A) -> Result<(B,)> {
pub async fn _3300066460_(&self, arg0: &A) -> Result<(B,)> {
ic_cdk::call(self.0, "函数名", (arg0,)).await
}
pub async fn _2669435454_(&self, arg0: candid::Nat) -> Result<(candid::Nat,)> {
pub async fn _2669435454_(&self, arg0: &candid::Nat) -> Result<(candid::Nat,)> {
ic_cdk::call(self.0, "👀", (arg0,)).await
}
}
Expand Down

0 comments on commit 66df8a9

Please sign in to comment.