Skip to content

Commit 70883ab

Browse files
committed
Support no_std
Fixes #3.
1 parent 9f3e867 commit 70883ab

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ matrix:
1313
script:
1414
- cargo build --verbose --features "$FEATURES"
1515
- cargo test --verbose --features "$FEATURES"
16+
- cargo build --verbose --no-default-features --features "$FEATURES"
17+
- cargo test --verbose --no-default-features --features "$FEATURES"
1618
- if [ "$BUILD_BENCH" == "true" ]; then cargo bench --verbose --no-run --features "$FEATURES"; fi
1719

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ readme = "README.md"
1111
exclude = ["performance.png"]
1212

1313
[features]
14+
default = ["std"]
1415
i128 = []
16+
std = []

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ where `itoa::Integer` is implemented for `i8`, `u8`, `i16`, `u16`, `i32`, `u32`,
5353
only available with the nightly compiler when the `i128` feature is enabled for
5454
this crate. The return value gives the number of bytes written.
5555

56+
The `write` function is only available when the `std` feature is enabled
57+
(default is enabled).
58+
5659
## Dependency
5760

5861
Itoa is available on [crates.io](https://crates.io/crates/itoa). Use the

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88

99
#![doc(html_root_url = "https://docs.rs/itoa/0.3.4")]
1010

11+
#![cfg_attr(not(feature = "std"), no_std)]
12+
1113
#![cfg_attr(feature = "i128", feature(i128_type, i128))]
1214

1315
#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, unreadable_literal))]
1416

1517
#[cfg(feature = "i128")]
1618
mod udiv128;
1719

20+
#[cfg(feature = "std")]
1821
use std::{fmt, io, mem, ptr, slice, str};
1922

23+
#[cfg(not(feature = "std"))]
24+
use core::{fmt, mem, ptr, slice, str};
25+
26+
#[cfg(feature = "std")]
2027
#[inline]
2128
pub fn write<W: io::Write, V: Integer>(wr: W, value: V) -> io::Result<usize> {
2229
value.write(wr)
@@ -33,6 +40,7 @@ mod private {
3340
}
3441

3542
pub trait Integer: private::Sealed {
43+
#[cfg(feature = "std")]
3644
fn write<W: io::Write>(self, W) -> io::Result<usize>;
3745

3846
fn fmt<W: fmt::Write>(self, W) -> fmt::Result;
@@ -56,6 +64,7 @@ const MAX_LEN: usize = 40; // i128::MIN (including minus sign)
5664
macro_rules! impl_IntegerCommon {
5765
($t:ident) => {
5866
impl Integer for $t {
67+
#[cfg(feature = "std")]
5968
fn write<W: io::Write>(self, mut wr: W) -> io::Result<usize> {
6069
let mut buf = unsafe { mem::uninitialized() };
6170
let bytes = self.write_to(&mut buf);

tests/test.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ macro_rules! test {
1717
$(#[$attr])*
1818
#[test]
1919
fn $name() {
20-
let mut buf = [b'\0'; 40];
21-
let len = itoa::write(&mut buf[..], $value).unwrap();
22-
assert_eq!(&buf[0..len], $expected.as_bytes());
20+
#[cfg(feature = "std")]
21+
{
22+
let mut buf = [b'\0'; 40];
23+
let len = itoa::write(&mut buf[..], $value).unwrap();
24+
assert_eq!(&buf[0..len], $expected.as_bytes());
25+
}
2326

2427
let mut s = String::new();
2528
itoa::fmt(&mut s, $value).unwrap();

0 commit comments

Comments
 (0)