bytes_lit/lib.rs
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 99 100 101 102 103 104 105 106 107 108 109
//! Bytes converts literals into an array of bytes.
//!
//! Currently supports only integer literals of unbounded size.
mod bytes;
mod bytesmin;
use proc_macro::TokenStream;
extern crate proc_macro;
/// Bytes converts literals into an array of bytes.
///
/// Currently supports only integer literals of unbounded size.
///
/// The following integer literal forms are supported and preserve leading
/// zeros. The final byte representation always returns a consistent number of
/// bytes given the number of digits inputed.
/// - Base 16 (hex)
/// - Base 2 (binary)
///
/// For integer literal forms that preserve leading zeros, zeros on the front of
/// the number are preserved as zeros in the final bytes. For example: `0x0001`
/// will produce `[0, 1]`.
///
/// The following integer literal forms are supported and prohibit leading
/// zeros. The number of bytes returned is not based off the number of digits
/// entered.
/// - Base 10 (decimal)
/// - Base 8 (octal)
///
/// For integer literal forms that do not have consistent digit to byte lengths,
/// the number of bytes returned is the minimum number of bytes required to
/// represent the integer.
///
/// ### Examples
///
/// ```
/// let bytes = bytes_lit::bytes!(1);
/// assert_eq!(bytes, [1]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytes!(9);
/// assert_eq!(bytes, [9]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytes!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
/// assert_eq!(bytes, [
/// 253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
/// ]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytes!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
/// assert_eq!(bytes, [
/// 0, 0, 0, 0, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
/// ]);
/// ```
#[proc_macro]
pub fn bytes(input: TokenStream) -> TokenStream {
bytes::bytes(input.into()).into()
}
/// Bytesmin converts literals into an array of bytes of minimal size to capture
/// the value.
///
/// Currently supports only integer literals of unbounded size.
///
/// Leading zeroes on integer literals are discarded and not preserved. The
/// generated byte slice is the minimal bytes required to capture the literal
/// provided.
///
/// To preserve leading zeros, use [`bytes!`].
///
/// ### Examples
///
/// ```
/// let bytes = bytes_lit::bytesmin!(1);
/// assert_eq!(bytes, [1]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytesmin!(9);
/// assert_eq!(bytes, [9]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytesmin!(0xfded3f55dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
/// assert_eq!(bytes, [
/// 253, 237, 63, 85, 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114,
/// 250, 111, 250, 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
/// ]);
/// ```
///
/// ```
/// let bytes = bytes_lit::bytesmin!(0x00000000dec47250a52a8c0bb7038e72fa6ffaae33562f77cd2b629ef7fd424d);
/// assert_eq!(bytes, [
/// 222, 196, 114, 80, 165, 42, 140, 11, 183, 3, 142, 114, 250, 111, 250,
/// 174, 51, 86, 47, 119, 205, 43, 98, 158, 247, 253, 66, 77,
/// ]);
/// ```
#[proc_macro]
pub fn bytesmin(input: TokenStream) -> TokenStream {
bytesmin::bytesmin(input.into()).into()
}