Skip to content

Commit db1fc42

Browse files
committed
feat!: add error type to Actor trait
1 parent d19a4b1 commit db1fc42

File tree

11 files changed

+43
-14
lines changed

11 files changed

+43
-14
lines changed

benches/fibonacci.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use criterion::BenchmarkId;
22
use criterion::Criterion;
33
use criterion::{criterion_group, criterion_main};
4+
use kameo::error::Infallible;
45
use kameo::mailbox::bounded::BoundedMailbox;
56
use kameo::request::MessageSend;
67
use kameo::{
@@ -12,6 +13,7 @@ struct FibActor {}
1213

1314
impl Actor for FibActor {
1415
type Mailbox = BoundedMailbox<Self>;
16+
type Error = Infallible;
1517
}
1618

1719
struct Fib(u64);

benches/overhead.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use criterion::Criterion;
22
use criterion::{criterion_group, criterion_main};
3+
use kameo::error::Infallible;
34
use kameo::mailbox::unbounded::UnboundedMailbox;
45
use kameo::request::MessageSend;
56
use kameo::{
@@ -20,6 +21,7 @@ fn actor(c: &mut Criterion) {
2021

2122
impl Actor for BenchActor {
2223
type Mailbox = UnboundedMailbox<Self>;
24+
type Error = Infallible;
2325
}
2426

2527
impl Message<u32> for BenchActor {

examples/ask.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::time::Duration;
22

33
use kameo::{
4+
error::Infallible,
45
mailbox::unbounded::UnboundedMailbox,
56
message::{Context, Message},
67
request::MessageSendSync,
@@ -16,6 +17,7 @@ pub struct MyActor {
1617

1718
impl Actor for MyActor {
1819
type Mailbox = UnboundedMailbox<Self>;
20+
type Error = Infallible;
1921

2022
fn name() -> &'static str {
2123
"MyActor"

examples/basic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use kameo::{
2+
error::Infallible,
23
mailbox::unbounded::UnboundedMailbox,
34
message::{Context, Message},
45
request::MessageSendSync,
@@ -14,6 +15,7 @@ pub struct MyActor {
1415

1516
impl Actor for MyActor {
1617
type Mailbox = UnboundedMailbox<Self>;
18+
type Error = Infallible;
1719

1820
fn name() -> &'static str {
1921
"MyActor"

examples/stream.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{future::pending, time};
33
use futures::stream;
44
use kameo::{
55
actor::ActorRef,
6-
error::BoxError,
6+
error::Infallible,
77
mailbox::unbounded::UnboundedMailbox,
88
message::{Context, Message, StreamMessage},
99
Actor,
@@ -19,8 +19,9 @@ pub struct MyActor {
1919

2020
impl Actor for MyActor {
2121
type Mailbox = UnboundedMailbox<Self>;
22+
type Error = Infallible;
2223

23-
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
24+
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Self::Error> {
2425
let stream = Box::pin(
2526
stream::repeat(1)
2627
.take(5)

macros/src/derive_actor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl ToTokens for DeriveActor {
5050
#[automatically_derived]
5151
impl #impl_generics ::kameo::actor::Actor for #ident #ty_generics #where_clause {
5252
type Mailbox = #mailbox_expanded;
53+
type Error = ::kameo::error::Infallible;
5354

5455
fn name() -> &'static str {
5556
#name

src/actor.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub mod pool;
2828
pub mod pubsub;
2929
mod spawn;
3030

31-
use std::any;
31+
use std::{any, error::Error};
3232

3333
use futures::Future;
3434

3535
use crate::{
36-
error::{ActorStopReason, BoxError, PanicError},
36+
error::{ActorStopReason, PanicError},
3737
mailbox::Mailbox,
3838
};
3939

@@ -117,6 +117,9 @@ pub trait Actor: Sized + Send + 'static {
117117
/// - **Unbounded Mailbox**: Allows an infinite number of messages, but can consume large amounts of memory.
118118
type Mailbox: Mailbox<Self>;
119119

120+
/// The error type which can occur in the actors lifecycle hooks.
121+
type Error: Error + Send;
122+
120123
/// The name of the actor, which can be useful for logging or debugging.
121124
///
122125
/// # Default Implementation
@@ -145,7 +148,7 @@ pub trait Actor: Sized + Send + 'static {
145148
fn on_start(
146149
&mut self,
147150
actor_ref: ActorRef<Self>,
148-
) -> impl Future<Output = Result<(), BoxError>> + Send {
151+
) -> impl Future<Output = Result<(), Self::Error>> + Send {
149152
async { Ok(()) }
150153
}
151154

@@ -165,7 +168,7 @@ pub trait Actor: Sized + Send + 'static {
165168
&mut self,
166169
actor_ref: WeakActorRef<Self>,
167170
err: PanicError,
168-
) -> impl Future<Output = Result<Option<ActorStopReason>, BoxError>> + Send {
171+
) -> impl Future<Output = Result<Option<ActorStopReason>, Self::Error>> + Send {
169172
async move { Ok(Some(ActorStopReason::Panicked(err))) }
170173
}
171174

@@ -182,7 +185,7 @@ pub trait Actor: Sized + Send + 'static {
182185
actor_ref: WeakActorRef<Self>,
183186
id: ActorID,
184187
reason: ActorStopReason,
185-
) -> impl Future<Output = Result<Option<ActorStopReason>, BoxError>> + Send {
188+
) -> impl Future<Output = Result<Option<ActorStopReason>, Self::Error>> + Send {
186189
async move {
187190
match &reason {
188191
ActorStopReason::Normal => Ok(None),
@@ -207,7 +210,7 @@ pub trait Actor: Sized + Send + 'static {
207210
&mut self,
208211
actor_ref: WeakActorRef<Self>,
209212
reason: ActorStopReason,
210-
) -> impl Future<Output = Result<(), BoxError>> + Send {
213+
) -> impl Future<Output = Result<(), Self::Error>> + Send {
211214
async { Ok(()) }
212215
}
213216
}

src/actor/pool.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use itertools::repeat_n;
5050

5151
use crate::{
5252
actor::{Actor, ActorRef},
53-
error::{ActorStopReason, BoxError, SendError},
53+
error::{ActorStopReason, Infallible, SendError},
5454
mailbox::bounded::BoundedMailbox,
5555
message::{BoxDebug, Context, Message},
5656
reply::Reply,
@@ -165,12 +165,13 @@ where
165165
A: Actor,
166166
{
167167
type Mailbox = BoundedMailbox<Self>;
168+
type Error = Infallible;
168169

169170
fn name() -> &'static str {
170171
"ActorPool"
171172
}
172173

173-
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), BoxError> {
174+
async fn on_start(&mut self, actor_ref: ActorRef<Self>) -> Result<(), Self::Error> {
174175
for worker in &self.workers {
175176
worker.link(&actor_ref).await;
176177
}
@@ -183,7 +184,7 @@ where
183184
actor_ref: WeakActorRef<Self>,
184185
id: ActorID,
185186
_reason: ActorStopReason,
186-
) -> Result<Option<ActorStopReason>, BoxError> {
187+
) -> Result<Option<ActorStopReason>, Self::Error> {
187188
let Some(actor_ref) = actor_ref.upgrade() else {
188189
return Ok(None);
189190
};

src/actor/pubsub.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::collections::HashMap;
4949
use futures::future::{join_all, BoxFuture};
5050

5151
use crate::{
52-
error::SendError,
52+
error::{Infallible, SendError},
5353
mailbox::bounded::BoundedMailbox,
5454
message::{Context, Message},
5555
request::{LocalTellRequest, MessageSend, TellRequest, WithoutRequestTimeout},
@@ -166,6 +166,7 @@ impl<M> PubSub<M> {
166166

167167
impl<M: 'static> Actor for PubSub<M> {
168168
type Mailbox = BoundedMailbox<Self>;
169+
type Error = Infallible;
169170
}
170171

171172
impl<M> Default for PubSub<M> {

src/request/ask.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ mod tests {
997997
use std::time::Duration;
998998

999999
use crate::{
1000-
error::SendError,
1000+
error::{Infallible, SendError},
10011001
mailbox::{
10021002
bounded::{BoundedMailbox, BoundedMailboxReceiver},
10031003
unbounded::UnboundedMailbox,
@@ -1013,6 +1013,7 @@ mod tests {
10131013

10141014
impl Actor for MyActor {
10151015
type Mailbox = BoundedMailbox<Self>;
1016+
type Error = Infallible;
10161017
}
10171018

10181019
struct Msg;
@@ -1060,6 +1061,7 @@ mod tests {
10601061

10611062
impl Actor for MyActor {
10621063
type Mailbox = UnboundedMailbox<Self>;
1064+
type Error = Infallible;
10631065
}
10641066

10651067
struct Msg;
@@ -1107,6 +1109,7 @@ mod tests {
11071109

11081110
impl Actor for MyActor {
11091111
type Mailbox = BoundedMailbox<Self>;
1112+
type Error = Infallible;
11101113
}
11111114

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

11591162
impl Actor for MyActor {
11601163
type Mailbox = UnboundedMailbox<Self>;
1164+
type Error = Infallible;
11611165
}
11621166

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

12101214
impl Actor for MyActor {
12111215
type Mailbox = BoundedMailbox<Self>;
1216+
type Error = Infallible;
12121217

12131218
fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
12141219
BoundedMailbox::new(1)
@@ -1256,6 +1261,7 @@ mod tests {
12561261

12571262
impl Actor for MyActor {
12581263
type Mailbox = BoundedMailbox<Self>;
1264+
type Error = Infallible;
12591265

12601266
fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
12611267
BoundedMailbox::new(1)
@@ -1317,6 +1323,7 @@ mod tests {
13171323

13181324
impl Actor for MyActor {
13191325
type Mailbox = BoundedMailbox<Self>;
1326+
type Error = Infallible;
13201327
}
13211328

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

13641371
impl Actor for MyActor {
13651372
type Mailbox = UnboundedMailbox<Self>;
1373+
type Error = Infallible;
13661374
}
13671375

13681376
#[derive(Clone, Copy, PartialEq, Eq)]

src/request/tell.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ mod tests {
518518
use std::time::Duration;
519519

520520
use crate::{
521-
error::SendError,
521+
error::{Infallible, SendError},
522522
mailbox::{
523523
bounded::{BoundedMailbox, BoundedMailboxReceiver},
524524
unbounded::UnboundedMailbox,
@@ -537,6 +537,7 @@ mod tests {
537537

538538
impl Actor for MyActor {
539539
type Mailbox = BoundedMailbox<Self>;
540+
type Error = Infallible;
540541
}
541542

542543
struct Msg;
@@ -574,6 +575,7 @@ mod tests {
574575

575576
impl Actor for MyActor {
576577
type Mailbox = UnboundedMailbox<Self>;
578+
type Error = Infallible;
577579
}
578580

579581
struct Msg;
@@ -608,6 +610,7 @@ mod tests {
608610

609611
impl Actor for MyActor {
610612
type Mailbox = BoundedMailbox<Self>;
613+
type Error = Infallible;
611614
}
612615

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

663666
impl Actor for MyActor {
664667
type Mailbox = UnboundedMailbox<Self>;
668+
type Error = Infallible;
665669
}
666670

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

721725
impl Actor for MyActor {
722726
type Mailbox = BoundedMailbox<Self>;
727+
type Error = Infallible;
723728

724729
fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
725730
BoundedMailbox::new(1)
@@ -774,6 +779,7 @@ mod tests {
774779

775780
impl Actor for MyActor {
776781
type Mailbox = BoundedMailbox<Self>;
782+
type Error = Infallible;
777783

778784
fn new_mailbox() -> (BoundedMailbox<Self>, BoundedMailboxReceiver<Self>) {
779785
BoundedMailbox::new(1)

0 commit comments

Comments
 (0)