From 4c123a9e71b2446d079e91c5dc742852d5fa578e Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 Jun 2015 14:34:22 +0200 Subject: [PATCH] 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. --- .travis.yml | 4 ++++ Cargo.toml | 11 +++++++++-- src/bigint.rs | 2 -- src/complex.rs | 3 ++- src/lib.rs | 17 ++++++++++++++++- src/rational.rs | 22 +++++++++++++++++++--- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 887404c..f754c62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,10 @@ sudo: false script: - cargo build --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 ] || ( cargo bench && diff --git a/Cargo.toml b/Cargo.toml index 4e003e3..9860128 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,15 @@ rational, and complex types. """ [dependencies] -rustc-serialize = "0.3.13" -rand = "0.3.8" +rustc-serialize = { version = "0.3.13", optional = true } +rand = { version = "0.3.8", optional = true } + +[features] + +complex = [] +rational = [] +bigint = ["rustc-serialize", "rand"] +default = ["complex", "rational", "bigint"] [[bench]] name = "shootout-pidigits" diff --git a/src/bigint.rs b/src/bigint.rs index 22ed5ac..5c10c35 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -58,8 +58,6 @@ //! # } //! ``` -extern crate rustc_serialize; - use Integer; use std::default::Default; diff --git a/src/complex.rs b/src/complex.rs index a791a8c..ab5e86f 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -20,7 +20,8 @@ use {Zero, One, Num, Float}; // probably doesn't map to C's _Complex correctly. /// 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 { /// Real portion of the complex number pub re: T, diff --git a/src/lib.rs b/src/lib.rs index 23be173..ab8aec7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ //! //! ``` //! extern crate num; +//! # #[cfg(all(feature = "bigint", feature="rational"))] +//! # pub mod test { //! //! use num::FromPrimitive; //! use num::bigint::BigInt; @@ -35,10 +37,14 @@ //! //! approx //! } +//! # } +//! # #[cfg(not(all(feature = "bigint", feature="rational")))] +//! # fn approx_sqrt(n: u64, _: usize) -> u64 { n } //! //! fn main() { //! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 //! } +//! //! ``` //! //! [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_playground_url = "http://play.rust-lang.org/")] +#[cfg(feature = "rustc-serialize")] extern crate rustc_serialize; +#[cfg(feature = "rand")] extern crate rand; +#[cfg(feature = "bigint")] 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 integer::Integer; 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}; +#[cfg(feature = "bigint")] pub mod bigint; pub mod complex; pub mod integer; pub mod iter; pub mod traits; +#[cfg(feature = "rational")] pub mod rational; /// Returns the additive identity, `0`. diff --git a/src/rational.rs b/src/rational.rs index 3f7d2ae..7109ad0 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -18,12 +18,14 @@ use std::fmt; use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; use std::str::FromStr; -use traits::{FromPrimitive, Float, PrimInt}; +#[cfg(feature = "bigint")] use bigint::{BigInt, BigUint, Sign}; +use traits::{FromPrimitive, Float, PrimInt}; use {Num, Signed, Zero, One}; /// 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)] pub struct Ratio { numer: T, @@ -35,6 +37,7 @@ pub type Rational = Ratio; pub type Rational32 = Ratio; pub type Rational64 = Ratio; +#[cfg(feature = "bigint")] /// Alias for arbitrary precision rationals. pub type BigRational = Ratio; @@ -197,6 +200,7 @@ impl Ratio { } } +#[cfg(feature = "bigint")] impl Ratio { /// Converts a float into a rational number. pub fn from_float(f: T) -> Option { @@ -493,7 +497,9 @@ impl Error for ParseRatioError { #[cfg(test)] mod test { - use super::{Ratio, Rational, BigRational}; + use super::{Ratio, Rational}; + #[cfg(feature = "bigint")] + use super::BigRational; use std::str::FromStr; use std::i32; use {Zero, One, Signed, FromPrimitive, Float}; @@ -509,12 +515,20 @@ mod test { pub const _2_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 { Ratio::new( FromPrimitive::from_isize(n.numer).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] fn test_test_constants() { @@ -809,6 +823,7 @@ mod test { } } + #[cfg(feature = "bigint")] #[test] fn test_from_float() { fn test(given: T, (numer, denom): (&str, &str)) { @@ -835,6 +850,7 @@ mod test { test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376")); } + #[cfg(feature = "bigint")] #[test] fn test_from_float_fail() { use std::{f32, f64};