From 06ce35aa69121d4ef418bf14f6b932ec67217142 Mon Sep 17 00:00:00 2001 From: Solomon Ucko Date: Tue, 9 Aug 2022 23:42:27 -0400 Subject: [PATCH] refactor macro structure --- src/logic.rs | 90 +++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/src/logic.rs b/src/logic.rs index e26243a..a163279 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -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 + } + } + }; }