website icon indicating copy to clipboard operation
website copied to clipboard

Example on https://riker.rs/actors/ does not compile

Open werner291 opened this issue 4 years ago • 1 comments

Hello,

the example here (found on https://riker.rs/actors/):

use std::time::Duration;
use riker::actors::*;

struct MyActor;

// implement the Actor trait
impl Actor for MyActor {
    type Msg = String;

    fn recv(&mut self,
            _ctx: &Context<String>,
            msg: String,
            _sender: Sender) {

        println!("Received: {}", msg);
    }
}

// start the system and create an actor
fn main() {
    let sys = ActorSystem::new().unwrap();

    let my_actor = sys.actor_of::<MyActor>("my-actor").unwrap();

    my_actor.tell("Hello my actor!".to_string(), None);

    // force main to wait before exiting program
    std::thread::sleep(Duration::from_millis(500));
}

does not compile, because:

error[E0277]: the trait bound `MyActor: Default` is not satisfied
  --> src/main.rs:23:24
   |
23 |     let my_actor = sys.actor_of::<MyActor>("my-actor").unwrap();
   |                        ^^^^^^^^ the trait `Default` is not implemented for `MyActor`
   |
   = note: required because of the requirements on the impl of `ActorFactory` for `MyActor`

This is on riker = "0.4", with cargo 1.50.0 (f04e7fab7 2021-02-04) and rustc 1.50.0 (cb75ad5db 2021-02-10).

werner291 avatar Feb 14 '21 12:02 werner291

Simply implementing as follows seems to work:

impl Default for MyActor {
    fn default() -> Self {
        Self
    }
}

However, it's probably a good idea to add some text about how actors are instantiated, since Riker manages the lifetime of the actors.

werner291 avatar Feb 14 '21 12:02 werner291