use unsigned_abs

This commit is contained in:
Solomon Ucko 2022-08-10 23:10:32 -04:00
parent 1b4407ab45
commit 70bc33217c
7 changed files with 14 additions and 18 deletions

View File

@ -37,7 +37,7 @@ impl From<&SmallInt> for BigInt {
match s.0 { match s.0 {
SmallIntType::Inline(i) => Self::from(i), SmallIntType::Inline(i) => Self::from(i),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let sign = s.signum(); let sign = s.signum();
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
let bs = match sign { let bs = match sign {

View File

@ -109,7 +109,7 @@ impl TryFrom<SmallInt> for u128 {
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let mut ret: u128 = 0; let mut ret: u128 = 0;
let mut bits = 0; let mut bits = 0;
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
for i in slice { for i in slice {
if bits >= 128 { if bits >= 128 {
@ -169,9 +169,9 @@ impl SmallUint {
/// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error. /// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error.
pub fn from_smallint_unsigned(value: SmallInt) -> Self { pub fn from_smallint_unsigned(value: SmallInt) -> Self {
match value.0 { match value.0 {
SmallIntType::Inline(i) => Self::try_from(i.abs()).unwrap(), SmallIntType::Inline(i) => Self::from(i.unsigned_abs()),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
if size > 4 { if size > 4 {
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
let mut ret = vec![0; size]; let mut ret = vec![0; size];

View File

@ -213,12 +213,10 @@ logic_op! {
todo!() todo!()
}, },
{ {
let slice1 = unsafe { core::slice::from_raw_parts(p, s.unsigned_abs()) };
let slice2 = unsafe { core::slice::from_raw_parts(q, t as usize) };
match (s.cmp(&0), t.cmp(&0)) { match (s.cmp(&0), t.cmp(&0)) {
(Ordering::Greater, Ordering::Greater) => { (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()); let min = std::cmp::min(slice1.len(), slice2.len());
#[allow(unused_mut)] #[allow(unused_mut)]

View File

@ -19,7 +19,7 @@ impl Neg for SmallInt {
} }
}, },
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
let mut ret = vec![0; size]; let mut ret = vec![0; size];
ret.clone_from_slice(slice); ret.clone_from_slice(slice);

View File

@ -69,8 +69,8 @@ impl PartialEq for SmallInt {
Ordering::Greater => false, Ordering::Greater => false,
Ordering::Less => false, Ordering::Less => false,
Ordering::Equal => { Ordering::Equal => {
let us = usize::try_from(s.abs()).unwrap(); let us = s.unsigned_abs();
let uj = usize::try_from(j.abs()).unwrap(); let uj = j.unsigned_abs();
let slice1 = unsafe { core::slice::from_raw_parts(r, us) }; let slice1 = unsafe { core::slice::from_raw_parts(r, us) };
let slice2 = unsafe { core::slice::from_raw_parts(i, uj) }; let slice2 = unsafe { core::slice::from_raw_parts(i, uj) };
for i in 0..*s { for i in 0..*s {

View File

@ -107,8 +107,7 @@ impl core::fmt::UpperHex for SmallInt {
_ => panic!("This should not happen."), _ => panic!("This should not happen."),
}; };
let slice = let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
let mut iter = slice.iter().rev(); let mut iter = slice.iter().rev();
if let Some(i) = iter.next() { if let Some(i) = iter.next() {
write!(f, "{}0x{:X}", sign, i)?; write!(f, "{}0x{:X}", sign, i)?;
@ -137,8 +136,7 @@ impl core::fmt::LowerHex for SmallInt {
_ => panic!("This should not happen."), _ => panic!("This should not happen."),
}; };
let slice = let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
let mut iter = slice.iter().rev(); let mut iter = slice.iter().rev();
if let Some(i) = iter.next() { if let Some(i) = iter.next() {
write!(f, "{}0x{:x}", sign, i)?; write!(f, "{}0x{:x}", sign, i)?;

View File

@ -22,7 +22,7 @@ pub enum SmallUintType {
impl Drop for SmallInt { impl Drop for SmallInt {
fn drop(&mut self) { fn drop(&mut self) {
if let Self(SmallIntType::Heap((r, s))) = self { if let Self(SmallIntType::Heap((r, s))) = self {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let slice = unsafe { core::slice::from_raw_parts_mut(*r, size) }; let slice = unsafe { core::slice::from_raw_parts_mut(*r, size) };
unsafe { std::mem::drop(Box::from_raw(slice)) } unsafe { std::mem::drop(Box::from_raw(slice)) }
} }
@ -58,7 +58,7 @@ impl Clone for SmallInt {
match self.0 { match self.0 {
SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)), SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
let mut ret = vec![0; size]; let mut ret = vec![0; size];
ret.clone_from_slice(slice); ret.clone_from_slice(slice);
@ -86,7 +86,7 @@ impl Hash for SmallInt {
match self.0 { match self.0 {
SmallIntType::Inline(i) => i.hash(state), SmallIntType::Inline(i) => i.hash(state),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap(); let size = s.unsigned_abs();
let slice = unsafe { core::slice::from_raw_parts(r, size) }; let slice = unsafe { core::slice::from_raw_parts(r, size) };
slice.hash(state); slice.hash(state);
} }