sway_core/language/ty/
variable_mutability.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::language::Visibility;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum VariableMutability {
    // mutable
    Mutable,
    // referenceable + mutable
    RefMutable,
    // immutable
    #[default]
    Immutable,
}

impl VariableMutability {
    pub fn new_from_ref_mut(is_reference: bool, is_mutable: bool) -> VariableMutability {
        if is_reference {
            VariableMutability::RefMutable
        } else if is_mutable {
            VariableMutability::Mutable
        } else {
            VariableMutability::Immutable
        }
    }

    pub fn is_mutable(&self) -> bool {
        matches!(
            self,
            VariableMutability::Mutable | VariableMutability::RefMutable
        )
    }

    pub fn visibility(&self) -> Visibility {
        Visibility::Private
    }

    pub fn is_immutable(&self) -> bool {
        !self.is_mutable()
    }
}