sway_core/language/
purity.rsuse serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum Purity {
#[default]
Pure,
Reads,
Writes,
ReadsWrites,
}
impl Purity {
pub fn can_call(&self, other: Purity) -> bool {
match self {
Purity::Pure => other == Purity::Pure,
Purity::Reads => other == Purity::Pure || other == Purity::Reads,
Purity::Writes => true, Purity::ReadsWrites => true,
}
}
pub fn to_attribute_syntax(&self) -> String {
use sway_types::constants::*;
match self {
Purity::Pure => "".to_owned(),
Purity::Reads => STORAGE_PURITY_READ_NAME.to_owned(),
Purity::Writes => STORAGE_PURITY_WRITE_NAME.to_owned(),
Purity::ReadsWrites => {
format!("{STORAGE_PURITY_READ_NAME}, {STORAGE_PURITY_WRITE_NAME}")
}
}
}
}
pub fn promote_purity(from: Purity, to: Purity) -> Purity {
match (from, to) {
(Purity::Reads, Purity::Writes)
| (Purity::Writes, Purity::Reads)
| (Purity::ReadsWrites, _) => Purity::ReadsWrites,
_otherwise => to,
}
}