Ver Fonte

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.
root há 9 anos atrás
pai
commit
4c123a9e71
6 ficheiros alterados com 50 adições e 9 exclusões
  1. 4 0
      .travis.yml
  2. 9 2
      Cargo.toml
  3. 0 2
      src/bigint.rs
  4. 2 1
      src/complex.rs
  5. 16 1
      src/lib.rs
  6. 19 3
      src/rational.rs

+ 4 - 0
.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 &&

+ 9 - 2
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"

+ 0 - 2
src/bigint.rs

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

+ 2 - 1
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<T> {
     /// Real portion of the complex number
     pub re: T,

+ 16 - 1
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`.

+ 19 - 3
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<T> {
     numer: T,
@@ -35,6 +37,7 @@ pub type Rational = Ratio<isize>;
 pub type Rational32 = Ratio<i32>;
 pub type Rational64 = Ratio<i64>;
 
+#[cfg(feature = "bigint")]
 /// Alias for arbitrary precision rationals.
 pub type BigRational = Ratio<BigInt>;
 
@@ -197,6 +200,7 @@ impl<T: Clone + Integer + PartialOrd + PrimInt> Ratio<T> {
     }
 }
 
+#[cfg(feature = "bigint")]
 impl Ratio<BigInt> {
     /// Converts a float into a rational number.
     pub fn from_float<T: Float>(f: T) -> Option<BigRational> {
@@ -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<T: Float>(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};