diff --git a/.travis.yml b/.travis.yml index 6a5a6db..0748cb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,5 +13,7 @@ matrix: script: - cargo build --verbose --features "$FEATURES" - cargo test --verbose --features "$FEATURES" + - cargo build --verbose --no-default-features --features "$FEATURES" + - cargo test --verbose --no-default-features --features "$FEATURES" - if [ "$BUILD_BENCH" == "true" ]; then cargo bench --verbose --no-run --features "$FEATURES"; fi diff --git a/Cargo.toml b/Cargo.toml index 62b8fe6..3123045 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ readme = "README.md" exclude = ["performance.png"] [features] +default = ["std"] i128 = [] +std = [] diff --git a/README.md b/README.md index 999faba..491c602 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ where `itoa::Integer` is implemented for `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, only available with the nightly compiler when the `i128` feature is enabled for this crate. The return value gives the number of bytes written. +The `write` function is only available when the `std` feature is enabled +(default is enabled). + ## Dependency Itoa is available on [crates.io](https://crates.io/crates/itoa). Use the diff --git a/src/lib.rs b/src/lib.rs index 86bb7c8..11971f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,8 @@ #![doc(html_root_url = "https://docs.rs/itoa/0.3.4")] +#![cfg_attr(not(feature = "std"), no_std)] + #![cfg_attr(feature = "i128", feature(i128_type, i128))] #![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, unreadable_literal))] @@ -15,8 +17,13 @@ #[cfg(feature = "i128")] mod udiv128; +#[cfg(feature = "std")] use std::{fmt, io, mem, ptr, slice, str}; +#[cfg(not(feature = "std"))] +use core::{fmt, mem, ptr, slice, str}; + +#[cfg(feature = "std")] #[inline] pub fn write(wr: W, value: V) -> io::Result { value.write(wr) @@ -33,6 +40,7 @@ mod private { } pub trait Integer: private::Sealed { + #[cfg(feature = "std")] fn write(self, W) -> io::Result; fn fmt(self, W) -> fmt::Result; @@ -56,6 +64,7 @@ const MAX_LEN: usize = 40; // i128::MIN (including minus sign) macro_rules! impl_IntegerCommon { ($t:ident) => { impl Integer for $t { + #[cfg(feature = "std")] fn write(self, mut wr: W) -> io::Result { let mut buf = unsafe { mem::uninitialized() }; let bytes = self.write_to(&mut buf); diff --git a/tests/test.rs b/tests/test.rs index 58b728e..ad7c3d7 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -17,9 +17,12 @@ macro_rules! test { $(#[$attr])* #[test] fn $name() { - let mut buf = [b'\0'; 40]; - let len = itoa::write(&mut buf[..], $value).unwrap(); - assert_eq!(&buf[0..len], $expected.as_bytes()); + #[cfg(feature = "std")] + { + let mut buf = [b'\0'; 40]; + let len = itoa::write(&mut buf[..], $value).unwrap(); + assert_eq!(&buf[0..len], $expected.as_bytes()); + } let mut s = String::new(); itoa::fmt(&mut s, $value).unwrap();