Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the alloc crate for no_std (Rust 1.36+) #101

Merged
merged 3 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ rust:
- 1.22.0 # rand
- 1.26.0 # has_i128
- 1.31.0 # 2018!
- 1.36.0 # alloc
- stable
- beta
- nightly
Expand All @@ -19,6 +20,14 @@ matrix:
apt:
packages:
- gcc-multilib
# try a target that doesn't have std at all, but does have alloc
- name: "no_std"
rust: stable
env: TARGET=thumbv6m-none-eabi
before_script:
- rustup target add $TARGET
script:
- cargo build --verbose --target $TARGET --no-default-features --features "i128 serde"
- name: "rustfmt"
rust: 1.31.0
before_script:
Expand Down
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ features = ["std"]
optional = true
version = "1.0"
default-features = false
features = ["std"]

[dependencies.quickcheck]
optional = true
Expand All @@ -64,9 +63,6 @@ optional = true
version = "0.8"
default-features = false

[dev-dependencies.serde_test]
version = "1.0"

[features]
default = ["std"]
i128 = ["num-integer/i128", "num-traits/i128"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ extern crate num_bigint;

## Features

The `std` crate feature is mandatory and enabled by default. If you depend on
`num-bigint` with `default-features = false`, you must manually enable the
`std` feature yourself. In the future, we hope to support `#![no_std]` with
the `alloc` crate when `std` is not enabled.
The `std` crate feature is enabled by default, and is mandatory before Rust
1.36 and the stabilized `alloc` crate. If you depend on `num-bigint` with
`default-features = false`, you must manually enable the `std` feature yourself
if your compiler is not new enough.

Implementations for `i128` and `u128` are only available with Rust 1.26 and
later. The build script automatically detects this, but you can make it
Expand Down
12 changes: 12 additions & 0 deletions ci/big_serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "big_serde"
version = "0.1.0"
authors = ["Josh Stone <cuviper@gmail.com>"]

[dependencies]
num-traits = "0.2.11"
serde_test = "1.0"

[dependencies.num-bigint]
features = ["serde"]
path = "../.."
6 changes: 5 additions & 1 deletion tests/serde.rs → ci/big_serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
//! The serialized formats should not change, even if we change our
//! internal representation, because we want to preserve forward and
//! backward compatibility of serialized data!
//!
//! This test is in a completely separate crate so its `serde_test`
//! dependency does not "infect" the rest of the build with `serde`'s
//! default features, especially not `serde/std`.

#![cfg(feature = "serde")]
#![cfg(test)]

extern crate num_bigint;
extern crate num_traits;
Expand Down
2 changes: 1 addition & 1 deletion ci/rustup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
set -ex

export TRAVIS_RUST_VERSION
for TRAVIS_RUST_VERSION in 1.15.0 1.22.0 1.26.0 stable beta nightly; do
for TRAVIS_RUST_VERSION in 1.15.0 1.22.0 1.26.0 1.31.0 1.36.0 stable beta nightly; do
run="rustup run $TRAVIS_RUST_VERSION"
$run cargo build --verbose
$run $PWD/ci/test_full.sh
Expand Down
47 changes: 34 additions & 13 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ set -ex

echo Testing num-bigint on rustc ${TRAVIS_RUST_VERSION}

FEATURES="serde"
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable.*|1.31.0|1.26.0|1.22.0)$ ]]; then
FEATURES="$FEATURES rand"
fi
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable.*|1.31.0|1.26.0)$ ]]; then
FEATURES="$FEATURES i128"
fi
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable.*|1.31.0)$ ]]; then
FEATURES="$FEATURES quickcheck quickcheck_macros"
fi
case "$TRAVIS_RUST_VERSION" in
1.1[5-9].* | 1.2[0-1].*) STD_FEATURES="serde" ;;
1.2[2-5].*) STD_FEATURES="serde rand" ;;
1.2[6-9].* | 1.30.*) STD_FEATURES="serde rand i128" ;;
*) STD_FEATURES="serde rand i128 quickcheck quickcheck_macros" ;;
esac

case "$TRAVIS_RUST_VERSION" in
1.1[5-9].* | 1.2[0-9].* | 1.3[0-5].*) ;;
*) NO_STD_FEATURES="i128 serde" ;;
esac

# num-bigint should build and test everywhere.
cargo build --verbose
Expand All @@ -24,16 +25,36 @@ cargo build --no-default-features --features="std"
cargo test --no-default-features --features="std"

# Each isolated feature should also work everywhere.
for feature in $FEATURES; do
for feature in $STD_FEATURES; do
cargo build --verbose --no-default-features --features="std $feature"
cargo test --verbose --no-default-features --features="std $feature"
done

# test all supported features together
cargo build --features="std $FEATURES"
cargo test --features="std $FEATURES"
cargo build --features="std $STD_FEATURES"
cargo test --features="std $STD_FEATURES"

if test -n "${NO_STD_FEATURES:+true}"; then
# It should build with minimal features too.
cargo build --no-default-features
cargo test --no-default-features

# Each isolated feature should also work everywhere.
for feature in $NO_STD_FEATURES; do
cargo build --verbose --no-default-features --features="$feature"
cargo test --verbose --no-default-features --features="$feature"
done

# test all supported features together
cargo build --no-default-features --features="$NO_STD_FEATURES"
cargo test --no-default-features --features="$NO_STD_FEATURES"
fi

# make sure benchmarks can be built
if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
cargo bench --all-features --no-run
fi

case "$STD_FEATURES" in
*serde*) cargo test --manifest-path ci/big_serde/Cargo.toml
esac
10 changes: 5 additions & 5 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use std::cmp;
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::iter::repeat;
use std::mem;
use core::cmp;
use core::cmp::Ordering::{self, Equal, Greater, Less};
use core::iter::repeat;
use core::mem;
use std_alloc::{Cow, Vec};
use traits;
use traits::{One, Zero};

Expand Down
26 changes: 15 additions & 11 deletions src/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#[allow(deprecated, unused_imports)]
use std::ascii::AsciiExt;
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::default::Default;
use std::fmt;
use std::iter::{Product, Sum};
use std::mem;
use std::ops::{
use core::cmp::Ordering::{self, Equal, Greater, Less};
use core::default::Default;
use core::fmt;
use core::iter::{Product, Sum};
use core::mem;
use core::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use std::str::{self, FromStr};
use core::str::{self, FromStr};
#[cfg(has_i128)]
use std::{i128, u128};
use std::{i64, u64};
use core::{i128, u128};
use core::{i64, u64};
#[cfg(feature = "std")]
#[allow(deprecated, unused_imports)]
use std::ascii::AsciiExt;
#[cfg(feature = "quickcheck")]
use std_alloc::Box;
use std_alloc::{String, Vec};

#[cfg(feature = "serde")]
use serde;
Expand Down
2 changes: 1 addition & 1 deletion src/bigrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<R: Rng + ?Sized> RandBigInt for R {

#[cfg(u64_digit)]
fn gen_biguint(&mut self, bit_size: usize) -> BigUint {
use std::slice;
use core::slice;

let (digits, rem) = bit_size.div_rem(&32);
let native_digits = bit_size.div_ceil(&64);
Expand Down
Loading