wip adding register_allocator v3
[bigint-presentation-code.git] / register_allocator / src / macros.rs
1 macro_rules! validated_fields {
2 (
3 #[fields_ty = $fields_ty:ident]
4 $(#[$meta:meta])*
5 $vis:vis struct $ty:ident $body:tt
6 ) => {
7 $(#[$meta])*
8 #[derive(::serde::Serialize, ::serde::Deserialize)]
9 $vis struct $fields_ty $body
10
11 $(#[$meta])*
12 $vis struct $ty($fields_ty);
13
14 impl TryFrom<$fields_ty> for $ty {
15 type Error = crate::error::Error;
16
17 fn try_from(fields: $fields_ty) -> Result<Self, Self::Error> {
18 $ty::new(fields)
19 }
20 }
21
22 impl From<$ty> for $fields_ty {
23 fn from(value: $ty) -> Self {
24 value.0
25 }
26 }
27
28 impl ::std::ops::Deref for $ty {
29 type Target = $fields_ty;
30
31 fn deref(&self) -> &Self::Target {
32 &self.0
33 }
34 }
35
36 impl $ty {
37 $vis const fn get(&self) -> &$fields_ty {
38 &self.0
39 }
40 }
41
42 impl ::serde::Serialize for $ty {
43 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
44 where
45 S: ::serde::Serializer,
46 {
47 self.0.serialize(serializer)
48 }
49 }
50
51 impl<'de> ::serde::Deserialize<'de> for $ty {
52 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53 where
54 D: ::serde::Deserializer<'de>,
55 {
56 $fields_ty::deserialize(deserializer)?
57 .try_into()
58 .map_err(<D::Error as ::serde::de::Error>::custom)
59 }
60 }
61 };
62 }
63
64 macro_rules! const_unwrap_opt {
65 ($v:expr $(, $($msg:literal $(,)?)?)?) => {
66 match $v {
67 Some(v) => v,
68 None => panic!(concat!("tried to unwrap None", $($(": ", $msg,)?)?)),
69 }
70 };
71 }
72
73 macro_rules! const_unwrap_res {
74 ($v:expr $(, $($msg:literal $(,)?)?)?) => {
75 match $v {
76 Ok(v) => v,
77 Err(_e) => panic!(concat!("tried to unwrap Err", $($(": ", $msg,)?)?)),
78 }
79 };
80 }
81
82 macro_rules! const_try {
83 ($v:expr, Err($var:ident) => Err($err:expr) $(,)?) => {
84 match $v {
85 Ok(v) => v,
86 Err($var) => return Err($err),
87 }
88 };
89 ($v:expr $(,)?) => {
90 match $v {
91 Ok(v) => v,
92 Err(e) => return Err(e),
93 }
94 };
95 }
96
97 macro_rules! nzu32_lit {
98 ($v:literal) => {{
99 const V: NonZeroU32 = match NonZeroU32::new($v) {
100 Some(v) => v,
101 None => panic!("literal must not be zero"),
102 };
103 V
104 }};
105 }