-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsender.rs
More file actions
39 lines (33 loc) · 1.43 KB
/
sender.rs
File metadata and controls
39 lines (33 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt;
use std::fmt::Debug;
use futures::future::BoxFuture;
#[cfg(test)]
use mockall::{automock, predicate::str};
/// A trait for sending events.
#[cfg_attr(test, automock(type Event=();))]
pub trait Sender: Sync + Send {
type Event: Send + Clone;
/// Sends an event to all active receivers.
///
/// Returns a future that resolves to an `Option<Result<usize, SendError<Self::Event>>>`:
///
/// - `Some(Ok(n))` — the event was successfully sent to `n` receivers.
/// - `Some(Err(e))` — an error occurred while sending the event.
/// - `None` — the sender is inactive or disconnected, and the event was not sent.
///
/// The `Option` allows implementations to express cases where sending is not possible
/// (e.g., when the sender is disabled or there are no active receivers).
///
/// The `usize` typically represents the number of receivers the message was delivered to,
/// but its semantics may vary depending on the concrete implementation.
fn send(&self, event: Self::Event) -> BoxFuture<'_, Option<Result<usize, SendError<Self::Event>>>>;
}
/// Error returned by the [`send`] function on a [`Sender`].
#[derive(Debug)]
pub struct SendError<Event>(pub Event);
impl<Event> fmt::Display for SendError<Event> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "channel closed")
}
}
impl<Event: fmt::Debug> std::error::Error for SendError<Event> {}