proc_bitfield/
traits.rs

1mod arr_impls;
2mod int_impls;
3
4/// Trait implemented by all bitfields generated by the crate in order to know their storage type.
5pub trait Bitfield {
6    /// This bitfield's storage type.
7    type Storage;
8}
9
10#[cfg(feature = "gce")]
11const_trait! {
12    /// Marker trait for bitfields that can be used as read-only nested bitfields.
13    #[cfg_attr(all(doc, feature = "nightly"), doc(cfg(feature = "gce")))]
14    pub trait NestableBitfield<S, const START: usize, const END: usize> {
15        type Nested<'a>: const crate::__private::NestedBitfield<'a, S>
16        where
17            S: 'a;
18    }
19}
20
21#[cfg(feature = "gce")]
22const_trait! {
23    /// Marker trait for bitfields that can be used as readable and writable nested bitfields.
24    #[cfg_attr(all(doc, feature = "nightly"), doc(cfg(feature = "gce")))]
25    pub trait NestableMutBitfield<S, const START: usize, const END: usize> {
26        type NestedMut<'a>: const crate::__private::NestedMutBitfield<'a, S>
27        where
28            S: 'a;
29    }
30}
31
32#[cfg(feature = "gce")]
33const_trait! {
34    /// Marker trait for bitfields that can be used as write-only nested bitfields.
35    #[cfg_attr(all(doc, feature = "nightly"), doc(cfg(feature = "gce")))]
36    pub trait NestableWriteBitfield<S, const START: usize, const END: usize> {
37        type NestedWrite<'a>: const crate::__private::NestedWriteBitfield<'a, S>
38        where
39            S: 'a;
40    }
41}
42
43const_trait! {
44    /// Read a range of bits inside a value.
45    pub trait Bits<T> {
46        /// Read `self`'s `START..END` bit range (with `END` excluded) as a value of type `T`.
47        fn bits<const START: usize, const END: usize>(&self) -> T;
48    }
49}
50
51const_trait! {
52    /// Return a value with a range of bits modified.
53    pub trait WithBits<T> {
54        #[must_use]
55        /// Returns `self` with the the `START..END` bit range (with `END` excluded) set to the given
56        /// value of type `T`.
57        fn with_bits<const START: usize, const END: usize>(self, value: T) -> Self;
58    }
59}
60
61const_trait! {
62    /// Modify a range of bits inside a value in place.
63    pub trait SetBits<T> {
64        /// Sets `self`'s `START..END` bit range (with `END` excluded) to the given value of type `T`
65        /// in place.
66        fn set_bits<const START: usize, const END: usize>(&mut self, value: T);
67    }
68}
69
70const_trait! {
71    /// Read a single bit inside a value.
72    pub trait Bit {
73        /// Read `self`'s specified bit.
74        fn bit<const BIT: usize>(&self) -> bool;
75    }
76}
77
78const_trait! {
79    /// Return a value with a single bit modified.
80    pub trait WithBit {
81        /// Returns `self` with the the specified bit set to 1 if `value` is `true`, and 0
82        /// otherwise.
83        #[must_use]
84        fn with_bit<const BIT: usize>(self, value: bool) -> Self;
85    }
86}
87
88const_trait! {
89    /// Modify a single bit inside a value in place.
90    pub trait SetBit {
91        /// Sets `self`'s specified bit to 1 if `value` is `true`, and 0 otherwise.
92        fn set_bit<const BIT: usize>(&mut self, value: bool);
93    }
94}