Skip to content

Commit

Permalink
async_state which doesn't work well
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Dec 14, 2023
1 parent 4a7dc0a commit a5011c2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 14 deletions.
12 changes: 4 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 12 additions & 0 deletions async_state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
46 changes: 46 additions & 0 deletions async_state/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<Canister>> = 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
}
}
1 change: 0 additions & 1 deletion async_trait/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
ic-cdk.workspace = true
async-trait = "0.1"

[lib]
crate-type = ["cdylib"]
2 changes: 0 additions & 2 deletions async_trait/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,7 +33,6 @@ thread_local! {

struct Canister;

#[async_trait::async_trait]
impl Counter for Canister {
async fn inc() {
COUNTER.set(COUNTER.get() + 1);
Expand Down
7 changes: 7 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "beta"
targets = ["wasm32-unknown-unknown"]

0 comments on commit a5011c2

Please sign in to comment.