From 97a124a51be7976fbf967304437c7f81c83f2fc1 Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Fri, 27 Oct 2023 16:31:04 -0400 Subject: [PATCH] stateful --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 +- build_script/build.rs | 2 +- dfx.json | 7 +++++++ stateful/Cargo.toml | 12 +++++++++++ stateful/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 stateful/Cargo.toml create mode 100644 stateful/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index faf9832..4602e17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -456,6 +456,14 @@ dependencies = [ "winapi", ] +[[package]] +name = "stateful" +version = "0.1.0" +dependencies = [ + "ic-cdk", + "once_cell", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index 1cd99fe..1911c72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["unit_struct", "build_script"] +members = ["unit_struct", "build_script", "stateful"] resolver = "2" [workspace.dependencies] diff --git a/build_script/build.rs b/build_script/build.rs index c9253fc..f273376 100644 --- a/build_script/build.rs +++ b/build_script/build.rs @@ -31,5 +31,5 @@ fn main() { // https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method.compile_protos let out_dir = std::env::var("OUT_DIR").unwrap(); let path = Path::new(&out_dir).join("counter.rs"); - std::fs::write(&path, BINDING).unwrap(); + std::fs::write(path, BINDING).unwrap(); } diff --git a/dfx.json b/dfx.json index 45ab1dd..b4f814d 100644 --- a/dfx.json +++ b/dfx.json @@ -13,6 +13,13 @@ "candid": "counter.did", "wasm": "target/wasm32-unknown-unknown/release/build_script.wasm", "shrink": true + }, + "stateful": { + "type": "custom", + "build": "sh build.sh stateful", + "candid": "counter.did", + "wasm": "target/wasm32-unknown-unknown/release/stateful.wasm", + "shrink": true } } } \ No newline at end of file diff --git a/stateful/Cargo.toml b/stateful/Cargo.toml new file mode 100644 index 0000000..5a8bfb8 --- /dev/null +++ b/stateful/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "stateful" +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/stateful/src/lib.rs b/stateful/src/lib.rs new file mode 100644 index 0000000..5149014 --- /dev/null +++ b/stateful/src/lib.rs @@ -0,0 +1,46 @@ +// ic_cdk::generate!({path: "../../counter.did", service : Canister, trait: Counter}); + +// Expand Start +trait Counter { + 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(); + 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 { + fn inc(&mut self) { + self.counter += 1; + } + + fn read(&self) -> u64 { + self.counter + } +}