typst_utils/
macros.rs

1/// Create a lazy initialized, globally unique `'static` reference to a value.
2#[macro_export]
3macro_rules! singleton {
4    ($ty:ty, $value:expr) => {{
5        static VALUE: ::std::sync::LazyLock<$ty> = ::std::sync::LazyLock::new(|| $value);
6        &*VALUE
7    }};
8}
9
10/// Implement the `Sub` trait based on existing `Neg` and `Add` impls.
11#[macro_export]
12macro_rules! sub_impl {
13    ($a:ident - $b:ident -> $c:ident) => {
14        impl ::core::ops::Sub<$b> for $a {
15            type Output = $c;
16
17            fn sub(self, other: $b) -> $c {
18                self + -other
19            }
20        }
21    };
22}
23
24/// Implement an assign trait based on an existing non-assign trait.
25#[macro_export]
26macro_rules! assign_impl {
27    ($a:ident += $b:ident) => {
28        impl ::core::ops::AddAssign<$b> for $a {
29            fn add_assign(&mut self, other: $b) {
30                *self = *self + other;
31            }
32        }
33    };
34
35    ($a:ident -= $b:ident) => {
36        impl ::core::ops::SubAssign<$b> for $a {
37            fn sub_assign(&mut self, other: $b) {
38                *self = *self - other;
39            }
40        }
41    };
42
43    ($a:ident *= $b:ident) => {
44        impl ::core::ops::MulAssign<$b> for $a {
45            fn mul_assign(&mut self, other: $b) {
46                *self = *self * other;
47            }
48        }
49    };
50
51    ($a:ident /= $b:ident) => {
52        impl ::core::ops::DivAssign<$b> for $a {
53            fn div_assign(&mut self, other: $b) {
54                *self = *self / other;
55            }
56        }
57    };
58}