1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use core::{mem, slice, str};
use pretty;
#[cfg(feature = "no-panic")]
use no_panic::no_panic;
#[derive(Copy, Clone)]
pub struct Buffer {
bytes: [u8; 24],
}
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 {}