Skip to content

Commit

Permalink
stateful
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Oct 27, 2023
1 parent b65e960 commit 97a124a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
8 changes: 8 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"]
members = ["unit_struct", "build_script", "stateful"]
resolver = "2"

[workspace.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion build_script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
7 changes: 7 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
12 changes: 12 additions & 0 deletions stateful/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
46 changes: 46 additions & 0 deletions stateful/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 {
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<Mutex<Canister>> = 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
}
}

0 comments on commit 97a124a

Please sign in to comment.