Skip to content

Commit

Permalink
feat!: relax request impls to be generic to any mailbox (#71)
Browse files Browse the repository at this point in the history
* feat!: relax request impls to be generic to any mailbox

* feat: add generic implementation of TryMessageSendSync
  • Loading branch information
tqwewe authored Oct 20, 2024
1 parent c093fdf commit 0aea82b
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 488 deletions.
2 changes: 1 addition & 1 deletion src/actor/actor_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl<A: Actor> RemoteActorRef<A> {
>
where
A: remote::RemoteActor + Message<M> + remote::RemoteMessage<M>,
M: serde::Serialize,
M: serde::Serialize + Send + 'static,
<A::Reply as Reply>::Ok: for<'de> serde::Deserialize<'de>,
{
AskRequest::new_remote(self, msg)
Expand Down
1 change: 1 addition & 0 deletions src/actor/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ where
pub enum WorkerReply<A, M>
where
A: Actor + Message<M>,
M: Send + 'static,
{
/// The message was forwarded to a worker.
Forwarded,
Expand Down
4 changes: 4 additions & 0 deletions src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub trait Mailbox<A: Actor>: SignalMailbox + Clone + Send + Sync {
&self,
signal: Signal<A>,
) -> impl Future<Output = Result<(), SendError<Signal<A>, E>>> + Send + '_;
/// Tries to send a signal to the mailbox, failing if the mailbox is full.
fn try_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>>;
/// Sends a signal to the mailbox, blocking the current thread.
fn blocking_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>>;
/// Waits for the mailbox to be closed.
fn closed(&self) -> impl Future<Output = ()> + Send + '_;
/// Checks if the mailbox is closed.
Expand Down
10 changes: 10 additions & 0 deletions src/mailbox/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl<A: Actor> Mailbox<A> for BoundedMailbox<A> {
Ok(self.0.send(signal).await?)
}

#[inline]
fn try_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>> {
Ok(self.0.try_send(signal)?)
}

#[inline]
fn blocking_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>> {
Ok(self.0.blocking_send(signal)?)
}

#[inline]
async fn closed(&self) {
self.0.closed().await
Expand Down
10 changes: 10 additions & 0 deletions src/mailbox/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl<A: Actor> Mailbox<A> for UnboundedMailbox<A> {
Ok(self.0.send(signal)?)
}

#[inline]
fn try_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>> {
Ok(self.0.send(signal)?)
}

#[inline]
fn blocking_send<E: 'static>(&self, signal: Signal<A>) -> Result<(), SendError<Signal<A>, E>> {
Ok(self.0.send(signal)?)
}

#[inline]
async fn closed(&self) {
self.0.closed().await
Expand Down
2 changes: 1 addition & 1 deletion src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) type BoxReply = Box<dyn any::Any + Send>;
/// Messages are processed sequentially one at a time, with exclusive mutable access to the actors state.
///
/// The reply type must implement [Reply].
pub trait Message<T>: Actor {
pub trait Message<T: Send + 'static>: Actor {
/// The reply sent back to the message caller.
type Reply: Reply;

Expand Down
Loading

0 comments on commit 0aea82b

Please sign in to comment.