zero_trust_rps/common/
immutable.rs

1use std::{fmt::Debug, ops::Deref};
2
3/// A container for values that can only be deref'd immutably.
4#[repr(transparent)]
5#[derive(Clone, Copy)]
6pub struct Immutable<T> {
7    value: T,
8}
9
10impl<T> Immutable<T> {
11    #[inline]
12    pub fn new(value: T) -> Self {
13        Immutable { value }
14    }
15}
16
17impl<T> Deref for Immutable<T> {
18    type Target = T;
19
20    fn deref(&self) -> &Self::Target {
21        &self.value
22    }
23}
24
25impl<T: Debug> Debug for Immutable<T> {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        self.value.fmt(f)
28    }
29}
30
31impl<T: Default> Default for Immutable<T> {
32    fn default() -> Self {
33        Immutable::new(Default::default())
34    }
35}
36impl<T> From<T> for Immutable<T> {
37    fn from(value: T) -> Self {
38        Immutable::new(value)
39    }
40}