nodedb_types/volatility.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! Function volatility: how far a call's result can be reused.
4//!
5//! Volatility is orthogonal to a function's category. A function is
6//! `Scalar` and `Volatile` at the same time.
7
8/// How far a function call's result can be reused.
9///
10/// Two states, because the engine has two reuse boundaries: a value folded
11/// into a cached plan, and a value produced fresh for one execution. A
12/// per-statement memoization boundary would need a third state; no such
13/// boundary exists, so no third state does either.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum Volatility {
16 /// Same arguments always give the same result. Foldable at plan time.
17 #[default]
18 Immutable,
19 /// Can change per call, or has side effects.
20 Volatile,
21}
22
23impl Volatility {
24 /// Whether a call can be folded to a literal at plan time.
25 pub fn is_foldable(self) -> bool {
26 matches!(self, Self::Immutable)
27 }
28
29 /// Whether a call must re-evaluate on every execution.
30 pub fn is_volatile(self) -> bool {
31 matches!(self, Self::Volatile)
32 }
33}