#no-std #interface #convert #textual #representation #string #value

no-std to-str

Efficient interface to convert value to it's textual representation

1 stable release

1.0.0 May 10, 2020

#277 in Value formatting

BSL-1.0 license

18KB
318 lines

to-str

Crates.io Documentation

Generic trait for efficient conversion to string. Suitable for no_std

Performance

It is up to ToStr implementation, but in general default impls are efficient alternative to Rust's core fmt

Integers

Inspired by C++ fmt and itoa

Performance in general is close to itoa, but lags a bit behind due to some missing optimization opportunities (possibly due to slices in ToStr).

Differently from itoa though the implementation avoids generics per integer type, but rather uses common functions for u8, u64 and u128 types


lib.rs:

no_std friendly interface for conversion to str

type Buffer = to_str::Buffer64;

let mut buf = String::new();
let _ = buf.push_str(Buffer::fmt(5usize).as_str());
assert_eq!(buf, "5");

buf.push_str(Buffer::fmt(0usize).as_str());
assert_eq!(buf, "50");
buf.push_str(Buffer::fmt(&5usize).as_str());
assert_eq!(buf, "505");
buf.push_str(Buffer::fmt(&mut 0usize).as_str());
assert_eq!(buf, "5050");

No runtime deps