Skip to main content

bytes_lit/
lib.rs

1//! Bytes converts literals into an array of bytes.
2//!
3//! Currently supports only integer literals of unbounded size.
4//!
5//! The macros are intended for small byte inputs. There is no arbitrary limit,
6//! but very large inputs may result in long compilation times. For large amounts
7//! of bytes, store the bytes in a file and use [`include_bytes!`] instead.
8
9mod bytes;
10mod bytesmin;
11
12use proc_macro::TokenStream;
13
14extern crate proc_macro;
15
16/// Bytes converts literals into an array of bytes.
17///
18/// Currently supports only integer literals of unbounded size.
19///
20/// Supported literal forms:
21/// - Base 16 (hex), e.g. `0xff`
22/// - Base 2 (binary), e.g. `0b11111111`
23///
24/// Leading zeros are preserved. Zeros on the front of the number are preserved
25/// as zeros in the final bytes. For example: `0x0001` will produce `[0, 1]`.
26///
27/// Decimal and octal literal forms are not supported.
28///
29/// This macro is intended for small byte inputs. There is no arbitrary limit,
30/// but very large inputs may result in long compilation times. For large
31/// amounts of bytes, store the bytes in a file and use [`include_bytes!`]
32/// instead.
33///
34/// ### Examples
35///
36/// ```
37/// let bytes = bytes_lit::bytes!(0x1);
38/// assert_eq!(bytes, [1]);
39/// ```
40///
41/// ```
42/// let bytes = bytes_lit::bytes!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
43/// assert_eq!(bytes, [
44///     253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
45///     250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
46/// ]);
47/// ```
48///
49/// ```
50/// let bytes = bytes_lit::bytes!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
51/// assert_eq!(bytes, [
52///     0, 0, 0, 0, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
53///     250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
54/// ]);
55/// ```
56#[proc_macro]
57pub fn bytes(input: TokenStream) -> TokenStream {
58    bytes::bytes(input.into()).into()
59}
60
61/// Bytesmin converts literals into an array of bytes of minimal size to capture
62/// the value.
63///
64/// Currently supports only integer literals of unbounded size.
65///
66/// Supported literal forms:
67/// - Base 16 (hex), e.g. `0xff`
68/// - Base 2 (binary), e.g. `0b11111111`
69///
70/// Leading zeroes on integer literals are discarded and not preserved. The
71/// generated byte slice is the minimal bytes required to capture the literal
72/// provided.
73///
74/// Decimal and octal literal forms are not supported.
75///
76/// To preserve leading zeros, use [`bytes!`].
77///
78/// This macro is intended for small byte inputs. There is no arbitrary limit,
79/// but very large inputs may result in long compilation times. For large
80/// amounts of bytes, store the bytes in a file and use [`include_bytes!`]
81/// instead.
82///
83/// ### Examples
84///
85/// ```
86/// let bytes = bytes_lit::bytesmin!(0x1);
87/// assert_eq!(bytes, [1]);
88/// ```
89///
90/// ```
91/// let bytes = bytes_lit::bytesmin!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
92/// assert_eq!(bytes, [
93///     253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
94///     250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
95/// ]);
96/// ```
97///
98/// ```
99/// let bytes = bytes_lit::bytesmin!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
100/// assert_eq!(bytes, [
101///     222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114, 250, 111, 250,
102///     174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
103/// ]);
104/// ```
105#[proc_macro]
106pub fn bytesmin(input: TokenStream) -> TokenStream {
107    bytesmin::bytesmin(input.into()).into()
108}