Merge pull request #154 from briansmith/optional-rand-rustc_serialize
Make `rand` and `rustc-serialize` dependencies optional.
This commit is contained in:
commit
9ec4106a99
|
@ -17,12 +17,17 @@ complex, rational, range iterators, generic integers, and more!
|
||||||
rustc-serialize = { version = "0.3.13", optional = true }
|
rustc-serialize = { version = "0.3.13", optional = true }
|
||||||
rand = { version = "0.3.8", optional = true }
|
rand = { version = "0.3.8", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
# Some tests of non-rand functionality still use rand because the tests
|
||||||
|
# themselves are randomized.
|
||||||
|
rand = { version = "0.3.8" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
||||||
complex = []
|
complex = []
|
||||||
rational = []
|
rational = []
|
||||||
bigint = ["rustc-serialize", "rand"]
|
bigint = []
|
||||||
default = ["complex", "rational", "bigint"]
|
default = ["bigint", "complex", "rand", "rational", "rustc-serialize"]
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "bigint"
|
name = "bigint"
|
||||||
|
|
|
@ -43,6 +43,8 @@
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! extern crate rand;
|
//! extern crate rand;
|
||||||
//! extern crate num;
|
//! extern crate num;
|
||||||
|
//!
|
||||||
|
//! # #[cfg(feature = "rand")]
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! use num::bigint::{ToBigInt, RandBigInt};
|
//! use num::bigint::{ToBigInt, RandBigInt};
|
||||||
//!
|
//!
|
||||||
|
@ -56,6 +58,10 @@
|
||||||
//! // Probably an even larger number.
|
//! // Probably an even larger number.
|
||||||
//! println!("{}", a * b);
|
//! println!("{}", a * b);
|
||||||
//! # }
|
//! # }
|
||||||
|
//!
|
||||||
|
//! # #[cfg(not(feature = "rand"))]
|
||||||
|
//! # fn main() {
|
||||||
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use Integer;
|
use Integer;
|
||||||
|
@ -71,6 +77,10 @@ use std::cmp::Ordering::{self, Less, Greater, Equal};
|
||||||
use std::{f32, f64};
|
use std::{f32, f64};
|
||||||
use std::{u8, i64, u64};
|
use std::{u8, i64, u64};
|
||||||
|
|
||||||
|
// Some of the tests of non-RNG-based functionality are randomized using the
|
||||||
|
// RNG-based functionality, so the RNG-based functionality needs to be enabled
|
||||||
|
// for tests.
|
||||||
|
#[cfg(any(feature = "rand", test))]
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use traits::{ToPrimitive, FromPrimitive};
|
use traits::{ToPrimitive, FromPrimitive};
|
||||||
|
@ -173,11 +183,13 @@ fn div_wide(hi: BigDigit, lo: BigDigit, divisor: BigDigit) -> (BigDigit, BigDigi
|
||||||
let rhs = divisor as DoubleBigDigit;
|
let rhs = divisor as DoubleBigDigit;
|
||||||
((lhs / rhs) as BigDigit, (lhs % rhs) as BigDigit)
|
((lhs / rhs) as BigDigit, (lhs % rhs) as BigDigit)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A big unsigned integer type.
|
/// A big unsigned integer type.
|
||||||
///
|
///
|
||||||
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
|
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
|
||||||
/// `(a + b * big_digit::BASE + c * big_digit::BASE^2)`.
|
/// `(a + b * big_digit::BASE + c * big_digit::BASE^2)`.
|
||||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)]
|
#[derive(Clone, Debug, Hash)]
|
||||||
|
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
|
||||||
pub struct BigUint {
|
pub struct BigUint {
|
||||||
data: Vec<BigDigit>
|
data: Vec<BigDigit>
|
||||||
}
|
}
|
||||||
|
@ -1729,7 +1741,8 @@ fn get_radix_base(radix: u32) -> (DoubleBigDigit, usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A Sign is a `BigInt`'s composing element.
|
/// A Sign is a `BigInt`'s composing element.
|
||||||
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, RustcEncodable, RustcDecodable, Hash)]
|
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)]
|
||||||
|
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
|
||||||
pub enum Sign { Minus, NoSign, Plus }
|
pub enum Sign { Minus, NoSign, Plus }
|
||||||
|
|
||||||
impl Neg for Sign {
|
impl Neg for Sign {
|
||||||
|
@ -1760,7 +1773,8 @@ impl Mul<Sign> for Sign {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A big signed integer type.
|
/// A big signed integer type.
|
||||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)]
|
#[derive(Clone, Debug, Hash)]
|
||||||
|
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
|
||||||
pub struct BigInt {
|
pub struct BigInt {
|
||||||
sign: Sign,
|
sign: Sign,
|
||||||
data: BigUint
|
data: BigUint
|
||||||
|
@ -2387,6 +2401,7 @@ pub trait RandBigInt {
|
||||||
fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt;
|
fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "rand", test))]
|
||||||
impl<R: Rng> RandBigInt for R {
|
impl<R: Rng> RandBigInt for R {
|
||||||
fn gen_biguint(&mut self, bit_size: usize) -> BigUint {
|
fn gen_biguint(&mut self, bit_size: usize) -> BigUint {
|
||||||
let (digits, rem) = bit_size.div_rem(&big_digit::BITS);
|
let (digits, rem) = bit_size.div_rem(&big_digit::BITS);
|
||||||
|
|
|
@ -59,7 +59,11 @@
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
#[cfg(feature = "rustc-serialize")]
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
#[cfg(feature = "rand")]
|
|
||||||
|
// Some of the tests of non-RNG-based functionality are randomized using the
|
||||||
|
// RNG-based functionality, so the RNG-based functionality needs to be enabled
|
||||||
|
// for tests.
|
||||||
|
#[cfg(any(feature = "rand", all(feature = "bigint", test)))]
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
#[cfg(feature = "bigint")]
|
#[cfg(feature = "bigint")]
|
||||||
|
|
Loading…
Reference in New Issue