pub struct DtoaTmp { /* private fields */ }Expand description
A short-lived version of Dtoa
This version doesn’t save the formatting
computation, and is meant for cases where the
lifetime of the formatted output &str is very brief.
This version has less overhead, but the string must be formatted everytime you need it.
See crate::dtoa!() for a quick 1-line format macro.
assert_eq!(DtoaTmp::new().format(1.0), "1.0");You could keep a DtoaTmp around to use it
as a factory to keep formatting new strings,
as it will reuse the inner buffer:
let mut dtoa = DtoaTmp::new();
assert_eq!(dtoa.format(1.0), "1.0");
assert_eq!(dtoa.format(2.0), "2.0");
assert_eq!(dtoa.format(3.0), "3.0");Size
assert_eq!(std::mem::size_of::<readable::DtoaTmp>(), 25);Implementations§
source§impl DtoaTmp
impl DtoaTmp
sourcepub fn format<N: IntoDtoa>(&mut self, num: N) -> &str
pub fn format<N: IntoDtoa>(&mut self, num: N) -> &str
Format an IntoDtoa into a &str with an existing DtoaTmp
This function will properly format non-finite floats.
See DtoaTmp::format_finite() if you know your float is finite
(not f32::NAN, f32::INFINITY, f32::NEG_INFINITY).
// We can cheaply reuse this.
let mut dtoa = DtoaTmp::new();
assert_eq!(dtoa.format(18.425), "18.425");
assert_eq!(dtoa.format(19.0918), "19.0918");
assert_eq!(dtoa.format(1.0), "1.0");
assert_eq!(dtoa.format(f32::NAN), "NaN");
assert_eq!(dtoa.format(f32::INFINITY), "inf");
assert_eq!(dtoa.format(f32::NEG_INFINITY), "-inf");sourcepub fn format_finite<N: IntoDtoa>(&mut self, num: N) -> &str
pub fn format_finite<N: IntoDtoa>(&mut self, num: N) -> &str
Format an IntoDtoa into a &str with an existing DtoaTmp
This function will not properly format non-finite floats.
See DtoaTmp::format() for non-finite float formatting
(not f32::NAN, f32::INFINITY, f32::NEG_INFINITY).
// We can cheaply reuse this.
let mut dtoa = DtoaTmp::new();
assert_eq!(dtoa.format_finite(18.425), "18.425");
assert_eq!(dtoa.format_finite(19.0918), "19.0918");
// This is safe, but the output strings will be incorrect.
assert_eq!(dtoa.format_finite(f64::NAN), "2.696539702293474e308");
assert_eq!(dtoa.format_finite(f64::INFINITY), "1.797693134862316e308");
assert_eq!(dtoa.format_finite(f64::NEG_INFINITY), "-1.797693134862316e308");