Skip to main content

byteflow/bytecode/
restart.rs

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