bilge_impl/
lib.rs

1use proc_macro::TokenStream;
2use proc_macro_error2::proc_macro_error;
3
4mod bitsize;
5mod bitsize_internal;
6mod debug_bits;
7mod default_bits;
8mod fmt_bits;
9mod from_bits;
10#[cfg(feature = "serde")]
11#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
12mod serde_bits;
13mod try_from_bits;
14
15mod shared;
16
17/// Defines the bitsize of a struct or an enum.
18///
19/// e.g. `#[bitsize(4)]` represents the item as a u4, which is UInt<u8, 4> underneath.
20/// The size of structs is currently limited to 128 bits.
21/// The size of enums is limited to 64 bits.
22/// Please open an issue if you have a usecase for bigger bitfields.
23#[proc_macro_error]
24#[proc_macro_attribute]
25pub fn bitsize(args: TokenStream, item: TokenStream) -> TokenStream {
26    bitsize::bitsize(args.into(), item.into()).into()
27}
28
29/// This is internally used, not to be used by anything besides `bitsize`.
30/// No guarantees are given.
31#[proc_macro_error]
32#[proc_macro_attribute]
33pub fn bitsize_internal(args: TokenStream, item: TokenStream) -> TokenStream {
34    bitsize_internal::bitsize_internal(args.into(), item.into()).into()
35}
36
37/// Generate an `impl TryFrom<uN>` for unfilled bitfields.
38///
39/// This should be used when your enum or enums nested in
40/// a struct don't fill their given `bitsize`.
41#[proc_macro_error]
42#[proc_macro_derive(TryFromBits, attributes(bitsize_internal, fallback))]
43pub fn derive_try_from_bits(item: TokenStream) -> TokenStream {
44    try_from_bits::try_from_bits(item.into()).into()
45}
46
47/// Generate an `impl From<uN>` for filled bitfields.
48///
49/// This should be used when your enum or enums nested in
50/// a struct fill their given `bitsize` or if you're not
51/// using enums.
52#[proc_macro_error]
53#[proc_macro_derive(FromBits, attributes(bitsize_internal, fallback))]
54pub fn derive_from_bits(item: TokenStream) -> TokenStream {
55    from_bits::from_bits(item.into()).into()
56}
57
58/// Generate an `impl core::fmt::Debug` for bitfield structs.
59///
60/// Please use normal #[derive(Debug)] for enums.
61#[proc_macro_error]
62#[proc_macro_derive(DebugBits, attributes(bitsize_internal))]
63pub fn debug_bits(item: TokenStream) -> TokenStream {
64    debug_bits::debug_bits(item.into()).into()
65}
66
67/// Generate an `impl core::fmt::Binary` for bitfields.
68#[proc_macro_error]
69#[proc_macro_derive(BinaryBits)]
70pub fn derive_binary_bits(item: TokenStream) -> TokenStream {
71    fmt_bits::binary(item.into()).into()
72}
73
74/// Generate an `impl core::default::Default` for bitfield structs.
75#[proc_macro_error]
76#[proc_macro_derive(DefaultBits)]
77pub fn derive_default_bits(item: TokenStream) -> TokenStream {
78    default_bits::default_bits(item.into()).into()
79}
80
81/// Generate an `impl serde::Serialize` for bitfield structs.
82///
83/// Please use normal #[derive(Serialize)] for enums.
84#[cfg(feature = "serde")]
85#[proc_macro_error]
86#[proc_macro_derive(SerializeBits, attributes(bitsize_internal))]
87pub fn serialize_bits(item: TokenStream) -> TokenStream {
88    serde_bits::serialize_bits(item.into()).into()
89}
90
91/// Generate an `impl serde::Deserialize` for bitfield structs.
92///
93/// Please use normal #[derive(Deserialize)] for enums.
94#[cfg(feature = "serde")]
95#[proc_macro_error]
96#[proc_macro_derive(DeserializeBits, attributes(bitsize_internal))]
97pub fn deserialize_bits(item: TokenStream) -> TokenStream {
98    serde_bits::deserialize_bits(item.into()).into()
99}