Skip to content

Commit c821b26

Browse files
committed
Introduce the std feature
So far, it only toggles whether to build with `#![no_std]`.
1 parent 7f769dd commit c821b26

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ version = "0.1.35"
1212
readme = "README.md"
1313

1414
[dependencies.num-traits]
15-
version = "0.1.32"
15+
version = "0.2.0"
16+
default-features = false
17+
18+
[features]
19+
default = ["std"]
20+
std = ["num-traits/std"]

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ and this to your crate root:
2222
extern crate num_integer;
2323
```
2424

25+
## Features
26+
27+
This crate can be used without the standard library (`#![no_std]`) by disabling
28+
the default `std` feature. Use this in `Cargo.toml`:
29+
30+
```toml
31+
[dependencies.num-integer]
32+
version = "0.1.36"
33+
default-features = false
34+
```
35+
36+
There is no functional difference with and without `std` at this time, but
37+
there may be in the future.
38+
2539
## Compatibility
2640

2741
The `num-integer` crate is tested for rustc 1.8 and greater.

ci/test_full.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ echo Testing num-integer on rustc ${TRAVIS_RUST_VERSION}
88
cargo build --verbose
99
cargo test --verbose
1010

11-
# We have no features to test...
11+
# test `no_std`
12+
cargo build --verbose --no-default-features
13+
cargo test --verbose --no-default-features

src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
1717
#![doc(html_root_url = "https://docs.rs/num-integer/0.1")]
1818

19+
#![cfg_attr(not(feature = "std"), no_std)]
20+
#[cfg(feature = "std")]
21+
extern crate core;
22+
1923
extern crate num_traits as traits;
2024

21-
use std::ops::Add;
25+
use core::ops::Add;
26+
use core::mem;
2227

2328
use traits::{Num, Signed};
2429

@@ -271,7 +276,7 @@ macro_rules! impl_integer_for_isize {
271276

272277
while m != 0 {
273278
m >>= m.trailing_zeros();
274-
if n > m { ::std::mem::swap(&mut n, &mut m) }
279+
if n > m { mem::swap(&mut n, &mut m) }
275280
m -= n;
276281
}
277282

@@ -316,6 +321,7 @@ macro_rules! impl_integer_for_isize {
316321
#[cfg(test)]
317322
mod $test_mod {
318323
use Integer;
324+
use core::mem;
319325

320326
/// Checks that the division rule holds for:
321327
///
@@ -393,7 +399,7 @@ macro_rules! impl_integer_for_isize {
393399
fn test_gcd_cmp_with_euclidean() {
394400
fn euclidean_gcd(mut m: $T, mut n: $T) -> $T {
395401
while m != 0 {
396-
::std::mem::swap(&mut m, &mut n);
402+
mem::swap(&mut m, &mut n);
397403
m %= n;
398404
}
399405

@@ -530,7 +536,7 @@ macro_rules! impl_integer_for_usize {
530536

531537
while m != 0 {
532538
m >>= m.trailing_zeros();
533-
if n > m { ::std::mem::swap(&mut n, &mut m) }
539+
if n > m { mem::swap(&mut n, &mut m) }
534540
m -= n;
535541
}
536542

@@ -577,6 +583,7 @@ macro_rules! impl_integer_for_usize {
577583
#[cfg(test)]
578584
mod $test_mod {
579585
use Integer;
586+
use core::mem;
580587

581588
#[test]
582589
fn test_div_mod_floor() {
@@ -604,7 +611,7 @@ macro_rules! impl_integer_for_usize {
604611
fn test_gcd_cmp_with_euclidean() {
605612
fn euclidean_gcd(mut m: $T, mut n: $T) -> $T {
606613
while m != 0 {
607-
::std::mem::swap(&mut m, &mut n);
614+
mem::swap(&mut m, &mut n);
608615
m %= n;
609616
}
610617
n
@@ -821,9 +828,10 @@ fn test_iter_binomial() {
821828
macro_rules! check_simple {
822829
($t:ty) => { {
823830
let n: $t = 3;
824-
let c: Vec<_> = IterBinomial::new(n).collect();
825-
let expected = vec![1, 3, 3, 1];
826-
assert_eq!(c, expected);
831+
let expected = [1, 3, 3, 1];
832+
for (b, &e) in IterBinomial::new(n).zip(&expected) {
833+
assert_eq!(b, e);
834+
}
827835
} }
828836
}
829837

@@ -839,9 +847,8 @@ fn test_iter_binomial() {
839847
macro_rules! check_binomial {
840848
($t:ty, $n:expr) => { {
841849
let n: $t = $n;
842-
let c: Vec<_> = IterBinomial::new(n).collect();
843850
let mut k: $t = 0;
844-
for b in c {
851+
for b in IterBinomial::new(n) {
845852
assert_eq!(b, binomial(n, k));
846853
k += 1;
847854
}

0 commit comments

Comments
 (0)