use core::{mem, slice, str};
use pretty;
#[cfg(feature = "no-panic")]
use no_panic::no_panic;
#[derive(Copy, Clone)]
pub struct Buffer {
bytes: [u8; 25],
}
impl Buffer {
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn new() -> Self {
Buffer { bytes: unsafe { mem::uninitialized() } }
}
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn format<F: Float>(&mut self, f: F) -> &str {
unsafe {
let n = f.write_to_ryu_buffer(&mut self.bytes[0]);
debug_assert!(n <= self.bytes.len());
let slice = slice::from_raw_parts(&self.bytes[0], n);
str::from_utf8_unchecked(slice)
}
}
}
impl Default for Buffer {
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
fn default() -> Self {
Buffer::new()
}
}
pub trait Float: Sealed {
#[doc(hidden)]
unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize;
}
impl Float for f32 {
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize {
pretty::f2s_buffered_n(self, result)
}
}
impl Float for f64 {
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize {
pretty::d2s_buffered_n(self, result)
}
}
pub trait Sealed {}
impl Sealed for f32 {}
impl Sealed for f64 {}