zero_trust_rps/common/
immutable.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
40
use std::{fmt::Debug, ops::Deref};

/// A container for values that can only be deref'd immutably.
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Immutable<T> {
    value: T,
}

impl<T> Immutable<T> {
    #[inline]
    pub fn new(value: T) -> Self {
        Immutable { value }
    }
}

impl<T> Deref for Immutable<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T: Debug> Debug for Immutable<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.value.fmt(f)
    }
}

impl<T: Default> Default for Immutable<T> {
    fn default() -> Self {
        Immutable::new(Default::default())
    }
}
impl<T> From<T> for Immutable<T> {
    fn from(value: T) -> Self {
        Immutable::new(value)
    }
}