Skip to content

Commit

Permalink
feat: Disable arc_type requirement in candid for ic-utils (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity authored Aug 3, 2023
1 parent 6d923cb commit 432ff1d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ic-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include = ["src", "Cargo.toml", "../LICENSE", "README.md"]

[dependencies]
async-trait = "0.1.68"
candid = { workspace = true, features = ["arc_type"] }
candid = { workspace = true }
ic-agent = { workspace = true, default-features = false }
serde = { workspace = true }
serde_bytes = { workspace = true }
Expand Down
23 changes: 15 additions & 8 deletions ic-utils/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ use ic_agent::{agent::UpdateBuilder, export::Principal, Agent, AgentError, Reque
use serde::de::DeserializeOwned;
use std::fmt;
use std::future::Future;
use std::pin::Pin;

mod expiry;
pub use expiry::Expiry;

#[cfg(target_family = "wasm")]
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
#[cfg(not(target_family = "wasm"))]
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;

/// A type that implements synchronous calls (ie. 'query' calls).
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
Expand Down Expand Up @@ -96,14 +102,15 @@ where
/// .create_canister()
/// .as_provisional_create_with_amount(None)
/// .with_effective_canister_id(effective_id)
/// .and_then(|(canister_id,)| async move {
/// management_canister
/// .install_code(&canister_id, canister_wasm)
/// .build()
/// .unwrap()
/// .call_and_wait()
/// .await?;
/// Ok((canister_id,))
/// .and_then(|(canister_id,)| {
/// let call = management_canister
/// .install_code(&canister_id, canister_wasm)
/// .build()
/// .unwrap();
/// async move {
/// call.call_and_wait().await?;
/// Ok((canister_id,))
/// }
/// })
/// .call_and_wait()
/// .await?;
Expand Down
24 changes: 16 additions & 8 deletions ic-utils/src/interfaces/management_canister/builders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Builder interfaces for some method calls of the management canister.

use crate::{
call::AsyncCall, canister::Argument, interfaces::management_canister::MgmtMethod, Canister,
call::{AsyncCall, BoxFuture},
canister::Argument,
interfaces::management_canister::MgmtMethod,
Canister,
};
use async_trait::async_trait;
use candid::{CandidType, Deserialize, Nat};
Expand Down Expand Up @@ -437,15 +440,20 @@ impl<'agent, 'canister: 'agent> InstallCodeBuilder<'agent, 'canister> {
}
}

#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
impl<'agent, 'canister: 'agent> AsyncCall<()> for InstallCodeBuilder<'agent, 'canister> {
async fn call(self) -> Result<RequestId, AgentError> {
self.build()?.call().await
fn call<'async_trait>(self) -> BoxFuture<'async_trait, Result<RequestId, AgentError>>
where
Self: 'async_trait,
{
let call_res = self.build();
Box::pin(async move { call_res?.call().await })
}

async fn call_and_wait(self) -> Result<(), AgentError> {
self.build()?.call_and_wait().await
fn call_and_wait<'async_trait>(self) -> BoxFuture<'async_trait, Result<(), AgentError>>
where
Self: 'async_trait,
{
let call_res = self.build();
Box::pin(async move { call_res?.call_and_wait().await })
}
}

Expand Down
31 changes: 18 additions & 13 deletions ic-utils/src/interfaces/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
//!
//! [cycles wallet]: https://github.com/dfinity/cycles-wallet

use std::ops::Deref;
use std::{future::Future, ops::Deref};

use crate::{
call::{AsyncCall, SyncCall},
call::{AsyncCall, BoxFuture, SyncCall},
canister::Argument,
interfaces::management_canister::{
attributes::{ComputeAllocation, FreezingThreshold, MemoryAllocation},
builders::CanisterSettings,
},
Canister,
};
use async_trait::async_trait;
use candid::{decode_args, utils::ArgumentDecoder, CandidType, Deserialize, Nat};
use ic_agent::{agent::RejectCode, export::Principal, Agent, AgentError, RequestId};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -111,28 +110,34 @@ where
}

/// Calls the forwarded canister call on the wallet canister. Equivalent to `.build().call()`.
pub async fn call(self) -> Result<RequestId, AgentError> {
self.build()?.call().await
pub fn call(self) -> impl Future<Output = Result<RequestId, AgentError>> + 'agent {
let call_res = self.build();
async move { call_res?.call().await }
}

/// Calls the forwarded canister call on the wallet canister, and waits for the result. Equivalent to `.build().call_and_wait()`.
pub async fn call_and_wait(self) -> Result<Out, AgentError> {
self.build()?.call_and_wait().await
pub fn call_and_wait(self) -> impl Future<Output = Result<Out, AgentError>> + 'agent {
let call_res = self.build();
async move { call_res?.call_and_wait().await }
}
}

#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
impl<'agent, 'canister: 'agent, Out> AsyncCall<Out> for CallForwarder<'agent, 'canister, Out>
where
Out: for<'de> ArgumentDecoder<'de> + Send + Sync,
{
async fn call(self) -> Result<RequestId, AgentError> {
self.call().await
fn call<'async_trait>(self) -> BoxFuture<'async_trait, Result<RequestId, AgentError>>
where
Self: 'async_trait,
{
Box::pin(self.call())
}

async fn call_and_wait(self) -> Result<Out, AgentError> {
self.call_and_wait().await
fn call_and_wait<'async_trait>(self) -> BoxFuture<'async_trait, Result<Out, AgentError>>
where
Self: 'async_trait,
{
Box::pin(self.call_and_wait())
}
}

Expand Down
1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[toolchain]
channel = "1.65.0"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]

0 comments on commit 432ff1d

Please sign in to comment.