Asset canister in Rust

Hi! I’m quite stuck on this:

I need to store files from another canister and retrieve them as links (mostly images). So I’m trying to use the assets canister in rust. I found this crate: ic_certified_assets and I’m trying to use it. So I created my canister and this is the code inside the lib.rs file:

use ic_cdk::{init, post_upgrade, pre_upgrade};
use ic_cdk::storage::{stable_restore, stable_save};
use ic_certified_assets::StableState;

#[init]
fn init() {
    ic_certified_assets::init();
}

#[pre_upgrade]
fn pre_upgrade() {
    let state: StableState = ic_certified_assets::pre_upgrade();
    stable_save((state,)).expect("failed to save stable state");
}

#[post_upgrade]
fn post_upgrade() {
    let (stable_state,): (StableState,) =  stable_restore()
        .expect("failed to restore stable state");
    ic_certified_assets::post_upgrade(stable_state);
}

It is similar to what I read from here.

The problem is that if I try to build it, I got this error:

error[E0277]: the trait bound StableState: CandidType is not satisfied
→ src/assets/src/lib.rs:13:18
|
13 | stable_save((state,)).expect(“failed to save stable state”);
| ----------- ^^^^^ the trait CandidType is not implemented for StableState
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait CandidType:
bool
isize
i8
i16
i32
i64
i128
usize
and 113 others
= note: required for (StableState,) to implement ArgumentEncoder
note: required by a bound in stable_save
→ /Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ic-cdk-0.13.2/src/storage.rs:9:8
|
7 | pub fn stable_save(t: T) → Result<(), candid::Error>
| ----------- required by a bound in this function
8 | where
9 | T: candid::utils::ArgumentEncoder,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in stable_save

error[E0277]: the trait bound StableState: CandidType is not satisfied
→ src/assets/src/lib.rs:18:44
|
18 | let (stable_state,): (StableState,) = stable_restore()
| ^^^^^^^^^^^^^^ the trait CandidType is not implemented for StableState
|
= help: the following other types implement trait CandidType:
bool
isize
i8
i16
i32
i64
i128
usize
and 113 others
= note: required for (StableState,) to implement for<'de> ArgumentDecoder<'de>
note: required by a bound in stable_restore
→ /Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ic-cdk-0.13.2/src/storage.rs:19:8
|
17 | pub fn stable_restore() → Result<T, String>
| -------------- required by a bound in this function
18 | where
19 | T: for<'de> candid::utils::ArgumentDecoder<'de>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in stable_restore

For more information about this error, try rustc --explain E0277.
error: could not compile assets (lib) due to 2 previous errors

Do you have some idea to fix this? Or better ways to deploy an assets canister in rust?

1 Like

There is better way, you can store them directly on stable memory using stable-structures, and ignore the pre/post_upgrade!

3 Likes

Thanks for the advise! Is it possible to serve images as https://.icp0.io/ using stable structures? I don’t know if there are some examples I can learn from, but would be very useful :smile:

Stable is about the storing, Other things are the same!

1 Like

For what it’s worth, you can “just” spin up a satellite on Juno, and you get that with its storage capabilities out of the box.

1 Like

I considered Juno, but I need to implement custom logic for allowing upload and deletion…

With serverless functions, you can implement synchronous custom assertions and asynchronous post-hooks logic. Not a call to action, just saying it for the record. :wink:

Hope you will get an answer to your original question.

1 Like

CandidType from candid <0.10 is not the same / compatible with CandidType from candid >= 0.10. I suspect this is where the problem comes from. You can check with cargo tree which crate uses which candid version. Solutions would be to either downgrade everything to 0.9 or to use the sdk repo on a current commit instead of the (very old) published version

Related: some discussion about the future of the ic-certified-assets crate:

1 Like