PSArc_lib/traits/
as_bytes.rs

1/// **ConvertAsBytes** convert different types of data into a `Vec<u8>`, made just for convinence <br>
2/// It is auto implemented (if imported) for the following types: <br>
3/// `Vec<u8>`, `&[u8]`, `&str`, `String`, `[u8; N]` (1 thru 16), and `u8`
4pub trait ConvertAsBytes {
5    /// **convert_as_bytes** converts a previous type to a `Vec<u8>`
6    fn convert_as_bytes(&self) -> Vec<u8>;
7}
8
9impl ConvertAsBytes for Vec<u8> {
10    fn convert_as_bytes(&self) -> Vec<u8> {
11        return self.to_owned();
12    }
13}
14
15impl ConvertAsBytes for &[u8] {
16    fn convert_as_bytes(&self) -> Vec<u8> {
17        return self.to_vec();
18    }
19}
20
21impl ConvertAsBytes for &str {
22    fn convert_as_bytes(&self) -> Vec<u8> {
23        return self.as_bytes().to_vec();
24    }
25}
26
27impl ConvertAsBytes for String {
28    fn convert_as_bytes(&self) -> Vec<u8> {
29        return self.as_bytes().to_vec();
30    }
31}
32
33impl ConvertAsBytes for u8 {
34    fn convert_as_bytes(&self) -> Vec<u8> {
35        return vec![self.to_owned()];
36    }
37}
38
39seq_macro::seq!(N in 1..=16 {
40    impl ConvertAsBytes for [u8; N] {
41		fn convert_as_bytes(&self) -> Vec<u8> {
42			return self.to_vec();
43		}
44	}
45});