Rust Version of Actor Class

Is there a way to create an actor class from rust?
Is it possible to take some rust source code and generate wasm with a candid file simialr to that of an actor class in motoko:, i.e.

type Counter = 
 service {
  "inc": () -> ();
  "read": () -> (nat) query;
  "write": (nat) -> ();
 };
service: () -> Counter

Yes! The rust CDK has a few examples, one of which is similar to the Motoko counter example.

1 Like

Thanks Eric, I had seen these but they all seem to initialise an actor rather than an actor class (unless I am mistaken). I’m looking for a factory method that can be used to create more instances, something like this in the service definition:

type Counter = 
 service {
  "inc": () -> ();
  "read": () -> (nat) query;
  "write": (nat) -> ();
 };
service: () -> Counter

And then perhaps reimported with a macro like this?

#[import(canister = "counter")]
struct CounterCanister;

Do you have any tips?

This doesn’t look like the way of class,
for example (motoko):

 let actor = await ActorClass.Actor(arguments); 

I did not find such a way in rust

I haven’t tested this out myself yet but I think in the rust you can compile the canister(actor-class) in a separate-folder to wasm, then take those wasm-bytes and hardcode them into the rust canister that is creating the new actors and have it call the create_canister method on the management-canister with the wasm-bytes as the parameter for the wasm-module.

1 Like

Thanks, unfortunately the main reason I am wanting to do this is to create actors through a factory method locally on dfx, bypassing the lack of management canister.

Even with local deployment, there should be a ManagementCanister available (unless something has changed recently).

The Motoko compiler does pretty much what levi describes, so if that (still) works locally, the Rust code should too.

But I think this would be the solution if anyone has a

When I try to access it through Principal::management_canister_id(), seems to refer to an empty canister? Do you know how I could find the id locally?

This code shows how dfx is doing it, but it seems the management canister is accessed in a different way?

The management-canister’s-principal is the empty blob.

It should just be “aaaaa-aa”. Not sure how to construct that in Rust, sorry.

If it’s any help, here part of the Motoko code used by the Motoko compiler:

Thanks, possibly a mistake on my end. Will try again and revert to you.