feature bigint

This commit is contained in:
EvilMuffinHa 2022-08-02 09:21:00 -04:00
parent a5620cdda9
commit bd10994f4e
2 changed files with 14 additions and 3 deletions

View File

@ -6,4 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
num-bigint = "0.4.3" num-bigint = { version = "0.4.3", optional = true }
[dev-dependencies]
num-bigint = { version = "0.4.3" }

View File

@ -5,8 +5,12 @@
//! inline an integer and store it on the stack if that integer is small. However, for larger values, //! inline an integer and store it on the stack if that integer is small. However, for larger values,
//! this will be instead stored on the heap as a pointer to a `u32` slice, a length, and a sign. //! this will be instead stored on the heap as a pointer to a `u32` slice, a length, and a sign.
#[cfg(feature="num-bigint")]
use num_bigint::BigInt; use num_bigint::BigInt;
#[cfg(feature="num-bigint")]
use num_bigint::Sign; use num_bigint::Sign;
use core::mem::ManuallyDrop; use core::mem::ManuallyDrop;
/// An error that occurred when processing a SmallInt. /// An error that occurred when processing a SmallInt.
@ -132,6 +136,7 @@ impl TryFrom<SmallInt> for u128 {
} }
#[cfg(feature="num-bigint")]
impl From<BigInt> for SmallInt { impl From<BigInt> for SmallInt {
fn from(b: BigInt) -> Self { fn from(b: BigInt) -> Self {
match (&b).try_into() { match (&b).try_into() {
@ -149,9 +154,8 @@ impl From<BigInt> for SmallInt {
} }
} }
#[cfg(feature="num-bigint")]
impl From<SmallInt> for BigInt { impl From<SmallInt> for BigInt {
fn from(s: SmallInt) -> Self { fn from(s: SmallInt) -> Self {
match s { match s {
SmallInt::Inline(i) => Self::from(i), SmallInt::Inline(i) => Self::from(i),
@ -176,6 +180,8 @@ impl From<SmallInt> for BigInt {
mod conversion_tests { mod conversion_tests {
use crate::SmallInt; use crate::SmallInt;
#[cfg(feature="num-bigint")]
use num_bigint::{BigInt, Sign}; use num_bigint::{BigInt, Sign};
macro_rules! conversion_tests { macro_rules! conversion_tests {
@ -206,6 +212,7 @@ mod conversion_tests {
conversion_tests!(i128, test_i128); conversion_tests!(i128, test_i128);
#[test] #[test]
#[cfg(feature="num-bigint")]
fn test_bigint() { fn test_bigint() {
let i = BigInt::new(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]); let i = BigInt::new(Sign::Plus, vec![5, 4, 8, 3, 2, 9, 3]);
let s = SmallInt::from(i); let s = SmallInt::from(i);