Skip to content

Commit

Permalink
Revert "feat: Disable arc_type requirement in candid for ic-utils (df…
Browse files Browse the repository at this point in the history
…inity#454)"

This reverts commit 432ff1d.
  • Loading branch information
chenyan-dfinity committed Aug 28, 2023
1 parent 728f395 commit 974b8da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
23 changes: 8 additions & 15 deletions ic-utils/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ 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 @@ -102,15 +96,14 @@ where
/// .create_canister()
/// .as_provisional_create_with_amount(None)
/// .with_effective_canister_id(effective_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,))
/// }
/// .and_then(|(canister_id,)| async move {
/// management_canister
/// .install_code(&canister_id, canister_wasm)
/// .build()
/// .unwrap()
/// .call_and_wait()
/// .await?;
/// Ok((canister_id,))
/// })
/// .call_and_wait()
/// .await?;
Expand Down
24 changes: 8 additions & 16 deletions ic-utils/src/interfaces/management_canister/builders.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Builder interfaces for some method calls of the management canister.

use crate::{
call::{AsyncCall, BoxFuture},
canister::Argument,
interfaces::management_canister::MgmtMethod,
Canister,
call::AsyncCall, canister::Argument, interfaces::management_canister::MgmtMethod, Canister,
};
use async_trait::async_trait;
use candid::{CandidType, Deserialize, Nat};
Expand Down Expand Up @@ -440,20 +437,15 @@ 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> {
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(self) -> Result<RequestId, AgentError> {
self.build()?.call().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 })

async fn call_and_wait(self) -> Result<(), AgentError> {
self.build()?.call_and_wait().await
}
}

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

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

use crate::{
call::{AsyncCall, BoxFuture, SyncCall},
call::{AsyncCall, 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 @@ -110,34 +111,28 @@ where
}

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

/// Calls the forwarded canister call on the wallet canister, and waits for the result. Equivalent to `.build().call_and_wait()`.
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 }
pub async fn call_and_wait(self) -> Result<Out, AgentError> {
self.build()?.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,
{
fn call<'async_trait>(self) -> BoxFuture<'async_trait, Result<RequestId, AgentError>>
where
Self: 'async_trait,
{
Box::pin(self.call())
async fn call(self) -> Result<RequestId, AgentError> {
self.call().await
}

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

Expand Down

0 comments on commit 974b8da

Please sign in to comment.