proc_bitfield/
lib.rs

1#![doc = include_str!("../docs.md")]
2#![cfg_attr(not(test), no_std)]
3#![cfg_attr(feature = "nightly", feature(const_trait_impl))]
4#![cfg_attr(all(doc, feature = "nightly"), feature(doc_cfg))]
5#![cfg_attr(all(any(doc, test), feature = "nightly"), feature(trivial_bounds))]
6#![warn(clippy::all)]
7#![cfg_attr(
8    all(any(doc, test), feature = "gce"),
9    feature(generic_const_exprs),
10    expect(incomplete_features)
11)]
12
13#[cfg(not(feature = "nightly"))]
14macro_rules! const_trait {
15    ($trait: item) => {
16        $trait
17    };
18}
19
20#[cfg(feature = "nightly")]
21macro_rules! const_trait {
22    (
23        $(#[$attr:meta])*
24        $vis:vis trait $($tokens:tt)*
25    ) => {
26        $(#[$attr])*
27        $vis const trait $($tokens)*
28    };
29}
30
31#[doc(hidden)]
32pub mod __private {
33    pub use static_assertions;
34
35    #[inline(always)]
36    pub const fn min(a: usize, b: usize) -> usize {
37        if a < b {
38            a
39        } else {
40            b
41        }
42    }
43
44    #[inline(always)]
45    pub const fn max(a: usize, b: usize) -> usize {
46        if a > b {
47            a
48        } else {
49            b
50        }
51    }
52
53    #[cfg(feature = "gce")]
54    const_trait! {
55        pub trait NestedBitfield<'a, S> {
56            fn __from_storage(storage: &'a S) -> Self;
57        }
58    }
59
60    #[cfg(feature = "gce")]
61    const_trait! {
62        pub trait NestedMutBitfield<'a, S> {
63            fn __from_storage(storage: &'a mut S) -> Self;
64        }
65    }
66
67    #[cfg(feature = "gce")]
68    const_trait! {
69        pub trait NestedWriteBitfield<'a, S> {
70            fn __from_storage(storage: &'a mut S) -> Self;
71        }
72    }
73}
74
75/// The main focus of the crate. Defines a bitfield struct.
76#[doc = include_str!("../usage_examples/bitfield.md")]
77pub use macros::bitfield;
78
79/// Reads a single field from an anonymous bitfield, without creating a bitfield struct.
80#[doc = include_str!("../usage_examples/bits.md")]
81pub use macros::bits;
82
83/// Returns an anonymous bitfield with a single field modified, without creating a bitfield struct.
84#[doc = include_str!("../usage_examples/with_bits.md")]
85pub use macros::with_bits;
86
87/// Modifies a single field in an anonymous bitfield, without creating a bitfield struct.
88#[doc = include_str!("../usage_examples/set_bits.md")]
89pub use macros::set_bits;
90
91/// A derive macro to implement any applicable conversion traits between an enum and the builtin
92/// integer and boolean types corresponding to variant discriminants.
93#[doc = include_str!("../usage_examples/conv_raw.md")]
94pub use macros::ConvRaw;
95
96#[cfg(feature = "nightly")]
97#[cfg_attr(all(doc, feature = "nightly"), doc(cfg(feature = "nightly")))]
98/// A derive macro to implement `Bits<T> for U` and the related traits for a type `T` and all
99/// integer bitfield storage types `U`, by unwrapping the conversion results.
100#[doc = include_str!("../usage_examples/unwrap_bits.md")]
101pub use macros::UnwrapBits;
102
103mod conv;
104pub use conv::*;
105mod traits;
106pub use traits::*;
107
108#[cfg(any(test, doc))]
109extern crate self as proc_bitfield;
110
111#[cfg(doc)]
112/// Sample bitfields to showcase the crate's features
113pub mod example;