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
98
pub trait IntoBytes {
fn into_bytes(self) -> Vec<u8>;
}
macro_rules! itoa_into_bytes {
($t:ty) => {
impl IntoBytes for $t {
fn into_bytes(self) -> Vec<u8> {
let mut buf = ::itoa::Buffer::new();
let s = buf.format(self);
s.as_bytes().to_vec()
}
}
};
}
macro_rules! non_zero_itoa_into_bytes {
($t:ty) => {
impl IntoBytes for $t {
fn into_bytes(self) -> Vec<u8> {
let mut buf = ::itoa::Buffer::new();
let s = buf.format(self.get());
s.as_bytes().to_vec()
}
}
};
}
macro_rules! ryu_into_bytes {
($t:ty) => {
impl IntoBytes for $t {
fn into_bytes(self) -> Vec<u8> {
let mut buf = ::ryu::Buffer::new();
let s = buf.format(self);
s.as_bytes().to_vec()
}
}
};
}
itoa_into_bytes!(i8);
itoa_into_bytes!(u8);
itoa_into_bytes!(i16);
itoa_into_bytes!(u16);
itoa_into_bytes!(i32);
itoa_into_bytes!(u32);
itoa_into_bytes!(i64);
itoa_into_bytes!(u64);
itoa_into_bytes!(isize);
itoa_into_bytes!(usize);
non_zero_itoa_into_bytes!(core::num::NonZeroU8);
non_zero_itoa_into_bytes!(core::num::NonZeroI8);
non_zero_itoa_into_bytes!(core::num::NonZeroU16);
non_zero_itoa_into_bytes!(core::num::NonZeroI16);
non_zero_itoa_into_bytes!(core::num::NonZeroU32);
non_zero_itoa_into_bytes!(core::num::NonZeroI32);
non_zero_itoa_into_bytes!(core::num::NonZeroU64);
non_zero_itoa_into_bytes!(core::num::NonZeroI64);
non_zero_itoa_into_bytes!(core::num::NonZeroUsize);
non_zero_itoa_into_bytes!(core::num::NonZeroIsize);
ryu_into_bytes!(f32);
ryu_into_bytes!(f64);
impl IntoBytes for bool {
fn into_bytes(self) -> Vec<u8> {
if self {
vec![1]
} else {
vec![0]
}
}
}
impl IntoBytes for String {
fn into_bytes(self) -> Vec<u8> {
self.into_bytes()
}
}
impl<'a> IntoBytes for &'a str {
fn into_bytes(self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
impl IntoBytes for Vec<u8> {
fn into_bytes(self) -> Vec<u8> {
self
}
}
impl<'a> IntoBytes for &'a [u8] {
fn into_bytes(self) -> Vec<u8> {
self.to_vec()
}
}