Make rustc-serialize, bignum, rational, complex into opt-out features

Making bignum optional allows skipping the rustc-serialize and rand
dependencies too, and it makes a big difference in num's build time.

With default (all) features, clean build time including dependencies: 27
seconds.

With no default features, clean build time including dependencies (none):
5 seconds.
This commit is contained in:
root 2015-06-02 14:34:22 +02:00
parent aeebff3578
commit 4c123a9e71
6 changed files with 50 additions and 9 deletions

View File

@ -7,6 +7,10 @@ sudo: false
script: script:
- cargo build --verbose - cargo build --verbose
- cargo test --verbose - cargo test --verbose
- |
(for feature in '' bigint rational complex; do
cargo test --verbose --no-default-features --features="$feature" || exit 1
done)
- | - |
[ $TRAVIS_RUST_VERSION != nightly ] || ( [ $TRAVIS_RUST_VERSION != nightly ] || (
cargo bench && cargo bench &&

View File

@ -14,8 +14,15 @@ rational, and complex types.
""" """
[dependencies] [dependencies]
rustc-serialize = "0.3.13" rustc-serialize = { version = "0.3.13", optional = true }
rand = "0.3.8" rand = { version = "0.3.8", optional = true }
[features]
complex = []
rational = []
bigint = ["rustc-serialize", "rand"]
default = ["complex", "rational", "bigint"]
[[bench]] [[bench]]
name = "shootout-pidigits" name = "shootout-pidigits"

View File

@ -58,8 +58,6 @@
//! # } //! # }
//! ``` //! ```
extern crate rustc_serialize;
use Integer; use Integer;
use std::default::Default; use std::default::Default;

View File

@ -20,7 +20,8 @@ use {Zero, One, Num, Float};
// probably doesn't map to C's _Complex correctly. // probably doesn't map to C's _Complex correctly.
/// A complex number in Cartesian form. /// A complex number in Cartesian form.
#[derive(PartialEq, Copy, Clone, Hash, RustcEncodable, RustcDecodable, Debug)] #[derive(PartialEq, Copy, Clone, Hash, Debug)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
pub struct Complex<T> { pub struct Complex<T> {
/// Real portion of the complex number /// Real portion of the complex number
pub re: T, pub re: T,

View File

@ -19,6 +19,8 @@
//! //!
//! ``` //! ```
//! extern crate num; //! extern crate num;
//! # #[cfg(all(feature = "bigint", feature="rational"))]
//! # pub mod test {
//! //!
//! use num::FromPrimitive; //! use num::FromPrimitive;
//! use num::bigint::BigInt; //! use num::bigint::BigInt;
@ -35,10 +37,14 @@
//! //!
//! approx //! approx
//! } //! }
//! # }
//! # #[cfg(not(all(feature = "bigint", feature="rational")))]
//! # fn approx_sqrt(n: u64, _: usize) -> u64 { n }
//! //!
//! fn main() { //! fn main() {
//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 //! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
//! } //! }
//!
//! ``` //! ```
//! //!
//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
@ -47,11 +53,18 @@
html_root_url = "http://doc.rust-lang.org/num/", html_root_url = "http://doc.rust-lang.org/num/",
html_playground_url = "http://play.rust-lang.org/")] html_playground_url = "http://play.rust-lang.org/")]
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize; extern crate rustc_serialize;
#[cfg(feature = "rand")]
extern crate rand; extern crate rand;
#[cfg(feature = "bigint")]
pub use bigint::{BigInt, BigUint}; pub use bigint::{BigInt, BigUint};
pub use rational::{Rational, BigRational}; #[cfg(feature = "rational")]
pub use rational::Rational;
#[cfg(all(feature = "rational", feature="bigint"))]
pub use rational::BigRational;
#[cfg(feature = "complex")]
pub use complex::Complex; pub use complex::Complex;
pub use integer::Integer; pub use integer::Integer;
pub use iter::{range, range_inclusive, range_step, range_step_inclusive}; pub use iter::{range, range_inclusive, range_step, range_step_inclusive};
@ -63,11 +76,13 @@ pub use traits::{Num, Zero, One, Signed, Unsigned, Bounded,
use std::ops::{Mul}; use std::ops::{Mul};
#[cfg(feature = "bigint")]
pub mod bigint; pub mod bigint;
pub mod complex; pub mod complex;
pub mod integer; pub mod integer;
pub mod iter; pub mod iter;
pub mod traits; pub mod traits;
#[cfg(feature = "rational")]
pub mod rational; pub mod rational;
/// Returns the additive identity, `0`. /// Returns the additive identity, `0`.

View File

@ -18,12 +18,14 @@ use std::fmt;
use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::str::FromStr; use std::str::FromStr;
use traits::{FromPrimitive, Float, PrimInt}; #[cfg(feature = "bigint")]
use bigint::{BigInt, BigUint, Sign}; use bigint::{BigInt, BigUint, Sign};
use traits::{FromPrimitive, Float, PrimInt};
use {Num, Signed, Zero, One}; use {Num, Signed, Zero, One};
/// Represents the ratio between 2 numbers. /// Represents the ratio between 2 numbers.
#[derive(Copy, Clone, Hash, RustcEncodable, RustcDecodable, Debug)] #[derive(Copy, Clone, Hash, Debug)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub struct Ratio<T> { pub struct Ratio<T> {
numer: T, numer: T,
@ -35,6 +37,7 @@ pub type Rational = Ratio<isize>;
pub type Rational32 = Ratio<i32>; pub type Rational32 = Ratio<i32>;
pub type Rational64 = Ratio<i64>; pub type Rational64 = Ratio<i64>;
#[cfg(feature = "bigint")]
/// Alias for arbitrary precision rationals. /// Alias for arbitrary precision rationals.
pub type BigRational = Ratio<BigInt>; pub type BigRational = Ratio<BigInt>;
@ -197,6 +200,7 @@ impl<T: Clone + Integer + PartialOrd + PrimInt> Ratio<T> {
} }
} }
#[cfg(feature = "bigint")]
impl Ratio<BigInt> { impl Ratio<BigInt> {
/// Converts a float into a rational number. /// Converts a float into a rational number.
pub fn from_float<T: Float>(f: T) -> Option<BigRational> { pub fn from_float<T: Float>(f: T) -> Option<BigRational> {
@ -493,7 +497,9 @@ impl Error for ParseRatioError {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{Ratio, Rational, BigRational}; use super::{Ratio, Rational};
#[cfg(feature = "bigint")]
use super::BigRational;
use std::str::FromStr; use std::str::FromStr;
use std::i32; use std::i32;
use {Zero, One, Signed, FromPrimitive, Float}; use {Zero, One, Signed, FromPrimitive, Float};
@ -509,12 +515,20 @@ mod test {
pub const _2_3: Rational = Ratio { numer: 2, denom: 3}; pub const _2_3: Rational = Ratio { numer: 2, denom: 3};
pub const _NEG2_3: Rational = Ratio { numer: -2, denom: 3}; pub const _NEG2_3: Rational = Ratio { numer: -2, denom: 3};
#[cfg(feature = "bigint")]
pub fn to_big(n: Rational) -> BigRational { pub fn to_big(n: Rational) -> BigRational {
Ratio::new( Ratio::new(
FromPrimitive::from_isize(n.numer).unwrap(), FromPrimitive::from_isize(n.numer).unwrap(),
FromPrimitive::from_isize(n.denom).unwrap() FromPrimitive::from_isize(n.denom).unwrap()
) )
} }
#[cfg(not(feature = "bigint"))]
pub fn to_big(n: Rational) -> Rational {
Ratio::new(
FromPrimitive::from_isize(n.numer).unwrap(),
FromPrimitive::from_isize(n.denom).unwrap()
)
}
#[test] #[test]
fn test_test_constants() { fn test_test_constants() {
@ -809,6 +823,7 @@ mod test {
} }
} }
#[cfg(feature = "bigint")]
#[test] #[test]
fn test_from_float() { fn test_from_float() {
fn test<T: Float>(given: T, (numer, denom): (&str, &str)) { fn test<T: Float>(given: T, (numer, denom): (&str, &str)) {
@ -835,6 +850,7 @@ mod test {
test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376")); test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376"));
} }
#[cfg(feature = "bigint")]
#[test] #[test]
fn test_from_float_fail() { fn test_from_float_fail() {
use std::{f32, f64}; use std::{f32, f64};