Skip to content

Commit

Permalink
Merge pull request #305 from cuviper/alloc
Browse files Browse the repository at this point in the history
Commit to `alloc` as much as possible
  • Loading branch information
cuviper committed May 10, 2024
2 parents 98006b2 + f12e812 commit 2435646
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/bigint.rs
@@ -1,7 +1,8 @@
// `Add`/`Sub` ops may flip from `BigInt` to its `BigUint` magnitude
#![allow(clippy::suspicious_arithmetic_impl)]

use crate::std_alloc::{String, Vec};
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::Ordering::{self, Equal};
use core::default::Default;
use core::fmt;
Expand Down
4 changes: 2 additions & 2 deletions src/bigint/arbitrary.rs
@@ -1,8 +1,8 @@
use super::{BigInt, Sign};
use crate::BigUint;

#[cfg(feature = "quickcheck")]
use crate::std_alloc::Box;
use crate::BigUint;
use alloc::boxed::Box;

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigInt {
Expand Down
2 changes: 1 addition & 1 deletion src/bigint/bits.rs
Expand Up @@ -3,8 +3,8 @@ use super::Sign::{Minus, NoSign, Plus};

use crate::big_digit::{self, BigDigit, DoubleBigDigit};
use crate::biguint::IntDigits;
use crate::std_alloc::Vec;

use alloc::vec::Vec;
use core::cmp::Ordering::{Equal, Greater, Less};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
use num_traits::{ToPrimitive, Zero};
Expand Down
2 changes: 1 addition & 1 deletion src/bigint/convert.rs
@@ -1,10 +1,10 @@
use super::Sign::{self, Minus, NoSign, Plus};
use super::{BigInt, ToBigInt};

use crate::std_alloc::Vec;
use crate::TryFromBigIntError;
use crate::{BigUint, ParseBigIntError, ToBigUint};

use alloc::vec::Vec;
use core::cmp::Ordering::{Equal, Greater, Less};
use core::convert::TryFrom;
use core::str::{self, FromStr};
Expand Down
3 changes: 2 additions & 1 deletion src/biguint.rs
@@ -1,6 +1,7 @@
use crate::big_digit::{self, BigDigit};
use crate::std_alloc::{String, Vec};

use alloc::string::String;
use alloc::vec::Vec;
use core::cmp;
use core::cmp::Ordering;
use core::default::Default;
Expand Down
4 changes: 2 additions & 2 deletions src/biguint/arbitrary.rs
Expand Up @@ -2,8 +2,8 @@ use super::{biguint_from_vec, BigUint};

use crate::big_digit::BigDigit;
#[cfg(feature = "quickcheck")]
use crate::std_alloc::Box;
use crate::std_alloc::Vec;
use alloc::boxed::Box;
use alloc::vec::Vec;

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigUint {
Expand Down
2 changes: 1 addition & 1 deletion src/biguint/convert.rs
Expand Up @@ -8,10 +8,10 @@ use super::division::{div_rem_digit, FAST_DIV_WIDE};
use super::multiplication::mac_with_carry;

use crate::big_digit::{self, BigDigit};
use crate::std_alloc::Vec;
use crate::ParseBigIntError;
use crate::TryFromBigIntError;

use alloc::vec::Vec;
use core::cmp::Ordering::{Equal, Greater, Less};
use core::convert::TryFrom;
use core::mem;
Expand Down
2 changes: 1 addition & 1 deletion src/biguint/monty.rs
@@ -1,4 +1,4 @@
use crate::std_alloc::Vec;
use alloc::vec::Vec;
use core::mem;
use core::ops::Shl;
use num_traits::One;
Expand Down
3 changes: 1 addition & 2 deletions src/biguint/serde.rs
@@ -1,7 +1,6 @@
use super::{biguint_from_vec, BigUint};

use crate::std_alloc::Vec;

use alloc::vec::Vec;
use core::{cmp, fmt, mem};
use serde::de::{SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down
3 changes: 2 additions & 1 deletion src/biguint/shift.rs
@@ -1,8 +1,9 @@
use super::{biguint_from_vec, BigUint};

use crate::big_digit;
use crate::std_alloc::{Cow, Vec};

use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::mem;
use core::ops::{Shl, ShlAssign, Shr, ShrAssign};
use num_traits::{PrimInt, Zero};
Expand Down
36 changes: 7 additions & 29 deletions src/lib.rs
Expand Up @@ -60,10 +60,10 @@
//!
//! ## Features
//!
//! The `std` crate feature is enabled by default, and is mandatory before Rust
//! 1.36 and the stabilized `alloc` crate. If you depend on `num-bigint` with
//! `default-features = false`, you must manually enable the `std` feature yourself
//! if your compiler is not new enough.
//! The `std` crate feature is enabled by default, which enables [`std::error::Error`]
//! implementations and some internal use of floating point approximations. This can be disabled by
//! depending on `num-bigint` with `default-features = false`. Either way, the `alloc` crate is
//! always required for heap allocation of the `BigInt`/`BigUint` digits.
//!
//! ### Random Generation
//!
Expand All @@ -87,35 +87,13 @@
#![warn(rust_2018_idioms)]
#![no_std]

#[cfg(feature = "std")]
#[macro_use]
extern crate std;

#[cfg(feature = "std")]
mod std_alloc {
pub(crate) use std::borrow::Cow;
#[cfg(feature = "quickcheck")]
pub(crate) use std::boxed::Box;
pub(crate) use std::string::String;
pub(crate) use std::vec::Vec;
}

#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;

#[cfg(not(feature = "std"))]
mod std_alloc {
pub(crate) use alloc::borrow::Cow;
#[cfg(feature = "quickcheck")]
pub(crate) use alloc::boxed::Box;
pub(crate) use alloc::string::String;
pub(crate) use alloc::vec::Vec;
}
#[cfg(feature = "std")]
extern crate std;

use core::fmt;
#[cfg(feature = "std")]
use std::error::Error;

#[macro_use]
mod macros;
Expand Down Expand Up @@ -176,7 +154,7 @@ impl fmt::Display for ParseBigIntError {
}

#[cfg(feature = "std")]
impl Error for ParseBigIntError {
impl std::error::Error for ParseBigIntError {
fn description(&self) -> &str {
self.__description()
}
Expand Down

0 comments on commit 2435646

Please sign in to comment.