mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-04 09:01:39 -05:00
use unsigned_abs
This commit is contained in:
parent
1b4407ab45
commit
70bc33217c
|
@ -37,7 +37,7 @@ impl From<&SmallInt> for BigInt {
|
|||
match s.0 {
|
||||
SmallIntType::Inline(i) => Self::from(i),
|
||||
SmallIntType::Heap((r, s)) => {
|
||||
let size = usize::try_from(s.abs()).unwrap();
|
||||
let size = s.unsigned_abs();
|
||||
let sign = s.signum();
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||
let bs = match sign {
|
||||
|
|
|
@ -109,7 +109,7 @@ impl TryFrom<SmallInt> for u128 {
|
|||
SmallIntType::Heap((r, s)) => {
|
||||
let mut ret: u128 = 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) };
|
||||
for i in slice {
|
||||
if bits >= 128 {
|
||||
|
@ -169,9 +169,9 @@ impl SmallUint {
|
|||
/// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error.
|
||||
pub fn from_smallint_unsigned(value: SmallInt) -> Self {
|
||||
match value.0 {
|
||||
SmallIntType::Inline(i) => Self::try_from(i.abs()).unwrap(),
|
||||
SmallIntType::Inline(i) => Self::from(i.unsigned_abs()),
|
||||
SmallIntType::Heap((r, s)) => {
|
||||
let size = usize::try_from(s.abs()).unwrap();
|
||||
let size = s.unsigned_abs();
|
||||
if size > 4 {
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||
let mut ret = vec![0; size];
|
||||
|
|
|
@ -213,12 +213,10 @@ logic_op! {
|
|||
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)) {
|
||||
(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)]
|
||||
|
|
|
@ -19,7 +19,7 @@ impl Neg for SmallInt {
|
|||
}
|
||||
},
|
||||
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 mut ret = vec![0; size];
|
||||
ret.clone_from_slice(slice);
|
||||
|
|
|
@ -69,8 +69,8 @@ impl PartialEq for SmallInt {
|
|||
Ordering::Greater => false,
|
||||
Ordering::Less => false,
|
||||
Ordering::Equal => {
|
||||
let us = usize::try_from(s.abs()).unwrap();
|
||||
let uj = usize::try_from(j.abs()).unwrap();
|
||||
let us = s.unsigned_abs();
|
||||
let uj = j.unsigned_abs();
|
||||
let slice1 = unsafe { core::slice::from_raw_parts(r, us) };
|
||||
let slice2 = unsafe { core::slice::from_raw_parts(i, uj) };
|
||||
for i in 0..*s {
|
||||
|
|
|
@ -107,8 +107,7 @@ impl core::fmt::UpperHex for SmallInt {
|
|||
_ => panic!("This should not happen."),
|
||||
};
|
||||
|
||||
let slice =
|
||||
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "{}0x{:X}", sign, i)?;
|
||||
|
@ -137,8 +136,7 @@ impl core::fmt::LowerHex for SmallInt {
|
|||
_ => panic!("This should not happen."),
|
||||
};
|
||||
|
||||
let slice =
|
||||
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "{}0x{:x}", sign, i)?;
|
||||
|
|
|
@ -22,7 +22,7 @@ pub enum SmallUintType {
|
|||
impl Drop for SmallInt {
|
||||
fn drop(&mut 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) };
|
||||
unsafe { std::mem::drop(Box::from_raw(slice)) }
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ impl Clone for SmallInt {
|
|||
match self.0 {
|
||||
SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)),
|
||||
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 mut ret = vec![0; size];
|
||||
ret.clone_from_slice(slice);
|
||||
|
@ -86,7 +86,7 @@ impl Hash for SmallInt {
|
|||
match self.0 {
|
||||
SmallIntType::Inline(i) => i.hash(state),
|
||||
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) };
|
||||
slice.hash(state);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user