From 3f6071d196a94b3b434095890b6b004815e98833 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 16 Apr 2020 14:07:12 -0400 Subject: [PATCH] Add no_std support and testing --- Cargo.toml | 5 +++++ Jenkinsfile | 10 ++++++++++ src/bigint.rs | 11 +++++++++++ src/lib.rs | 14 +++++++++++++- src/rational.rs | 4 ++-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9c5bcb..2fa4709 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,11 @@ edition = "2018" [dev-dependencies] criterion = "0.3" +[features] +default = ["std"] +alloc = [] +std = ["alloc"] + [[bench]] name = "stock_functions" harness = false diff --git a/Jenkinsfile b/Jenkinsfile index 28d0739..af90c99 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -16,6 +16,16 @@ pipeline { sh "cargo test" } } + stage('Check no_std') { + steps { + sh "cargo check --no-default-features --features alloc" + } + } + stage('Test no_std') { + steps { + sh "cargo test --no-default-features --features alloc" + } + } stage('Coverage') { steps { sh "cargo clean" diff --git a/src/bigint.rs b/src/bigint.rs index 5a457d1..cce0172 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -2,6 +2,17 @@ //! \[WIP\] Arbitrarily large integers use crate::num::Sign::*; use crate::num::*; + +#[cfg(all(feature = "alloc", not(feature = "std")))] +extern crate alloc; +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::vec::*; +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::string::*; + +#[cfg(feature = "std")] +use std::prelude::v1::*; + use core::convert::TryInto; use core::mem::replace; use core::ops::{ diff --git a/src/lib.rs b/src/lib.rs index b281c9b..aaa3ccd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,18 @@ //! //! Playin' with Numerics in Rust #![forbid(unsafe_code)] +#![no_std] -use std::f64::consts::{E, PI}; +#[cfg(all(feature = "alloc", not(feature = "std")))] +#[macro_use] +extern crate alloc; + +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(feature = "std")] +use core::f64::consts::{E, PI}; pub mod bigint; pub mod num; @@ -183,6 +193,7 @@ pub fn factorial(n: usize) -> Option { /// let invalid = approx_factorial(171.0); // None /// # assert!(invalid.is_none()); /// ``` +#[cfg(feature = "std")] #[inline] pub fn approx_factorial(n: f64) -> Option { let power = (n / E).powf(n); @@ -233,6 +244,7 @@ mod tests { assert!(it_factorial(35).is_none()); } + #[cfg(feature = "std")] #[test] fn test_approx_factorial() { assert!(approx_factorial(170.624).is_some()); diff --git a/src/rational.rs b/src/rational.rs index 562ecf1..a64502c 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -2,8 +2,8 @@ use crate::num::Sign::*; use crate::num::*; -use std::cmp::{Ord, Ordering, PartialOrd}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::cmp::{Ord, Ordering, PartialOrd}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; /// Type representing a fraction ///