refactor macro structure

This commit is contained in:
Solomon Ucko 2022-08-09 23:42:27 -04:00
parent 8cf2d6b870
commit 06ce35aa69

View File

@ -4,8 +4,29 @@ use core::mem::ManuallyDrop;
use core::ops::{BitAnd, BitOr, BitXor};
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign};
macro_rules! basic_op {
($imp:ident, $typ:ty, $fun:ident) => {
macro_rules! logic_op {
(
$imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident;
$i:ident, $j:ident, $r:ident, $s:ident;
$inline_heap:tt, $heap_heap:tt
) => {
fn $fun(a: &$typ, b: &$typ) -> $typ {
match (&a.0, &b.0) {
(&$typ_inner::Inline($i), &$typ_inner::Inline($j)) => {
$typ($typ_inner::Inline($i.$fun($j)))
}
(&$typ_inner::Inline($i), &$typ_inner::Heap(($r, $s)))
| (&$typ_inner::Heap(($r, $s)), &$typ_inner::Inline($i)) => {
$inline_heap
}
(&$typ_inner::Heap(($r, $s)), &$typ_inner::Heap(($i, $j))) => {
$heap_heap
}
}
}
impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
type Output = $typ;
@ -37,43 +58,6 @@ macro_rules! basic_op {
(&self).$fun(&rhs)
}
}
};
}
macro_rules! logic_op {
(
$imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident;
$i:ident, $j:ident, $r:ident, $s:ident, $slice:ident, $slice1:ident, $slice2:ident, $min:ident, $res:ident;
$inline_heap:tt, $heap_heap_create_res:tt, $heap_heap_return:tt
) => {
fn $fun(a: &$typ, b: &$typ) -> $typ {
match (&a.0, &b.0) {
(&$typ_inner::Inline($i), &$typ_inner::Inline($j)) => {
$typ($typ_inner::Inline($i.$fun($j)))
}
(&$typ_inner::Inline($i), &$typ_inner::Heap(($r, $s)))
| (&$typ_inner::Heap(($r, $s)), &$typ_inner::Inline($i)) => {
let $slice = unsafe { core::slice::from_raw_parts($r, $s) };
$inline_heap
}
(&$typ_inner::Heap(($r, $s)), &$typ_inner::Heap(($i, $j))) => {
let $slice1 = unsafe { core::slice::from_raw_parts($r, $s) };
let $slice2 = unsafe { core::slice::from_raw_parts($i, $j) };
let $min = std::cmp::min($slice1.len(), $slice2.len());
#[allow(unused_mut)]
let mut $res = $heap_heap_create_res;
$heap_heap_return
}
}
}
basic_op!($imp, $typ, $fun);
impl<'a> $imp_assign<&'a SmallUint> for $typ {
fn $fun_assign(&mut self, rhs: &'a $typ) {
@ -86,7 +70,33 @@ macro_rules! logic_op {
*self = (&*self).$fun(rhs);
}
}
};
(
$imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident;
$i:ident, $j:ident, $r:ident, $s:ident, $slice:ident, $slice1:ident, $slice2:ident, $min:ident, $res:ident;
$inline_heap_inner:tt, $heap_heap_create_res:tt, $heap_heap_return:tt
) => {
logic_op! {
$imp, $imp_assign, $typ, $typ_inner, $fun, $fun_assign;
$i, $j, $r, $s;
{
let $slice = unsafe { core::slice::from_raw_parts($r, $s) };
$inline_heap_inner
},
{
let $slice1 = unsafe { core::slice::from_raw_parts($r, $s) };
let $slice2 = unsafe { core::slice::from_raw_parts($i, $j) };
let $min = std::cmp::min($slice1.len(), $slice2.len());
#[allow(unused_mut)]
let mut $res = $heap_heap_create_res;
$heap_heap_return
}
}
};
}