bytes_lit/lib.rs
1//! Bytes converts literals into an array of bytes.
2//!
3//! Currently supports only integer literals of unbounded size.
4
5mod bytes;
6mod bytesmin;
7
8use proc_macro::TokenStream;
9
10extern crate proc_macro;
11
12/// Bytes converts literals into an array of bytes.
13///
14/// Currently supports only integer literals of unbounded size.
15///
16/// The following integer literal forms are supported and preserve leading
17/// zeros. The final byte representation always returns a consistent number of
18/// bytes given the number of digits inputed.
19/// - Base 16 (hex)
20/// - Base 2 (binary)
21///
22/// For integer literal forms that preserve leading zeros, zeros on the front of
23/// the number are preserved as zeros in the final bytes. For example: `0x0001`
24/// will produce `[0, 1]`.
25///
26/// The following integer literal forms are supported and prohibit leading
27/// zeros. The number of bytes returned is not based off the number of digits
28/// entered.
29/// - Base 10 (decimal)
30/// - Base 8 (octal)
31///
32/// For integer literal forms that do not have consistent digit to byte lengths,
33/// the number of bytes returned is the minimum number of bytes required to
34/// represent the integer.
35///
36/// ### Examples
37///
38/// ```
39/// let bytes = bytes_lit::bytes!(1);
40/// assert_eq!(bytes, [1]);
41/// ```
42///
43/// ```
44/// let bytes = bytes_lit::bytes!(9);
45/// assert_eq!(bytes, [9]);
46/// ```
47///
48/// ```
49/// let bytes = bytes_lit::bytes!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
50/// assert_eq!(bytes, [
51/// 253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
52/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
53/// ]);
54/// ```
55///
56/// ```
57/// let bytes = bytes_lit::bytes!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
58/// assert_eq!(bytes, [
59/// 0, 0, 0, 0, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
60/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
61/// ]);
62/// ```
63#[proc_macro]
64pub fn bytes(input: TokenStream) -> TokenStream {
65 bytes::bytes(input.into()).into()
66}
67
68/// Bytesmin converts literals into an array of bytes of minimal size to capture
69/// the value.
70///
71/// Currently supports only integer literals of unbounded size.
72///
73/// Leading zeroes on integer literals are discarded and not preserved. The
74/// generated byte slice is the minimal bytes required to capture the literal
75/// provided.
76///
77/// To preserve leading zeros, use [`bytes!`].
78///
79/// ### Examples
80///
81/// ```
82/// let bytes = bytes_lit::bytesmin!(1);
83/// assert_eq!(bytes, [1]);
84/// ```
85///
86/// ```
87/// let bytes = bytes_lit::bytesmin!(9);
88/// assert_eq!(bytes, [9]);
89/// ```
90///
91/// ```
92/// let bytes = bytes_lit::bytesmin!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
93/// assert_eq!(bytes, [
94/// 253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
95/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
96/// ]);
97/// ```
98///
99/// ```
100/// let bytes = bytes_lit::bytesmin!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
101/// assert_eq!(bytes, [
102/// 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114, 250, 111, 250,
103/// 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
104/// ]);
105/// ```
106#[proc_macro]
107pub fn bytesmin(input: TokenStream) -> TokenStream {
108 bytesmin::bytesmin(input.into()).into()
109}