From 023a2a17fc407dd34657b873d7edaea529f78483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Wed, 5 Nov 2025 14:48:51 +0100 Subject: [PATCH] Add more ergonomic constructor of ActorHandles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- crates/tytix-core/src/actor.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/crates/tytix-core/src/actor.rs b/crates/tytix-core/src/actor.rs index ddf679b..1b9391f 100644 --- a/crates/tytix-core/src/actor.rs +++ b/crates/tytix-core/src/actor.rs @@ -7,10 +7,23 @@ use crate::IsContainedInBundle; use crate::Message; use crate::MessageBundle; -pub trait Actor: Any { +pub trait Actor: Any +where + Self: ActorHandler, +{ type HandledMessages: MessageBundle; } +pub trait IntoActorHandle { + fn into_actor_handle(self) -> ActorHandle; +} + +impl IntoActorHandle for A { + fn into_actor_handle(self) -> ActorHandle { + ActorHandle::new(self) + } +} + pub trait Handle { fn handle(&mut self, message: M) -> impl Future>; } @@ -26,7 +39,7 @@ macro_rules! impl_actor_handle { ( $($ty:ident),* ) => { impl ActorHandler<($($ty,)*)> for ACTOR where - ACTOR: Actor, + ACTOR: Actor, $( ACTOR: Handle<$ty>, )* $( $ty: Message, )* { @@ -40,7 +53,7 @@ macro_rules! impl_actor_handle { $( let msg = match msg.into_inner::<$ty>() { Ok(msg) => { - return self.handle(msg).await.map(InternalMessage::new); + return >::handle(self, msg).await.map(InternalMessage::new); } Err(msg) => msg, }; @@ -50,6 +63,7 @@ macro_rules! impl_actor_handle { } } + } }; } @@ -112,12 +126,6 @@ mod tests { type Reply = (); } - struct Zap; - - impl Message for Zap { - type Reply = (); - } - struct FActor; impl Actor for FActor { @@ -126,14 +134,14 @@ mod tests { impl Handle for FActor { async fn handle(&mut self, _message: Foo) -> anyhow::Result<::Reply> { - todo!() + Ok(()) } } #[apply(test!)] async fn test_name() { - let mut actor = ActorHandle::new(FActor); + let mut actor = FActor.into_actor_handle(); - actor.handle(Zap).await.unwrap(); + actor.handle(Foo).await.unwrap(); } }