1#![no_std]
2
3extern crate alloc;
4
5use alloc::{string::String, vec::Vec};
6use core::num::{
7 NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU16, NonZeroU32,
8 NonZeroU64, NonZeroU8, NonZeroUsize,
9};
10
11use impl_for::impl_for_each;
12
13pub trait IntoBytes {
15 fn into_bytes(self) -> Vec<u8>;
16}
17
18#[impl_for_each(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize)]
19impl IntoBytes for T {
20 fn into_bytes(self) -> Vec<u8> {
21 let mut buf = ::itoa::Buffer::new();
22 let s = buf.format(self);
23 s.as_bytes().to_vec()
24 }
25}
26
27#[impl_for_each(
28 NonZeroU8,
29 NonZeroI8,
30 NonZeroU16,
31 NonZeroI16,
32 NonZeroU32,
33 NonZeroI32,
34 NonZeroU64,
35 NonZeroI64,
36 NonZeroUsize,
37 NonZeroIsize
38)]
39impl IntoBytes for T {
40 fn into_bytes(self) -> Vec<u8> {
41 let mut buf = ::itoa::Buffer::new();
42 let s = buf.format(self.get());
43 s.as_bytes().to_vec()
44 }
45}
46
47#[impl_for_each(f32, f64)]
48impl IntoBytes for T {
49 fn into_bytes(self) -> Vec<u8> {
50 let mut buf = ::ryu::Buffer::new();
51 let s = buf.format(self);
52 s.as_bytes().to_vec()
53 }
54}
55
56impl IntoBytes for bool {
57 fn into_bytes(self) -> Vec<u8> {
58 if self {
59 Vec::from([1])
60 } else {
61 Vec::from([0])
62 }
63 }
64}
65
66impl IntoBytes for String {
67 fn into_bytes(self) -> Vec<u8> {
68 self.into_bytes()
69 }
70}
71
72impl IntoBytes for &str {
73 fn into_bytes(self) -> Vec<u8> {
74 self.as_bytes().to_vec()
75 }
76}
77
78impl IntoBytes for Vec<u8> {
79 fn into_bytes(self) -> Vec<u8> {
80 self
81 }
82}
83
84impl IntoBytes for &[u8] {
85 fn into_bytes(self) -> Vec<u8> {
86 self.to_vec()
87 }
88}