From 1b4407ab451d353ec7ec9ced294f4d71608b8bfc Mon Sep 17 00:00:00 2001 From: Solomon Ucko Date: Wed, 10 Aug 2022 21:27:09 -0400 Subject: [PATCH] make heap_heap_return_* compatible with BigInt --- src/logic.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/logic.rs b/src/logic.rs index 49dcd08..2280c02 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -1,5 +1,6 @@ -use crate::smallint::SmallUintType; -use crate::SmallUint; +use crate::smallint::{SmallIntType, SmallUintType}; +use crate::{SmallInt, SmallUint}; +use core::cmp::Ordering; use core::mem::ManuallyDrop; use core::ops::{BitAnd, BitOr, BitXor}; use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign}; @@ -154,7 +155,7 @@ macro_rules! heap_heap_create_res_longest { macro_rules! heap_heap_return_heap { ($typ:ident, $typ_inner:ident; $res:ident) => { let mut slice = ManuallyDrop::new($res.into_boxed_slice()); - $typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len()))) + $typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len().try_into().unwrap()))) } } @@ -173,7 +174,7 @@ macro_rules! heap_heap_return_any { $typ($typ_inner::Inline(r)) } else { let mut slice = ManuallyDrop::new($res.into_boxed_slice()); - $typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len()))) + $typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len().try_into().unwrap()))) } } } @@ -203,3 +204,32 @@ logic_op! { } + + +logic_op! { + BitXor, BitXorAssign, SmallInt, SmallIntType, bitxor, bitxor_assign; + i, j, p, q, s, t; + { + todo!() + }, + { + match (s.cmp(&0), t.cmp(&0)) { + (Ordering::Greater, Ordering::Greater) => { + // as usize is equivalent to abs for non-negative isize + let slice1 = unsafe { core::slice::from_raw_parts(p, s as usize) }; + let slice2 = unsafe { core::slice::from_raw_parts(q, t as usize) }; + + let min = std::cmp::min(slice1.len(), slice2.len()); + + #[allow(unused_mut)] + let mut res = { heap_heap_create_res_longest! { bitxor; slice1, slice2, min } }; + + heap_heap_return_any! { SmallInt, SmallIntType, i128; res } + }, + (Ordering::Greater, Ordering::Less) => todo!(), + (Ordering::Less, Ordering::Greater) => todo!(), + (Ordering::Less, Ordering::Less) => todo!(), + (Ordering::Equal, _) | (_, Ordering::Equal) => unreachable!("0 must be inline"), + } + } +}