diff --git a/Cargo.toml b/Cargo.toml index d901a40..5015f62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,12 @@ documentation = "https://docs.rs/fixed" repository = "https://gitlab.com/tspiteri/fixed" readme = "README.md" keywords = ["mathematics", "numerics"] -categories = ["algorithms", "data-structures", "science"] +categories = ["algorithms", "data-structures", "no-std", "science"] license = "MIT/Apache-2.0" [dependencies] typenum = "1.3" + +[features] +default = ["std"] +std = [] diff --git a/README.md b/README.md index 797df90..a635849 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ fixed-point numbers. ## What’s new +### Version 0.1.2 news (unreleased) + + * The crate can now be used without the standard library `std`. + ### Version 0.1.1 news (2018-08-11) * Comparisons are now supported between all fixed-point numbers with @@ -103,6 +107,24 @@ You also need to declare it by adding this to your crate root (usually extern crate fixed; ``` +## Optional features + +The *fixed* crate has one optional features: + + 1. `std`, enabled by default. This adds a dependency on the standard + library `std`. Currently this makes no difference functionally, + but that may change in the future. + +The `std` feature is enabled by default; to use the crate without a +dependency on the standard library `std`, you can add the dependency +like this to [*Cargo.toml*]: + +```toml +[dependencies.fixed] +version = "0.1.1" +default-features = false +``` + ## License This crate is free software: you can redistribute it and/or modify it diff --git a/RELEASES.md b/RELEASES.md index fae15b9..5a6dba1 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -5,6 +5,11 @@ modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty. --> +Version 0.1.2 (unreleased) +========================== + + * The crate can now be used without the standard library `std`. + Version 0.1.1 (2018-08-11) ========================== diff --git a/src/arith.rs b/src/arith.rs index 2668432..4c55b72 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -13,14 +13,14 @@ // and // . -use frac::Unsigned; -use std::cmp::Ordering; -use std::iter::{Product, Sum}; -use std::mem; -use std::ops::{ +use core::cmp::Ordering; +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 frac::Unsigned; use { FixedHelper, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedU8, diff --git a/src/cmp.rs b/src/cmp.rs index 779553e..f408741 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -13,8 +13,8 @@ // and // . +use core::cmp::Ordering; use frac::{self, Unsigned}; -use std::cmp::Ordering; use { FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedU8, @@ -135,7 +135,7 @@ mod tests { #[test] fn cmp_signed() { - use std::cmp::Ordering::*; + use core::cmp::Ordering::*; let neg1_16 = FixedI32::::from_int(-1).unwrap(); let neg1_20 = FixedI32::::from_int(-1).unwrap(); let mut a = neg1_16; @@ -172,7 +172,7 @@ mod tests { #[test] fn cmp_unsigned() { - use std::cmp::Ordering::*; + use core::cmp::Ordering::*; let one_16 = FixedU32::::from_int(1).unwrap(); let one_20 = FixedU32::::from_int(1).unwrap(); let mut a = one_16; diff --git a/src/display.rs b/src/display.rs index 95ef1b2..6e23e31 100644 --- a/src/display.rs +++ b/src/display.rs @@ -13,9 +13,11 @@ // and // . -use std::cmp::Ordering; -use std::fmt::{Binary, Debug, Display, Formatter, LowerHex, Octal, Result as FmtResult, UpperHex}; -use std::str; +use core::cmp::Ordering; +use core::fmt::{ + Binary, Debug, Display, Formatter, LowerHex, Octal, Result as FmtResult, UpperHex, +}; +use core::str; use typenum::Unsigned; use FixedHelper; @@ -158,6 +160,7 @@ fn dec_int_digits(int_bits: u32) -> u32 { let digits = (int_bits * 3 + i) / 10; // check that digits is ceil(log10(2^int_bits - 1)), except when int_bits < 2 + #[cfg(feature = "std")] debug_assert!( int_bits < 2 || digits == (f64::from(int_bits).exp2() - 1.0).log10().ceil() as u32 ); @@ -179,8 +182,10 @@ fn dec_frac_digits(frac_bits: u32) -> u32 { // check that error < delta, where // error = 0.5 * 10^-digits // delta = 2^-frac_bits + #[cfg(feature = "std")] debug_assert!(0.5 * 10f64.powi(0 - digits as i32) < (-f64::from(frac_bits)).exp2()); // check that error with one less digit >= delta + #[cfg(feature = "std")] debug_assert!(0.5 * 10f64.powi(1 - digits as i32) >= (-f64::from(frac_bits)).exp2()); digits @@ -272,7 +277,7 @@ where fmt.pad_integral(!is_neg, "", buf) } -#[cfg(test)] +#[cfg(all(test, feature = "std"))] mod tests { use *; @@ -304,7 +309,6 @@ mod tests { let bits = !0u32 ^ i; let flt = bits as f64 / (frac as f64).exp2(); let fix = FixedU32::::from_bits(bits); - println!("i is {}", i); assert_eq!(format!("{}", fix), format!("{:.2}", flt)); } } diff --git a/src/helper.rs b/src/helper.rs index 5039e44..6face58 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -13,9 +13,8 @@ // and // . -use std::cmp::Ordering; -use std::mem; - +use core::cmp::Ordering; +use core::mem; use typenum::Unsigned; use { FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, diff --git a/src/lib.rs b/src/lib.rs index 03805b8..e3a51af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,24 @@ You also need to declare it by adding this to your crate root (usually extern crate fixed; ``` +## Optional features + +The *fixed* crate has one optional features: + + 1. `std`, enabled by default. This adds a dependency on the standard + library `std`. Currently this makes no difference functionally, + but that may change in the future. + +The `std` feature is enabled by default; to use the crate without a +dependency on the standard library `std`, you can add the dependency +like this to [*Cargo.toml*]: + +```toml +[dependencies.fixed] +version = "0.1.1" +default-features = false +``` + ## License This crate is free software: you can redistribute it and/or modify it @@ -110,11 +128,15 @@ additional terms or conditions. [channels]: https://doc.rust-lang.org/book/second-edition/appendix-07-nightly-rust.html [const generics]: https://github.com/rust-lang/rust/issues/44580 */ +#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] #![doc(html_root_url = "https://docs.rs/fixed/0.1.1")] #![doc(test(attr(deny(warnings))))] #![cfg_attr(nightly_repr_transparent, feature(repr_transparent))] +#[cfg(feature = "std")] +extern crate core; + extern crate typenum; macro_rules! if_signed { @@ -139,13 +161,13 @@ pub mod frac; mod helper; use arith::MulDivDir; +use core::cmp::Ordering; +use core::f32; +use core::f64; +use core::hash::{Hash, Hasher}; +use core::marker::PhantomData; use frac::Unsigned; use helper::FixedHelper; -use std::cmp::Ordering; -use std::f32; -use std::f64; -use std::hash::{Hash, Hasher}; -use std::marker::PhantomData; macro_rules! pass_method { ($comment:expr, $Fixed:ident($Inner:ty) => fn $method:ident()) => {