Skip to main content

behavior/
lib.rs

1//! Pure, typed actor-behavior primitives. A [`Behavior`] folds its associated
2//! event protocol into exactly [`Actions`]: sends, fresh creations, and its
3//! next behavior or termination. Higher capabilities are composed from these
4//! explicit transition parts.
5
6// The `workers!` macro emits `::behavior::…` paths; this alias lets
7// those expansions resolve inside this crate too (macro hygiene).
8extern crate self as behavior;
9
10mod actor;
11mod calculus;
12mod effects;
13mod next;
14mod pool;
15mod stash;
16mod supervision;
17mod timing;
18mod watch;
19
20// `Machine` is a thin state-machine helper built from the core stashing primitive.
21mod compose;
22mod machine;
23mod protocol;
24mod shutdown;
25
26pub use actor::{
27    Address, BirthMode, Births, Create, CreationKind, Delivery, MailAddr, NoBirths, Recipient,
28};
29pub use calculus::{
30    ActionReducer, ActiveTurn, Behavior, BehaviorActed, BehaviorBase, Effects, EventInput,
31    FoldFailure, Folded, InitializationTurn, RouteInput, User, UserEvent, fold_events,
32};
33pub use compose::{Active, Compose, Initialized};
34pub use effects::{Acted, Actions, Become, Own, SendAlgebra, SendInput, ServiceSends};
35pub use machine::{Machine, Move};
36pub use next::{Never, Step};
37pub use pool::{
38    AffinitySelector, AssignmentId, InterruptionPolicy, JobId, KeyedPoolEvent, KeyedPoolMessage,
39    KeyedWorkerPool, PoolActions, PoolAssignment, PoolBehaviorSends, PoolConfigError, PoolError,
40    PoolEvent, PoolInterruption, PoolMessage, PoolRejection, PoolResponse, PoolSends, WorkerPhase,
41    WorkerPool, WorkerRetirement,
42};
43pub use protocol::{
44    ChildStopped, CreationRejection, CreationResolved, ObserveChild, ObserveCreation, ObservePeer,
45    PeerStopped, ReplacementResolution, ReportWorkerCreationResolved, ReportWorkerStopped,
46    ScheduleAfter, ScheduleAt, ShutdownRequested, TimerElapsed, TimerGeneration, TimerId,
47    UnwatchPeer, WorkerCreationResolved, WorkerStopped,
48};
49pub use shutdown::{FinalizeOnShutdown, ShutdownProtocol, ShutdownReaction, StopOnShutdown};
50pub use stash::{Stash, StashRoute, StashStatus};
51pub use supervision::{
52    FleetError, IncarnationPhase, Proxy, ProxyCommand, ProxyError, ProxyEvent, ProxySends,
53    RestartPolicy, Strategy, SupervisionEvent, SupervisionFailure, SupervisionFailureReaction,
54    Supervisor, SupervisorError, SupervisorSends, restart_all, restart_one, restart_rest,
55    retire_on_supervision_failure, stop_on_supervision_failure,
56};
57pub use timing::{
58    Deadline, DeadlineEvent, DeadlineReaction, DeadlineSends, ReceiveTimeout, ReceiveTimeoutEvent,
59    ReceiveTimeoutReaction, ReceiveTimeoutSends, TimedEvent,
60};
61pub use watch::{LinkReaction, Watch, WatchEvent, WatchSends, stop_on_abnormal_death};
62
63mod exit;
64
65pub use exit::{Crash, Exit, RestartDenial, SupervisionFailureReason};
66
67/// Generate `Behavior` wiring for an inherent impl with an exact `receive`
68/// method and an optional exact `init` method. When omitted, initialization is
69/// the explicit empty transition: no sends, no creations, and `Continue`.
70/// Invalid receivers are rejected at compile time.
71///
72/// ```compile_fail
73/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
74///
75/// struct Invalid;
76/// #[behavior::behavior(
77///     addr = MailAddr,
78///     message = u8,
79///     sends = Vec<Never>,
80///     births = NoBirths,
81///     error = Never,
82/// )]
83/// impl Invalid {
84///     fn init(&self) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
85///         Ok(Actions::cont())
86///     }
87///     fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
88///         Ok(Actions::cont())
89///     }
90/// }
91/// ```
92///
93/// Missing receive methods are rejected by the macro itself:
94///
95/// ```compile_fail
96/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
97/// struct Missing;
98/// #[behavior::behavior(
99///     addr = MailAddr,
100///     message = u8,
101///     sends = Vec<Never>,
102///     births = NoBirths,
103///     error = Never,
104/// )]
105/// impl Missing {
106/// }
107/// ```
108///
109/// Async behavior methods cannot introduce an erased or alternate execution
110/// path:
111///
112/// ```compile_fail
113/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
114/// struct Async;
115/// #[behavior::behavior(
116///     addr = MailAddr,
117///     message = u8,
118///     sends = Vec<Never>,
119///     births = NoBirths,
120///     error = Never,
121/// )]
122/// impl Async {
123///     async fn init(&mut self, _: crate::InitializationTurn) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
124///         Ok(Actions::cont())
125///     }
126///     fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
127///         Ok(Actions::cont())
128///     }
129/// }
130/// ```
131pub use behavior_macros::behavior;
132pub use behavior_macros::workers;