Skip to content

Commit

Permalink
Differentiate single and broadcast message on type-system level (#166)
Browse files Browse the repository at this point in the history
Prior to this patch-set, we had constructs like `MessageType` and `SentMessage`. These were used to write code that is generic over a particular message type. In reality however, we don't actually need to differentiate between these cases because the codepaths for each type are statically known in all cases.

Unfortunately, introducing an actual split introduced a bit more code (net ~ 70 lines) but in exchange, we remove a few `unreachable` error clauses and make it overall easier to follow the dataflow through xtra's channel implementation.
  • Loading branch information
thomaseizinger authored Aug 30, 2022
1 parent e6941e2 commit 89ac7bb
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 267 deletions.
4 changes: 2 additions & 2 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use event_listener::EventListener;
use futures_util::FutureExt;

use crate::refcount::{Either, RefCounter, Strong, Weak};
use crate::send_future::{Broadcast, ResolveToHandlerReturn};
use crate::send_future::{ActorNamedBroadcasting, Broadcast, ResolveToHandlerReturn};
use crate::{inbox, ActorNamedSending, Handler, SendFuture};

/// An [`Address`] is a reference to an actor through which messages can be sent.
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<A, Rc: RefCounter> Address<A, Rc> {
///
/// The actor must implement [`Handler<Message>`] for this to work where [`Handler::Return`] is
/// set to `()`.
pub fn broadcast<M>(&self, msg: M) -> SendFuture<ActorNamedSending<A, Rc>, Broadcast>
pub fn broadcast<M>(&self, msg: M) -> SendFuture<ActorNamedBroadcasting<A, Rc>, Broadcast>
where
M: Clone + Send + Sync + 'static,
A: Handler<M, Return = ()>,
Expand Down
6 changes: 3 additions & 3 deletions src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use futures_core::future::BoxFuture;
use futures_util::FutureExt;

use crate::context::Context;
use crate::inbox::{HasPriority, Priority};
use crate::inbox::{HasPriority, MessageToAll, MessageToOne, Priority};
use crate::{Actor, Handler};

/// A message envelope is a struct that encapsulates a message and its return channel sender (if applicable).
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<A, M, R> HasPriority for ReturningEnvelope<A, M, R> {
}
}

impl<A> HasPriority for Box<dyn MessageEnvelope<Actor = A>> {
impl<A> HasPriority for MessageToOne<A> {
fn priority(&self) -> Priority {
self.as_ref().priority()
}
Expand Down Expand Up @@ -236,7 +236,7 @@ pub trait BroadcastEnvelope: HasPriority + Send + Sync {
) -> (BoxFuture<'a, ControlFlow<()>>, Span);
}

impl<A> HasPriority for Arc<dyn BroadcastEnvelope<Actor = A>> {
impl<A> HasPriority for MessageToAll<A> {
fn priority(&self) -> Priority {
self.as_ref().priority()
}
Expand Down
Loading

0 comments on commit 89ac7bb

Please sign in to comment.