rune/hir/
macros.rs
1#[rustfmt::skip]
3macro_rules! alloc_with {
4 ($cx:expr, $span:expr) => {
5 #[allow(unused)]
6 macro_rules! alloc {
7 ($value:expr) => {
8 $cx.arena.alloc($value).map_err(|e| {
9 $crate::compile::Error::new(
10 &*$span,
11 $crate::compile::ErrorKind::ArenaAllocError {
12 requested: e.requested,
13 },
14 )
15 })?
16 };
17 }
18
19 #[allow(unused)]
20 macro_rules! option {
21 ($value:expr) => {
22 option!($value, |value| value)
23 };
24
25 ($value:expr, |$pat:pat_param| $closure:expr) => {
26 match $value {
27 Some($pat) => {
28 Some(&*alloc!($closure))
29 }
30 None => {
31 None
32 }
33 }
34 };
35 }
36
37 #[allow(unused)]
38 macro_rules! iter {
39 ($iter:expr) => {
40 iter!($iter, |value| value)
41 };
42
43 ($iter:expr, |$pat:pat_param| $closure:expr) => {
44 iter!($iter, it, ExactSizeIterator::len(&it), |$pat| $closure)
45 };
46
47 ($iter:expr, $len:expr, |$pat:pat_param| $closure:expr) => {
48 iter!($iter, it, $len, |$pat| $closure)
49 };
50
51 ($iter:expr, $it:ident, $len:expr, |$pat:pat_param| $closure:expr) => {{
52 let mut $it = IntoIterator::into_iter($iter);
53
54 let mut writer = match $cx.arena.alloc_iter($len) {
55 Ok(writer) => writer,
56 Err(e) => {
57 return Err($crate::compile::Error::new(
58 &*$span,
59 $crate::compile::ErrorKind::ArenaAllocError {
60 requested: e.requested,
61 },
62 ));
63 }
64 };
65
66 while let Some($pat) = $it.next() {
67 if let Err(e) = writer.write($closure) {
68 return Err($crate::compile::Error::new(
69 &*$span,
70 $crate::compile::ErrorKind::ArenaWriteSliceOutOfBounds { index: e.index },
71 ));
72 }
73 }
74
75 writer.finish()
76 }};
77 }
78
79 #[allow(unused)]
80 macro_rules! alloc_str {
81 ($value:expr) => {
82 match $cx.arena.alloc_str($value) {
83 Ok(string) => string,
84 Err(e) => return Err($crate::compile::Error::new(
85 &*$span,
86 $crate::compile::ErrorKind::ArenaAllocError {
87 requested: e.requested,
88 },
89 )),
90 }
91 };
92 }
93
94 #[allow(unused)]
95 macro_rules! alloc_bytes {
96 ($value:expr) => {
97 match $cx.arena.alloc_bytes($value) {
98 Ok(bytes) => bytes,
99 Err(e) => return Err($crate::compile::Error::new(
100 &*$span,
101 $crate::compile::ErrorKind::ArenaAllocError {
102 requested: e.requested,
103 },
104 )),
105 }
106 };
107 }
108 };
109}