tensor_toolbox/
operation.rs

1use anchor_lang::prelude::*;
2
3/// Three-state operation that allows the ability to set, clear or do nothing with a value.
4/// Useful to use in lieu of an Option when the None variant could be ambiguous
5/// about whether to clear or do nothing.
6#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq, Eq, Hash)]
7pub enum Operation<T> {
8    None,
9    Clear,
10    Set(T),
11}
12
13impl<T> Operation<T> {
14    pub fn is_none(&self) -> bool {
15        matches!(self, Operation::None)
16    }
17
18    pub fn is_clear(&self) -> bool {
19        matches!(self, Operation::Clear)
20    }
21
22    pub fn is_set(&self) -> bool {
23        matches!(self, Operation::Set(_))
24    }
25
26    pub fn value(&self) -> Option<&T> {
27        match self {
28            Operation::Set(value) => Some(value),
29            _ => None,
30        }
31    }
32
33    pub fn into_value(self) -> Option<T> {
34        match self {
35            Operation::Set(value) => Some(value),
36            _ => None,
37        }
38    }
39}