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