byteflow/bytecode/restart.rs
1//! Supervisor restart policy (shared by bytecode `SetRestartPolicy` and host
2//! [`crate::Supervisor`] / [`crate::ChildSpec`]).
3
4/// Restart policy consulted when a supervised flow terminates.
5///
6/// Bytecode [`super::Opcode::SetRestartPolicy`] encodes:
7/// `0` = [`Self::Always`], `1` = [`Self::OnFailure`], `2` = [`Self::Never`].
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum RestartPolicy {
10 Always,
11 OnFailure,
12 Never,
13}
14
15impl RestartPolicy {
16 /// Encode as the `SetRestartPolicy` immediate (`0..=2`).
17 #[inline]
18 pub fn as_u8(self) -> u8 {
19 match self {
20 RestartPolicy::Always => 0,
21 RestartPolicy::OnFailure => 1,
22 RestartPolicy::Never => 2,
23 }
24 }
25
26 /// Decode a `SetRestartPolicy` immediate.
27 #[inline]
28 pub fn from_u8(byte: u8) -> Option<RestartPolicy> {
29 match byte {
30 0 => Some(RestartPolicy::Always),
31 1 => Some(RestartPolicy::OnFailure),
32 2 => Some(RestartPolicy::Never),
33 _ => None,
34 }
35 }
36}