-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "build_script" | ||
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 | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::path::Path; | ||
|
||
static BINDING: &str = "trait Counter { | ||
fn inc(); | ||
fn read() -> u64; | ||
} | ||
#[export_name = \"canister_update inc\"] | ||
fn __canister_method_inc() { | ||
ic_cdk::setup(); | ||
ic_cdk::spawn(async { | ||
let _result = Canister::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::read(); | ||
ic_cdk::api::call::reply((result,)) | ||
}); | ||
}"; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=../counter.did"); | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
// ic_cdk_bindgen::generate_provider("../counter.did").unwrap() | ||
// We can also introduce Config similar to | ||
// 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
include!(concat!(env!("OUT_DIR"), "/counter.rs")); | ||
|
||
use std::cell::Cell; | ||
|
||
thread_local! { | ||
static COUNTER: Cell<u64> = Cell::new(0); | ||
} | ||
|
||
struct Canister; | ||
|
||
impl Counter for Canister { | ||
fn inc() { | ||
COUNTER.set(COUNTER.get() + 1); | ||
} | ||
|
||
fn read() -> u64 { | ||
COUNTER.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters