This is a wrapper function for ic-stable-structures that allows us to write directly to stable memory via an API.
// memory_write
// @todo this isnt working
#[::ic::cdk::update(guard = "guard_creator")]
pub fn memory_write(memory_id: u8, chunk_offset: u32, src: &[u8]) -> Result<(), String> {
let memory = memory_get(memory_id);
::ic::db::memory::write(&memory, chunk_offset, &src)
}
and I get this error :
error: implementation of `ArgumentDecoder` is not general enough
--> src/canister/global/src/lib.rs:13:1
|
13 | init!();
| ^^^^^^^ implementation of `ArgumentDecoder` is not general enough
|
= note: `(u8, u32, &[u8])` must implement `ArgumentDecoder<'0>`, for any lifetime `'0`...
= note: ...but it actually implements `ArgumentDecoder<'1>`, for some specific lifetime `'1`
= note: this error originates in the attribute macro `::ic::cdk::update` (in Nightly builds, run with -Z macro-backtrace for more info)
Maybe I’m doing it wrong but that’s a weird error!
Here is the implementation of the guard functions
// guard_creator
// used as a guard function
pub fn guard_creator() -> Result<(), String> {
let caller = api::caller();
if *CREATOR == caller {
Ok(())
} else {
Err(format!("caller '{caller}' is not creator"))
}
}
// guard_controller
// NOTE: can't be used as guard right now because async
/*
pub async fn is_controller() -> Result<(), String> {
let caller = api::caller();
if this::canister().has_controller(&caller).await {
Ok(())
} else {
Err("caller is not controller".to_string())
}
}
*/