diff --git a/src/actor.rs b/src/actor.rs index 64508b6c..d8cb7b67 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -210,60 +210,63 @@ impl Actor for Box { /// attribute macro and implemented for each message type to receive. /// /// # Examples -/// -/// ``` -/// # use riker::actors::*; -/// -/// #[derive(Clone, Debug)] -/// pub struct Foo; -/// #[derive(Clone, Debug)] -/// pub struct Bar; -/// #[actor(Foo, Bar)] // <-- set our actor to receive Foo and Bar types -/// #[derive(Default)] -/// struct MyActor; -/// -/// impl Actor for MyActor { -/// type Msg = MyActorMsg; // <-- MyActorMsg is provided for us -/// -/// fn recv(&mut self, -/// ctx: &Context, -/// msg: Self::Msg, -/// sender: Sender) { -/// self.receive(ctx, msg, sender); // <-- call the respective implementation -/// } -/// } -/// -/// impl Receive for MyActor { -/// type Msg = MyActorMsg; -/// -/// fn receive(&mut self, -/// ctx: &Context, -/// msg: Foo, // <-- receive Foo -/// sender: Sender) { -/// println!("Received a Foo"); -/// } -/// } -/// -/// impl Receive for MyActor { -/// type Msg = MyActorMsg; -/// -/// fn receive(&mut self, -/// ctx: &Context, -/// msg: Bar, // <-- receive Bar -/// sender: Sender) { -/// println!("Received a Bar"); -/// } -/// } -/// -/// #[tokio::main] -/// async fn main() { -/// let sys = ActorSystem::new().unwrap(); -/// let actor = sys.actor_of::("my-actor").unwrap(); -/// -/// actor.tell(Foo, None); -/// actor.tell(Bar, None); -/// } -/// ``` +#[cfg_attr( + not(feature = "tokio_executor"), + doc = r##" +``` +# use riker::actors::*; + +#[derive(Clone, Debug)] +pub struct Foo; +#[derive(Clone, Debug)] +pub struct Bar; +#[actor(Foo, Bar)] // <-- set our actor to receive Foo and Bar types +#[derive(Default)] +struct MyActor; + +impl Actor for MyActor { + type Msg = MyActorMsg; // <-- MyActorMsg is provided for us + + fn recv(&mut self, + ctx: &Context, + msg: Self::Msg, + sender: Sender) { + self.receive(ctx, msg, sender); // <-- call the respective implementation + } +} + +impl Receive for MyActor { + type Msg = MyActorMsg; + + fn receive(&mut self, + ctx: &Context, + msg: Foo, // <-- receive Foo + sender: Sender) { + println!("Received a Foo"); + } +} + +impl Receive for MyActor { + type Msg = MyActorMsg; + + fn receive(&mut self, + ctx: &Context, + msg: Bar, // <-- receive Bar + sender: Sender) { + println!("Received a Bar"); + } +} + +fn main() { + let sys = ActorSystem::new().unwrap(); + let actor = sys.actor_of::("my-actor").unwrap(); + + actor.tell(Foo, None); + actor.tell(Bar, None); +} +``` +"## +)] pub trait Receive { type Msg: Message; diff --git a/src/actor/props.rs b/src/actor/props.rs index 598db3da..0e714427 100644 --- a/src/actor/props.rs +++ b/src/actor/props.rs @@ -17,36 +17,40 @@ use crate::actor::Actor; pub struct Props; impl Props { + #[cfg_attr( + not(feature = "tokio_executor"), + doc = r##" /// Creates an `ActorProducer` with no factory method parameters. /// /// # Examples /// - /// ``` - /// # use riker::actors::*; - /// - /// struct User; - /// - /// impl User { - /// fn actor() -> Self { - /// User - /// } - /// } - /// - /// # impl Actor for User { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new_from(User::actor); - /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); - /// } - /// ``` + ``` + # use riker::actors::*; + + struct User; + + impl User { + fn actor() -> Self { + User + } + } + + # impl Actor for User { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new_from(User::actor); + + // start the actor and get an `ActorRef` + let actor = sys.actor_of_props("user", props).unwrap(); + } + ``` + "## + )] #[inline] pub fn new_from(creator: F) -> Arc>> where @@ -56,73 +60,76 @@ impl Props { Arc::new(Mutex::new(ActorProps::new(creator))) } + #[cfg_attr( + not(feature = "tokio_executor"), + doc = r##" /// Creates an `ActorProducer` with one or more factory method parameters. /// /// # Examples /// An actor requiring a single parameter. - /// ``` - /// # use riker::actors::*; - /// - /// struct User { - /// name: String, - /// } - /// - /// impl User { - /// fn actor(name: String) -> Self { - /// User { - /// name - /// } - /// } - /// } - /// - /// # impl Actor for User { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new_from_args(User::actor, "Naomi Nagata".into()); - /// - /// let actor = sys.actor_of_props("user", props).unwrap(); - /// } - /// ``` + ``` + # use riker::actors::*; + + struct User { + name: String, + } + + impl User { + fn actor(name: String) -> Self { + User { + name + } + } + } + + # impl Actor for User { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new_from_args(User::actor, "Naomi Nagata".into()); + + let actor = sys.actor_of_props("user", props).unwrap(); + } + ``` /// An actor requiring multiple parameters. - /// ``` - /// # use riker::actors::*; - /// - /// struct BankAccount { - /// name: String, - /// number: String, - /// } - /// - /// impl BankAccount { - /// fn actor((name, number): (String, String)) -> Self { - /// BankAccount { - /// name, - /// number - /// } - /// } - /// } - /// - /// # impl Actor for BankAccount { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new_from_args(BankAccount::actor, - /// ("James Holden".into(), "12345678".into())); - /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("bank_account", props).unwrap(); - /// } - /// ``` + ``` + # use riker::actors::*; + + struct BankAccount { + name: String, + number: String, + } + + impl BankAccount { + fn actor((name, number): (String, String)) -> Self { + BankAccount { + name, + number + } + } + } + + # impl Actor for BankAccount { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new_from_args(BankAccount::actor, + ("James Holden".into(), "12345678".into())); + + // start the actor and get an `ActorRef` + let actor = sys.actor_of_props("bank_account", props).unwrap(); + } + ``` + "## + )] #[inline] pub fn new_from_args( creator: F, @@ -136,61 +143,64 @@ impl Props { Arc::new(Mutex::new(ActorPropsWithArgs::new(creator, args))) } + #[cfg_attr( + not(feature = "tokio_executor"), + doc = r##" /// Creates an `ActorProducer` from default constructible type with no factory method parameters. /// /// # Examples /// - /// ``` - /// # use riker::actors::*; - /// - /// #[derive(Default)] - /// struct User; - /// - /// # impl Actor for User { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new::(); - /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); - /// } - /// ``` - /// Creates an `ActorProducer` from a type which implements ActorFactory with no factory method parameters. - /// - /// # Examples - /// - /// ``` - /// # use riker::actors::*; - /// - /// struct User; - /// - /// impl ActorFactory for User { - /// fn create() -> Self { - /// User - /// } - /// } - /// - /// # impl Actor for User { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new::(); - /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); - /// } - /// ``` + ``` + # use riker::actors::*; + + #[derive(Default)] + struct User; + + # impl Actor for User { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new::(); + + // start the actor and get an `ActorRef` + let actor = sys.actor_of_props("user", props).unwrap(); + } + ``` + Creates an `ActorProducer` from a type which implements ActorFactory with no factory method parameters. + + # Examples + + ``` + # use riker::actors::*; + + struct User; + + impl ActorFactory for User { + fn create() -> Self { + User + } + } + + # impl Actor for User { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new::(); + + // start the actor and get an `ActorRef` + let actor = sys.actor_of_props("user", props).unwrap(); + } + ``` + "## + )] #[inline] pub fn new() -> Arc>> where @@ -199,73 +209,76 @@ impl Props { Self::new_from(A::create) } + #[cfg_attr( + not(feature = "tokio_executor"), + doc = r##" /// Creates an `ActorProducer` from a type which implements ActorFactoryArgs with one or more factory method parameters. /// /// # Examples /// An actor requiring a single parameter. - /// ``` - /// # use riker::actors::*; - /// - /// struct User { - /// name: String, - /// } - /// - /// impl ActorFactoryArgs for User { - /// fn create_args(name: String) -> Self { - /// User { - /// name - /// } - /// } - /// } - /// - /// # impl Actor for User { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new_args::("Naomi Nagata".into()); - /// - /// let actor = sys.actor_of_props("user", props).unwrap(); - /// } - /// ``` - /// An actor requiring multiple parameters. - /// ``` - /// # use riker::actors::*; - /// - /// struct BankAccount { - /// name: String, - /// number: String, - /// } - /// - /// impl ActorFactoryArgs<(String, String)> for BankAccount { - /// fn create_args((name, number): (String, String)) -> Self { - /// BankAccount { - /// name, - /// number - /// } - /// } - /// } - /// - /// # impl Actor for BankAccount { - /// # type Msg = String; - /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} - /// # } - /// - /// #[tokio::main] - /// async fn main() { - /// let sys = ActorSystem::new().unwrap(); - /// - /// let props = Props::new_from_args(BankAccount::create_args, - /// ("James Holden".into(), "12345678".into())); - /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("bank_account", props).unwrap(); - /// } - /// ``` + ``` + # use riker::actors::*; + + struct User { + name: String, + } + + impl ActorFactoryArgs for User { + fn create_args(name: String) -> Self { + User { + name + } + } + } + + # impl Actor for User { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new_args::("Naomi Nagata".into()); + + let actor = sys.actor_of_props("user", props).unwrap(); + } + ``` + An actor requiring multiple parameters. + ``` + # use riker::actors::*; + + struct BankAccount { + name: String, + number: String, + } + + impl ActorFactoryArgs<(String, String)> for BankAccount { + fn create_args((name, number): (String, String)) -> Self { + BankAccount { + name, + number + } + } + } + + # impl Actor for BankAccount { + # type Msg = String; + # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} + # } + + fn main() { + let sys = ActorSystem::new().unwrap(); + + let props = Props::new_from_args(BankAccount::create_args, + ("James Holden".into(), "12345678".into())); + + // start the actor and get an `ActorRef` + let actor = sys.actor_of_props("bank_account", props).unwrap(); + } + ``` + "## + )] #[inline] pub fn new_args(args: Args) -> Arc>> where