typst_utils/protected.rs
1/// Wraps a type, requiring justification on access.
2///
3/// On the type-system level, this does not do much, but it makes sure that
4/// users of the value think twice and justify their use.
5#[derive(Debug, Copy, Clone)]
6pub struct Protected<T>(T);
7
8impl<T> Protected<T> {
9 /// Wrap a value of type `T`.
10 pub fn new(inner: T) -> Self {
11 Self(inner)
12 }
13
14 /// Rewrap a value extracted via [`into_raw`](Self::into_raw).
15 ///
16 /// This is distinct from [`new`](Self::new) as it's only meant to be used
17 /// for rewrapping and not for initial wrapping.
18 pub fn from_raw(inner: T) -> Self {
19 Self(inner)
20 }
21
22 /// Extract the inner value without justification. The result may only be
23 /// used with [`from_raw`](Self::from_raw).
24 pub fn into_raw(self) -> T {
25 self.0
26 }
27
28 /// Access the underlying value, providing justification why it's okay.
29 pub fn access(&self, _justification: &'static str) -> &T {
30 &self.0
31 }
32}