diff --git a/benches/fibonacci.rs b/benches/fibonacci.rs index 4c92ead..0c3849f 100644 --- a/benches/fibonacci.rs +++ b/benches/fibonacci.rs @@ -1,6 +1,7 @@ use criterion::BenchmarkId; use criterion::Criterion; use criterion::{criterion_group, criterion_main}; +use kameo::error::Infallible; use kameo::mailbox::bounded::BoundedMailbox; use kameo::request::MessageSend; use kameo::{ @@ -12,6 +13,7 @@ struct FibActor {} impl Actor for FibActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } struct Fib(u64); diff --git a/benches/overhead.rs b/benches/overhead.rs index 1f08116..81e4afa 100644 --- a/benches/overhead.rs +++ b/benches/overhead.rs @@ -1,5 +1,6 @@ use criterion::Criterion; use criterion::{criterion_group, criterion_main}; +use kameo::error::Infallible; use kameo::mailbox::unbounded::UnboundedMailbox; use kameo::request::MessageSend; use kameo::{ @@ -20,6 +21,7 @@ fn actor(c: &mut Criterion) { impl Actor for BenchActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } impl Message for BenchActor { diff --git a/examples/ask.rs b/examples/ask.rs index 5426d89..bad3426 100644 --- a/examples/ask.rs +++ b/examples/ask.rs @@ -1,6 +1,7 @@ use std::time::Duration; use kameo::{ + error::Infallible, mailbox::unbounded::UnboundedMailbox, message::{Context, Message}, request::MessageSendSync, @@ -16,6 +17,7 @@ pub struct MyActor { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; fn name() -> &'static str { "MyActor" diff --git a/examples/basic.rs b/examples/basic.rs index 3cee2b3..b34c625 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,4 +1,5 @@ use kameo::{ + error::Infallible, mailbox::unbounded::UnboundedMailbox, message::{Context, Message}, request::MessageSendSync, @@ -14,6 +15,7 @@ pub struct MyActor { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; fn name() -> &'static str { "MyActor" diff --git a/examples/stream.rs b/examples/stream.rs index dd35cdf..6c8493a 100644 --- a/examples/stream.rs +++ b/examples/stream.rs @@ -3,7 +3,7 @@ use std::{future::pending, time}; use futures::stream; use kameo::{ actor::ActorRef, - error::BoxError, + error::Infallible, mailbox::unbounded::UnboundedMailbox, message::{Context, Message, StreamMessage}, Actor, @@ -19,8 +19,9 @@ pub struct MyActor { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; - async fn on_start(&mut self, actor_ref: ActorRef) -> Result<(), BoxError> { + async fn on_start(&mut self, actor_ref: ActorRef) -> Result<(), Self::Error> { let stream = Box::pin( stream::repeat(1) .take(5) diff --git a/macros/src/derive_actor.rs b/macros/src/derive_actor.rs index f94bc72..192ffc6 100644 --- a/macros/src/derive_actor.rs +++ b/macros/src/derive_actor.rs @@ -50,6 +50,7 @@ impl ToTokens for DeriveActor { #[automatically_derived] impl #impl_generics ::kameo::actor::Actor for #ident #ty_generics #where_clause { type Mailbox = #mailbox_expanded; + type Error = ::kameo::error::Infallible; fn name() -> &'static str { #name diff --git a/src/actor.rs b/src/actor.rs index 64379ba..fd0dc1e 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -28,12 +28,12 @@ pub mod pool; pub mod pubsub; mod spawn; -use std::any; +use std::{any, error::Error}; use futures::Future; use crate::{ - error::{ActorStopReason, BoxError, PanicError}, + error::{ActorStopReason, PanicError}, mailbox::Mailbox, }; @@ -117,6 +117,9 @@ pub trait Actor: Sized + Send + 'static { /// - **Unbounded Mailbox**: Allows an infinite number of messages, but can consume large amounts of memory. type Mailbox: Mailbox; + /// The error type which can occur in the actors lifecycle hooks. + type Error: Error + Send; + /// The name of the actor, which can be useful for logging or debugging. /// /// # Default Implementation @@ -145,7 +148,7 @@ pub trait Actor: Sized + Send + 'static { fn on_start( &mut self, actor_ref: ActorRef, - ) -> impl Future> + Send { + ) -> impl Future> + Send { async { Ok(()) } } @@ -165,7 +168,7 @@ pub trait Actor: Sized + Send + 'static { &mut self, actor_ref: WeakActorRef, err: PanicError, - ) -> impl Future, BoxError>> + Send { + ) -> impl Future, Self::Error>> + Send { async move { Ok(Some(ActorStopReason::Panicked(err))) } } @@ -182,7 +185,7 @@ pub trait Actor: Sized + Send + 'static { actor_ref: WeakActorRef, id: ActorID, reason: ActorStopReason, - ) -> impl Future, BoxError>> + Send { + ) -> impl Future, Self::Error>> + Send { async move { match &reason { ActorStopReason::Normal => Ok(None), @@ -207,7 +210,7 @@ pub trait Actor: Sized + Send + 'static { &mut self, actor_ref: WeakActorRef, reason: ActorStopReason, - ) -> impl Future> + Send { + ) -> impl Future> + Send { async { Ok(()) } } } diff --git a/src/actor/pool.rs b/src/actor/pool.rs index 62d1430..ef20e54 100644 --- a/src/actor/pool.rs +++ b/src/actor/pool.rs @@ -50,7 +50,7 @@ use itertools::repeat_n; use crate::{ actor::{Actor, ActorRef}, - error::{ActorStopReason, BoxError, SendError}, + error::{ActorStopReason, Infallible, SendError}, mailbox::bounded::BoundedMailbox, message::{BoxDebug, Context, Message}, reply::Reply, @@ -165,12 +165,13 @@ where A: Actor, { type Mailbox = BoundedMailbox; + type Error = Infallible; fn name() -> &'static str { "ActorPool" } - async fn on_start(&mut self, actor_ref: ActorRef) -> Result<(), BoxError> { + async fn on_start(&mut self, actor_ref: ActorRef) -> Result<(), Self::Error> { for worker in &self.workers { worker.link(&actor_ref).await; } @@ -183,7 +184,7 @@ where actor_ref: WeakActorRef, id: ActorID, _reason: ActorStopReason, - ) -> Result, BoxError> { + ) -> Result, Self::Error> { let Some(actor_ref) = actor_ref.upgrade() else { return Ok(None); }; diff --git a/src/actor/pubsub.rs b/src/actor/pubsub.rs index c0e4485..ca141db 100644 --- a/src/actor/pubsub.rs +++ b/src/actor/pubsub.rs @@ -49,7 +49,7 @@ use std::collections::HashMap; use futures::future::{join_all, BoxFuture}; use crate::{ - error::SendError, + error::{Infallible, SendError}, mailbox::bounded::BoundedMailbox, message::{Context, Message}, request::{LocalTellRequest, MessageSend, TellRequest, WithoutRequestTimeout}, @@ -166,6 +166,7 @@ impl PubSub { impl Actor for PubSub { type Mailbox = BoundedMailbox; + type Error = Infallible; } impl Default for PubSub { diff --git a/src/request/ask.rs b/src/request/ask.rs index 474d985..55692d0 100644 --- a/src/request/ask.rs +++ b/src/request/ask.rs @@ -997,7 +997,7 @@ mod tests { use std::time::Duration; use crate::{ - error::SendError, + error::{Infallible, SendError}, mailbox::{ bounded::{BoundedMailbox, BoundedMailboxReceiver}, unbounded::UnboundedMailbox, @@ -1013,6 +1013,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } struct Msg; @@ -1060,6 +1061,7 @@ mod tests { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } struct Msg; @@ -1107,6 +1109,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] @@ -1158,6 +1161,7 @@ mod tests { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] @@ -1209,6 +1213,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; fn new_mailbox() -> (BoundedMailbox, BoundedMailboxReceiver) { BoundedMailbox::new(1) @@ -1256,6 +1261,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; fn new_mailbox() -> (BoundedMailbox, BoundedMailboxReceiver) { BoundedMailbox::new(1) @@ -1317,6 +1323,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] @@ -1363,6 +1370,7 @@ mod tests { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] diff --git a/src/request/tell.rs b/src/request/tell.rs index 86ed576..1b3c761 100644 --- a/src/request/tell.rs +++ b/src/request/tell.rs @@ -518,7 +518,7 @@ mod tests { use std::time::Duration; use crate::{ - error::SendError, + error::{Infallible, SendError}, mailbox::{ bounded::{BoundedMailbox, BoundedMailboxReceiver}, unbounded::UnboundedMailbox, @@ -537,6 +537,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } struct Msg; @@ -574,6 +575,7 @@ mod tests { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } struct Msg; @@ -608,6 +610,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] @@ -662,6 +665,7 @@ mod tests { impl Actor for MyActor { type Mailbox = UnboundedMailbox; + type Error = Infallible; } #[derive(Clone, Copy, PartialEq, Eq)] @@ -720,6 +724,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; fn new_mailbox() -> (BoundedMailbox, BoundedMailboxReceiver) { BoundedMailbox::new(1) @@ -774,6 +779,7 @@ mod tests { impl Actor for MyActor { type Mailbox = BoundedMailbox; + type Error = Infallible; fn new_mailbox() -> (BoundedMailbox, BoundedMailboxReceiver) { BoundedMailbox::new(1)