Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add error type to Actor trait #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -12,6 +13,7 @@ struct FibActor {}

impl Actor for FibActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

struct Fib(u64);
Expand Down
2 changes: 2 additions & 0 deletions benches/overhead.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -20,6 +21,7 @@ fn actor(c: &mut Criterion) {

impl Actor for BenchActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

impl Message<u32> for BenchActor {
Expand Down
2 changes: 2 additions & 0 deletions examples/ask.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use kameo::{
error::Infallible,
mailbox::unbounded::UnboundedMailbox,
message::{Context, Message},
request::MessageSendSync,
Expand All @@ -16,6 +17,7 @@ pub struct MyActor {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;

fn name() -> &'static str {
"MyActor"
Expand Down
2 changes: 2 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use kameo::{
error::Infallible,
mailbox::unbounded::UnboundedMailbox,
message::{Context, Message},
request::MessageSendSync,
Expand All @@ -14,6 +15,7 @@ pub struct MyActor {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;

fn name() -> &'static str {
"MyActor"
Expand Down
5 changes: 3 additions & 2 deletions examples/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,8 +19,9 @@ pub struct MyActor {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;

async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Self::Error> {
let stream = Box::pin(
stream::repeat(1)
.take(5)
Expand Down
1 change: 1 addition & 0 deletions macros/src/derive_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -66,15 +66,16 @@ pub use spawn::*;
///
/// ```
/// use kameo::actor::{Actor, ActorRef, WeakActorRef};
/// use kameo::error::{ActorStopReason, BoxError};
/// use kameo::error::{ActorStopReason, Infallible};
/// use kameo::mailbox::unbounded::UnboundedMailbox;
///
/// struct MyActor;
///
/// impl Actor for MyActor {
/// type Mailbox = UnboundedMailbox<Self>;
/// type Error = Infallible;
///
/// async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
/// async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Infallible> {
/// println!("actor started");
/// Ok(())
/// }
Expand All @@ -83,7 +84,7 @@ pub use spawn::*;
/// &mut self,
/// actor_ref: WeakActorRef<Self>,
/// reason: ActorStopReason,
/// ) -> Result<(), BoxError> {
/// ) -> Result<(), Infallible> {
/// println!("actor stopped");
/// Ok(())
/// }
Expand Down Expand Up @@ -117,6 +118,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<Self>;

/// 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
Expand Down Expand Up @@ -145,7 +149,7 @@ pub trait Actor: Sized + Send + 'static {
fn on_start(
&mut self,
actor_ref: ActorRef<Self>,
) -> impl Future<Output = Result<(), BoxError>> + Send {
) -> impl Future<Output = Result<(), Self::Error>> + Send {
async { Ok(()) }
}

Expand All @@ -165,7 +169,7 @@ pub trait Actor: Sized + Send + 'static {
&mut self,
actor_ref: WeakActorRef<Self>,
err: PanicError,
) -> impl Future<Output = Result<Option<ActorStopReason>, BoxError>> + Send {
) -> impl Future<Output = Result<Option<ActorStopReason>, Self::Error>> + Send {
async move { Ok(Some(ActorStopReason::Panicked(err))) }
}

Expand All @@ -182,7 +186,7 @@ pub trait Actor: Sized + Send + 'static {
actor_ref: WeakActorRef<Self>,
id: ActorID,
reason: ActorStopReason,
) -> impl Future<Output = Result<Option<ActorStopReason>, BoxError>> + Send {
) -> impl Future<Output = Result<Option<ActorStopReason>, Self::Error>> + Send {
async move {
match &reason {
ActorStopReason::Normal => Ok(None),
Expand All @@ -207,7 +211,7 @@ pub trait Actor: Sized + Send + 'static {
&mut self,
actor_ref: WeakActorRef<Self>,
reason: ActorStopReason,
) -> impl Future<Output = Result<(), BoxError>> + Send {
) -> impl Future<Output = Result<(), Self::Error>> + Send {
async { Ok(()) }
}
}
5 changes: 3 additions & 2 deletions src/actor/actor_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,17 @@ where
/// use std::time::Duration;
///
/// use kameo::actor::{Actor, ActorRef};
/// use kameo::error::BoxError;
/// use kameo::error::Infallible;
/// use kameo::mailbox::unbounded::UnboundedMailbox;
/// use tokio::time::sleep;
///
/// struct MyActor;
///
/// impl Actor for MyActor {
/// type Mailbox = UnboundedMailbox<Self>;
/// type Error = Infallible;
///
/// async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
/// async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Infallible> {
/// sleep(Duration::from_secs(2)).await; // Some io operation
/// Ok(())
/// }
Expand Down
7 changes: 4 additions & 3 deletions src/actor/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -165,12 +165,13 @@ where
A: Actor,
{
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;

fn name() -> &'static str {
"ActorPool"
}

async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Self::Error> {
for worker in &self.workers {
worker.link(&actor_ref).await;
}
Expand All @@ -183,7 +184,7 @@ where
actor_ref: WeakActorRef<Self>,
id: ActorID,
_reason: ActorStopReason,
) -> Result<Option<ActorStopReason>, BoxError> {
) -> Result<Option<ActorStopReason>, Self::Error> {
let Some(actor_ref) = actor_ref.upgrade() else {
return Ok(None);
};
Expand Down
3 changes: 2 additions & 1 deletion src/actor/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -166,6 +166,7 @@ impl<M> PubSub<M> {

impl<M: 'static> Actor for PubSub<M> {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

impl<M> Default for PubSub<M> {
Expand Down
10 changes: 9 additions & 1 deletion src/request/ask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ mod tests {
use std::time::Duration;

use crate::{
error::SendError,
error::{Infallible, SendError},
mailbox::{
bounded::{BoundedMailbox, BoundedMailboxReceiver},
unbounded::UnboundedMailbox,
Expand All @@ -1013,6 +1013,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

struct Msg;
Expand Down Expand Up @@ -1060,6 +1061,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

struct Msg;
Expand Down Expand Up @@ -1107,6 +1109,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -1158,6 +1161,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -1209,6 +1213,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;

fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
BoundedMailbox::new(1)
Expand Down Expand Up @@ -1256,6 +1261,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;

fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
BoundedMailbox::new(1)
Expand Down Expand Up @@ -1317,6 +1323,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -1363,6 +1370,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down
8 changes: 7 additions & 1 deletion src/request/tell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ mod tests {
use std::time::Duration;

use crate::{
error::SendError,
error::{Infallible, SendError},
mailbox::{
bounded::{BoundedMailbox, BoundedMailboxReceiver},
unbounded::UnboundedMailbox,
Expand All @@ -537,6 +537,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

struct Msg;
Expand Down Expand Up @@ -574,6 +575,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

struct Msg;
Expand Down Expand Up @@ -608,6 +610,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -662,6 +665,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = UnboundedMailbox<Self>;
type Error = Infallible;
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -720,6 +724,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;

fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
BoundedMailbox::new(1)
Expand Down Expand Up @@ -774,6 +779,7 @@ mod tests {

impl Actor for MyActor {
type Mailbox = BoundedMailbox<Self>;
type Error = Infallible;

fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
BoundedMailbox::new(1)
Expand Down
Loading