Skip to content

Commit 9ec4106

Browse files
committed
Merge pull request #154 from briansmith/optional-rand-rustc_serialize
Make `rand` and `rustc-serialize` dependencies optional.
2 parents 22722ac + 9e3e855 commit 9ec4106

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ complex, rational, range iterators, generic integers, and more!
1717
rustc-serialize = { version = "0.3.13", optional = true }
1818
rand = { version = "0.3.8", optional = true }
1919

20+
[dev-dependencies]
21+
# Some tests of non-rand functionality still use rand because the tests
22+
# themselves are randomized.
23+
rand = { version = "0.3.8" }
24+
2025
[features]
2126

2227
complex = []
2328
rational = []
24-
bigint = ["rustc-serialize", "rand"]
25-
default = ["complex", "rational", "bigint"]
29+
bigint = []
30+
default = ["bigint", "complex", "rand", "rational", "rustc-serialize"]
2631

2732
[[bench]]
2833
name = "bigint"

src/bigint.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
//! ```rust
4444
//! extern crate rand;
4545
//! extern crate num;
46+
//!
47+
//! # #[cfg(feature = "rand")]
4648
//! # fn main() {
4749
//! use num::bigint::{ToBigInt, RandBigInt};
4850
//!
@@ -56,6 +58,10 @@
5658
//! // Probably an even larger number.
5759
//! println!("{}", a * b);
5860
//! # }
61+
//!
62+
//! # #[cfg(not(feature = "rand"))]
63+
//! # fn main() {
64+
//! # }
5965
//! ```
6066
6167
use Integer;
@@ -71,6 +77,10 @@ use std::cmp::Ordering::{self, Less, Greater, Equal};
7177
use std::{f32, f64};
7278
use std::{u8, i64, u64};
7379

80+
// Some of the tests of non-RNG-based functionality are randomized using the
81+
// RNG-based functionality, so the RNG-based functionality needs to be enabled
82+
// for tests.
83+
#[cfg(any(feature = "rand", test))]
7484
use rand::Rng;
7585

7686
use traits::{ToPrimitive, FromPrimitive};
@@ -173,11 +183,13 @@ fn div_wide(hi: BigDigit, lo: BigDigit, divisor: BigDigit) -> (BigDigit, BigDigi
173183
let rhs = divisor as DoubleBigDigit;
174184
((lhs / rhs) as BigDigit, (lhs % rhs) as BigDigit)
175185
}
186+
176187
/// A big unsigned integer type.
177188
///
178189
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
179190
/// `(a + b * big_digit::BASE + c * big_digit::BASE^2)`.
180-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)]
191+
#[derive(Clone, Debug, Hash)]
192+
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
181193
pub struct BigUint {
182194
data: Vec<BigDigit>
183195
}
@@ -1729,7 +1741,8 @@ fn get_radix_base(radix: u32) -> (DoubleBigDigit, usize) {
17291741
}
17301742

17311743
/// A Sign is a `BigInt`'s composing element.
1732-
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, RustcEncodable, RustcDecodable, Hash)]
1744+
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)]
1745+
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
17331746
pub enum Sign { Minus, NoSign, Plus }
17341747

17351748
impl Neg for Sign {
@@ -1760,7 +1773,8 @@ impl Mul<Sign> for Sign {
17601773
}
17611774

17621775
/// A big signed integer type.
1763-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)]
1776+
#[derive(Clone, Debug, Hash)]
1777+
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
17641778
pub struct BigInt {
17651779
sign: Sign,
17661780
data: BigUint
@@ -2387,6 +2401,7 @@ pub trait RandBigInt {
23872401
fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt;
23882402
}
23892403

2404+
#[cfg(any(feature = "rand", test))]
23902405
impl<R: Rng> RandBigInt for R {
23912406
fn gen_biguint(&mut self, bit_size: usize) -> BigUint {
23922407
let (digits, rem) = bit_size.div_rem(&big_digit::BITS);

src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959

6060
#[cfg(feature = "rustc-serialize")]
6161
extern crate rustc_serialize;
62-
#[cfg(feature = "rand")]
62+
63+
// Some of the tests of non-RNG-based functionality are randomized using the
64+
// RNG-based functionality, so the RNG-based functionality needs to be enabled
65+
// for tests.
66+
#[cfg(any(feature = "rand", all(feature = "bigint", test)))]
6367
extern crate rand;
6468

6569
#[cfg(feature = "bigint")]

0 commit comments

Comments
 (0)