Skip to content

Commit

Permalink
async_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Oct 27, 2023
1 parent 97a124a commit 8fce256
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["unit_struct", "build_script", "stateful"]
members = ["unit_struct", "build_script", "stateful", "async_trait"]
resolver = "2"

[workspace.dependencies]
Expand Down
13 changes: 13 additions & 0 deletions async_trait/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "async_trait"
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
async-trait = "0.1"

[lib]
crate-type = ["cdylib"]
46 changes: 46 additions & 0 deletions async_trait/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
#[async_trait::async_trait]
trait Counter {
async fn inc();
async fn read() -> u64;
}

#[export_name = "canister_update inc"]
fn __canister_method_inc() {
ic_cdk::setup();
ic_cdk::spawn(async {
let _result = Canister::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::read().await;
ic_cdk::api::call::reply((result,))
});
}
// Expand End

use std::cell::Cell;

thread_local! {
static COUNTER: Cell<u64> = Cell::new(0);
}

struct Canister;

#[async_trait::async_trait]
impl Counter for Canister {
async fn inc() {
COUNTER.set(COUNTER.get() + 1);
}

async fn read() -> u64 {
COUNTER.get()
}
}
8 changes: 0 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,3 @@ set -e
cargo build --target wasm32-unknown-unknown \
--release \
--package "$1"

# ic-wasm "$example_root/target/wasm32-unknown-unknown/release/$package.wasm" \
# -o "$example_root/target/wasm32-unknown-unknown/release/$package.wasm" \
# metadata candid:service -v public -f $did_file

# ic-wasm "$example_root/target/wasm32-unknown-unknown/release/$package.wasm" \
# -o "$example_root/target/wasm32-unknown-unknown/release/$package-opt.wasm" \
# shrink
7 changes: 7 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
"candid": "counter.did",
"wasm": "target/wasm32-unknown-unknown/release/stateful.wasm",
"shrink": true
},
"async_trait": {
"type": "custom",
"build": "sh build.sh async_trait",
"candid": "counter.did",
"wasm": "target/wasm32-unknown-unknown/release/async_trait.wasm",
"shrink": true
}
}
}

0 comments on commit 8fce256

Please sign in to comment.