diff --git a/Cargo.lock b/Cargo.lock index 479070e..9c283e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,21 +15,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] -name = "async-trait" -version = "0.1.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" +name = "async_state" +version = "0.1.0" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.38", + "ic-cdk", + "once_cell", ] [[package]] name = "async_trait" version = "0.1.0" dependencies = [ - "async-trait", "ic-cdk", ] diff --git a/Cargo.toml b/Cargo.toml index e397ba5..4453b56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,11 @@ [workspace] -members = ["unit_struct", "build_script", "stateful", "async_trait"] +members = [ + "unit_struct", + "build_script", + "stateful", + "async_trait", + "async_state", +] resolver = "2" [workspace.dependencies] diff --git a/README.md b/README.md index d49cb49..1eea4a5 100644 --- a/README.md +++ b/README.md @@ -35,5 +35,3 @@ The struct has state and the trait required methods take `&self` or `&mut self` ### async_trait The approach can work well with async. - -**Note**: Depends on `async-trait` crate. diff --git a/async_state/Cargo.toml b/async_state/Cargo.toml new file mode 100644 index 0000000..1dd718e --- /dev/null +++ b/async_state/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "async_state" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ic-cdk.workspace = true +once_cell = "1" +[lib] +crate-type = ["cdylib"] diff --git a/async_state/src/lib.rs b/async_state/src/lib.rs new file mode 100644 index 0000000..9fefceb --- /dev/null +++ b/async_state/src/lib.rs @@ -0,0 +1,46 @@ +// ic_cdk::generate!({path: "../../counter.did", service : Canister, trait: Counter}); + +// Expand Start +trait Counter { + async fn inc(&mut self); + fn read(&self) -> u64; +} + +#[export_name = "canister_update inc"] +fn __canister_method_inc() { + ic_cdk::setup(); + ic_cdk::spawn(async { + let _result = CANISTER.lock().unwrap().inc().await; + ic_cdk::api::call::reply(()) + }); +} + +#[export_name = "canister_query read"] +fn __canister_method_read() { + ic_cdk::setup(); + ic_cdk::spawn(async { + let result = CANISTER.lock().unwrap().read(); + ic_cdk::api::call::reply((result,)) + }); +} + +use once_cell::sync::Lazy; +use std::sync::Mutex; +static CANISTER: Lazy> = Lazy::new(|| Mutex::new(Default::default())); + +// Expand End + +#[derive(Default)] +struct Canister { + counter: u64, +} + +impl Counter for Canister { + async fn inc(&mut self) { + self.counter += 1; + } + + fn read(&self) -> u64 { + self.counter + } +} diff --git a/async_trait/Cargo.toml b/async_trait/Cargo.toml index 41a6398..b82d2b2 100644 --- a/async_trait/Cargo.toml +++ b/async_trait/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] ic-cdk.workspace = true -async-trait = "0.1" [lib] crate-type = ["cdylib"] diff --git a/async_trait/src/lib.rs b/async_trait/src/lib.rs index 1206361..eb645a8 100644 --- a/async_trait/src/lib.rs +++ b/async_trait/src/lib.rs @@ -1,7 +1,6 @@ // ic_cdk::generate!({path: "../../counter.did", service : Canister, trait: Counter}); // Expand Start -#[async_trait::async_trait] trait Counter { async fn inc(); async fn read() -> u64; @@ -34,7 +33,6 @@ thread_local! { struct Canister; -#[async_trait::async_trait] impl Counter for Canister { async fn inc() { COUNTER.set(COUNTER.get() + 1); diff --git a/dfx.json b/dfx.json index 6027d4c..c39d0f7 100644 --- a/dfx.json +++ b/dfx.json @@ -27,6 +27,13 @@ "candid": "counter.did", "wasm": "target/wasm32-unknown-unknown/release/async_trait.wasm", "shrink": true + }, + "async_state": { + "type": "custom", + "build": "sh build.sh async_state", + "candid": "counter.did", + "wasm": "target/wasm32-unknown-unknown/release/async_state.wasm", + "shrink": true } } } \ No newline at end of file diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..367314e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "beta" +targets = ["wasm32-unknown-unknown"]