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 `#[behavior]` expansion emits `::behavior::…` paths; this alias lets the
7// expansion resolve inside this crate too.
8extern crate self as behavior;
9
10mod actor;
11mod effects;
12mod next;
13mod reducer;
14mod transition;
15mod user_event;
16
17pub use actor::{
18    Address, BirthMode, Births, Create, CreationKind, Delivery, MailAddr, NoBirths, Recipient,
19};
20pub use effects::{Acted, Actions, Become, Own, SendAlgebra, SendInput, ServiceSends};
21pub use next::{Never, Step, Stopped};
22pub use reducer::{ActionReducer, Effects, FoldFailure, Folded, fold_events};
23pub use transition::{
24    ActiveTurn, Behavior, BehaviorActed, BehaviorBase, InitializationTurn, delegate_transition,
25    initialize,
26};
27pub use user_event::{EventInput, RouteInput, User, UserEvent};
28
29/// Generate `Behavior` wiring for an inherent impl with an exact `receive`
30/// method and an optional exact `init` method. When omitted, initialization is
31/// the explicit empty transition: no sends, no creations, and `Continue`.
32/// Invalid receivers are rejected at compile time.
33///
34/// ```compile_fail
35/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
36///
37/// struct Invalid;
38/// #[behavior::behavior(
39///     addr = MailAddr,
40///     message = u8,
41///     sends = Vec<Never>,
42///     births = NoBirths,
43///     error = Never,
44/// )]
45/// impl Invalid {
46///     fn init(&self) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
47///         Ok(Actions::cont())
48///     }
49///     fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
50///         Ok(Actions::cont())
51///     }
52/// }
53/// ```
54///
55/// Missing receive methods are rejected by the macro itself:
56///
57/// ```compile_fail
58/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
59/// struct Missing;
60/// #[behavior::behavior(
61///     addr = MailAddr,
62///     message = u8,
63///     sends = Vec<Never>,
64///     births = NoBirths,
65///     error = Never,
66/// )]
67/// impl Missing {
68/// }
69/// ```
70///
71/// Async behavior methods cannot introduce an erased or alternate execution
72/// path:
73///
74/// ```compile_fail
75/// use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
76/// struct Async;
77/// #[behavior::behavior(
78///     addr = MailAddr,
79///     message = u8,
80///     sends = Vec<Never>,
81///     births = NoBirths,
82///     error = Never,
83/// )]
84/// impl Async {
85///     async fn init(&mut self, _: crate::InitializationTurn) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
86///         Ok(Actions::cont())
87///     }
88///     fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Never>, NoBirths, Never> {
89///         Ok(Actions::cont())
90///     }
91/// }
92/// ```
93pub use behavior_macros::behavior;