Skip to main content

behavior/
protocol.rs

1//! Neutral typed vocabulary for interpreter-originated event and service lanes.
2//!
3//! Concrete behavior transformations define the closed sum types that add
4//! these lanes. Keeping their values and construction capabilities here avoids
5//! dependencies between otherwise independent transformations.
6
7use std::time::Duration;
8
9use tokio::time::Instant;
10
11use crate::behavior::Address;
12use crate::{Crash, Exit};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct TimerId(pub u64);
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub struct TimerGeneration(pub u64);
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct ScheduleAt {
22    pub id: TimerId,
23    pub generation: TimerGeneration,
24    pub at: Instant,
25}
26
27/// Request scheduling relative to the interpreter's clock.
28///
29/// Constructing this value does not observe a clock. The interpreter resolves
30/// `after` only when it interprets the successful transition that emitted the
31/// request.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub struct ScheduleAfter {
34    pub id: TimerId,
35    pub generation: TimerGeneration,
36    pub after: Duration,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub struct TimerElapsed {
41    pub id: TimerId,
42    pub generation: TimerGeneration,
43}
44
45pub trait TimeEvent: Sized {
46    fn time_reached(event: TimerElapsed) -> Option<Self>;
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub struct ObservePeer<A> {
51    pub peer: A,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct PeerStopped<A: Address> {
56    pub peer: A,
57    pub outcome: Result<Exit<A>, Crash>,
58}
59
60pub trait PeerEvent<A: Address>: Sized {
61    fn peer_stopped(event: PeerStopped<A>) -> Option<Self>;
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct ChildStopped<A: Address> {
66    pub nonce: A::Nonce,
67    pub outcome: Result<Exit<A>, Crash>,
68    pub at: Instant,
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct ObserveChild<A: Address> {
73    pub nonce: A::Nonce,
74}
75
76/// A proxy's request for its interpreter to report a worker termination to
77/// the proxy's parent. The interpreter supplies the emitting proxy's child
78/// nonce when constructing [`WorkerStopped`].
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct ReportWorkerStopped<A: Address> {
81    pub outcome: Result<Exit<A>, Crash>,
82    pub at: Instant,
83}
84
85/// A worker termination reported by a still-live supervised proxy.
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct WorkerStopped<A: Address> {
88    pub proxy: A::Nonce,
89    pub outcome: Result<Exit<A>, Crash>,
90    pub at: Instant,
91}
92
93pub trait ChildEvent<A: Address>: Sized {
94    fn child_stopped(event: ChildStopped<A>) -> Option<Self>;
95}
96
97pub trait WorkerEvent<A: Address>: Sized {
98    fn worker_stopped(event: WorkerStopped<A>) -> Option<Self>;
99}
100
101/// A request to finish through one serialized behavior transition.
102#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
103pub struct ShutdownRequested;
104
105pub trait ShutdownEvent: Sized {
106    fn shutdown_requested(event: ShutdownRequested) -> Option<Self>;
107}