Skip to content

Commit

Permalink
Support no_std
Browse files Browse the repository at this point in the history
Fixes #3.
  • Loading branch information
glandium committed Mar 17, 2018
1 parent 5babc6c commit 9d9ac83
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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 = ["use_std"]
i128 = []
use_std = []
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `use_std` feature is enabled
(default is enabled).

## Dependency

Itoa is available on [crates.io](https://crates.io/crates/itoa). Use the
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@

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

#![cfg_attr(not(feature = "use_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;

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

#[cfg(not(feature = "use_std"))]
use core::{fmt, mem, ptr, slice, str};

#[cfg(feature = "use_std")]
#[inline]
pub fn write<W: io::Write, V: Integer>(wr: W, value: V) -> io::Result<usize> {
value.write(wr)
Expand All @@ -28,6 +35,7 @@ pub fn fmt<W: fmt::Write, V: Integer>(wr: W, value: V) -> fmt::Result {
}

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

fn fmt<W: fmt::Write>(self, W) -> fmt::Result;
Expand All @@ -51,6 +59,7 @@ const MAX_LEN: usize = 40; // i128::MIN (including minus sign)
macro_rules! impl_IntegerCommon {
($t:ident) => {
impl Integer for $t {
#[cfg(feature = "use_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);
Expand Down
9 changes: 6 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "use_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();
Expand Down

0 comments on commit 9d9ac83

Please sign in to comment.