Skip to main content

behavior/calculus/
user_event.rs

1//! The user-message lane and its composition contracts.
2
3use crate::actor::Address;
4use crate::protocol::{
5    ChildEvent, ChildStopped, CreationEvent, CreationResolved, PeerEvent, PeerStopped,
6    ShutdownEvent, ShutdownRequested, TimeEvent, TimerElapsed, WorkerCreationEvent,
7    WorkerCreationResolved, WorkerEvent, WorkerStopped,
8};
9
10/// The user-message event at the Agha floor.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct User<A, M> {
13    pub from: A,
14    pub message: M,
15}
16
17/// A statically proven injection of one semantic input into a concrete event sum.
18///
19/// Implementations must select exactly one constructor and preserve `input`
20/// unchanged. Absence of an implementation means that the protocol does not
21/// accept that input.
22pub trait EventInput<Input>: Sized {
23    fn inject(input: Input) -> Self;
24}
25
26impl<A, M> EventInput<User<A, M>> for User<A, M> {
27    fn inject(input: User<A, M>) -> Self {
28        input
29    }
30}
31
32impl<A, M> User<A, M> {
33    #[must_use]
34    pub const fn new(from: A, message: M) -> Self {
35        Self { from, message }
36    }
37}
38
39impl<A, M> From<(A, M)> for User<A, M> {
40    fn from((from, message): (A, M)) -> Self {
41        Self::new(from, message)
42    }
43}
44
45/// Construction/extraction of the user lane through a composed event type.
46pub trait UserEvent: Sized {
47    type Addr: Address;
48    type Message;
49
50    fn user(from: Self::Addr, message: Self::Message) -> Self;
51
52    /// # Errors
53    /// Returns the unchanged event when it belongs to another composed lane.
54    fn into_user(self) -> Result<User<Self::Addr, Self::Message>, Self>;
55}
56
57impl<A: Address, M> UserEvent for User<A, M> {
58    type Addr = A;
59    type Message = M;
60
61    fn user(from: A, message: M) -> Self {
62        Self::new(from, message)
63    }
64    fn into_user(self) -> Result<Self, Self> {
65        Ok(self)
66    }
67}
68
69impl<A: Address, M> TimeEvent for User<A, M> {
70    fn time_reached(_: TimerElapsed) -> Option<Self> {
71        None
72    }
73}
74impl<A: Address, M> PeerEvent for User<A, M> {
75    fn peer_stopped(_: PeerStopped<A>) -> Option<Self> {
76        None
77    }
78}
79impl<A: Address, M> ChildEvent for User<A, M> {
80    fn child_stopped(_: ChildStopped<A>) -> Option<Self> {
81        None
82    }
83}
84impl<A: Address, M> WorkerEvent for User<A, M> {
85    fn worker_stopped(_: WorkerStopped<A>) -> Option<Self> {
86        None
87    }
88}
89impl<A: Address, M> CreationEvent for User<A, M> {
90    fn creation_resolved(_: CreationResolved<A::Nonce>) -> Option<Self> {
91        None
92    }
93}
94impl<A: Address, M> WorkerCreationEvent for User<A, M> {
95    fn worker_creation_resolved(_: WorkerCreationResolved<A::Nonce>) -> Option<Self> {
96        None
97    }
98}
99impl<A: Address, M> ShutdownEvent for User<A, M> {
100    fn shutdown_requested(_: ShutdownRequested) -> Option<Self> {
101        None
102    }
103}