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

Support no_std #14

Merged
merged 4 commits into from
Mar 18, 2018
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ readme = "README.md"
exclude = ["performance.png"]

[features]
default = ["std"]
i128 = []
std = []
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ itoa
[![Latest Version](https://img.shields.io/crates/v/itoa.svg)](https://crates.io/crates/itoa)

This crate provides fast functions for printing integer primitives to an
[`io::Write`](https://doc.rust-lang.org/std/io/trait.Write.html). The
[`io::Write`](https://doc.rust-lang.org/std/io/trait.Write.html) or a
[`fmt::Write`](https://doc.rust-lang.org/core/fmt/trait.Write.html). The
implementation comes straight from
[libcore](https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L201-L254)
but avoids the performance penalty of going through
Expand All @@ -32,19 +33,29 @@ println!("{:?}", buf);
let mut bytes = [b'\0'; 20];
let n = itoa::write(&mut bytes[..], 128u64)?;
println!("{:?}", &bytes[..n]);

// write to a String
let mut s = String::new();
itoa::fmt(&mut s, 128u64)?;
println!("{}", s);
```

The function signature is:
The function signatures are:

```rust
fn write<W: io::Write, V: itoa::Integer>(writer: W, value: V) -> io::Result<usize>

fn fmt<W: fmt::Write, V: itoa::Integer>(writer: W, value: V) -> fmt::Result
```

where `itoa::Integer` is implemented for `i8`, `u8`, `i16`, `u16`, `i32`, `u32`,
`i64`, `u64`, `i128`, `u128`, `isize` and `usize`. 128-bit integer support is
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
Expand Down
52 changes: 40 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,42 @@

#![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))]

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

use std::{io, mem, ptr, slice};
#[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<W: io::Write, V: Integer>(wr: W, value: V) -> io::Result<usize> {
value.write(wr)
}

pub trait Integer {
#[inline]
pub fn fmt<W: fmt::Write, V: Integer>(wr: W, value: V) -> fmt::Result {
value.fmt(wr)
}

// Seal to prevent downstream implementations of the Integer trait.
mod private {
pub trait Sealed {}
}

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

fn fmt<W: fmt::Write>(self, W) -> fmt::Result;
}

trait IntegerPrivate {
Expand All @@ -41,17 +61,32 @@ const MAX_LEN: usize = 40; // i128::MIN (including minus sign)

// Adaptation of the original implementation at
// https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L188-L266
macro_rules! impl_Integer {
($($t:ident),* as $conv_fn:ident) => {$(
macro_rules! impl_IntegerCommon {
($t:ident) => {
impl Integer for $t {
#[cfg(feature = "std")]
fn write<W: io::Write>(self, mut wr: W) -> io::Result<usize> {
let mut buf = unsafe { mem::uninitialized() };
let bytes = self.write_to(&mut buf);
try!(wr.write_all(bytes));
Ok(bytes.len())
}

fn fmt<W: fmt::Write>(self, mut wr: W) -> fmt::Result {
let mut buf = unsafe { mem::uninitialized() };
let bytes = self.write_to(&mut buf);
wr.write_str(unsafe { str::from_utf8_unchecked(bytes) })
}
}

impl private::Sealed for $t {}
};
}

macro_rules! impl_Integer {
($($t:ident),* as $conv_fn:ident) => {$(
impl_IntegerCommon!($t);

impl IntegerPrivate for $t {
#[allow(unused_comparisons)]
fn write_to(self, buf: &mut [u8; MAX_LEN]) -> &[u8] {
Expand Down Expand Up @@ -128,14 +163,7 @@ impl_Integer!(isize, usize as u64);
#[cfg(all(feature = "i128"))]
macro_rules! impl_Integer128 {
($($t:ident),*) => {$(
impl Integer for $t {
fn write<W: io::Write>(self, mut wr: W) -> io::Result<usize> {
let mut buf = unsafe { mem::uninitialized() };
let bytes = self.write_to(&mut buf);
try!(wr.write_all(bytes));
Ok(bytes.len())
}
}
impl_IntegerCommon!($t);

impl IntegerPrivate for $t {
#[allow(unused_comparisons)]
Expand Down
13 changes: 10 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ 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();
assert_eq!(s, $expected);
}
)*
}
Expand Down