Share struct for multiple canisters

I’ve got a project with multiple Rust canisters, is it possible to share easily the same struct accross these?

Like having:

my_project/src/canister_a/src/lib.rs
my_project/src/canister_b/src/lib.rs
my_project/src/canister_c/src/lib.rs

That share the same

my_project/src/shared/types.rs

Which contains something like

pub mod types {
    pub struct Args {
        pub something: u64
    }
}

Update actually same question for any modules because it could be handy to share utils too

Just like you’re able to use the same copy of a library from cargo.toml in your canisters, you are also able to reuse your own libraries.

I don’t know what is your project structure, but for a multi-canister setup I would recommend putting each canister into a separate crate. So you would have something like this:

my_workspace/
  canister_a/
    src/
    cargo.toml
  canister_b/
    src/
    cargo.toml
  canister_c/
    src/
    cargo.toml

  # this one is special
  shared_lib/
    src/
      types.rs
    cargo.toml

If you do it like that, then you can simply put

shared_lib: { path: "../shared_lib" }

into cargo.toml of each of your canister.

Doing like this allows you to easily decouple canisters from each other later when your project scales.

1 Like

Nice, thanks! That should do, I’ll give it a try later (since I posted my question I broke everything in my project :rofl:)

1 Like

So, I’m really a noob :sweat_smile:.

Once you have added the reference shared = { path = "../shared" } in a canister, how do you use your types modules?

Tried use crate::shared::types::{Args}; but I am missing something…

  shared/
    src/
      types.rs
    cargo.toml
  canister_a/
    src/
      lib.rs <-- here I tried to use `use crate::shared::types::{Args};`
    cargo.toml <-- here I added the path

check rust book and rust cargo book

What IDE are you using? On VSCode it should work to simply write let something:Args... And then it will highlight Args, and if you ctrl+. on that highlight it would offer to import it for you, and should be pretty good at it since you defined it in Cargo.toml.

I remember CLion having something similar, tho’ I only used it for a brief period and can’t remember the shortcut from memory.

1 Like

I’m closer, forgot to add the shared folder as a members in the root Cargo.toml.

Next one, so I probably fck up something in my toml…

use shared::types::shared::{BucketArgs};
   |     ^^^^^^ use of undeclared crate or module `shared`

Good idea @GLdev! I use Webstorm with Rust plugin. If I remove my import and use the auto-import of the editor it resolves the same usage.

Just to make sure, in the folder shared/src do you have a lib.rs that contains mod types; mod ..., right? It should work to call it like that…

Yes, even made it mod public types;.

It works out :partying_face:

No freaking clue why. I stashed my changes, hacked around, reverted everything and applied my shelfed changes again and…all good :man_shrugging:

Thanks everybody!

One of us, one of us!!! :laughing:

2 Likes