Skip to main content

bitpiece/impls/
int_types.rs

1use crate::*;
2
3macro_rules! impl_bitpiece_for_unsigned_int_types {
4    { $($bit_len: literal),+ $(,)? } => {
5        $(
6            paste::paste! {
7                pub struct [<BitPieceU $bit_len Converter>];
8                impl [<BitPieceU $bit_len Converter>] {
9                    pub const fn from_fields(fields: [<u $bit_len>]) -> [<u $bit_len>] {
10                        fields
11                    }
12                    pub const fn to_fields(x: [<u $bit_len>]) -> [<u $bit_len>] {
13                        x
14                    }
15                    pub const fn try_from_bits(bits: [<u $bit_len>]) -> Option<[<u $bit_len>]> {
16                        Some(bits)
17                    }
18                    pub const fn from_bits(bits: [<u $bit_len>]) -> [<u $bit_len>] {
19                        bits
20                    }
21                    pub const fn to_bits(x: [<u $bit_len>]) -> [<u $bit_len>] {
22                        x
23                    }
24                    pub const fn const_eq(a: [<u $bit_len>], b: [<u $bit_len>]) -> bool {
25                        a == b
26                    }
27                    pub const fn to_storage_mut_ref(x: &mut [<u $bit_len>]) -> BitPieceStorageMutRef<'_> {
28                        BitPieceStorageMutRef::[<U $bit_len>](x)
29                    }
30                }
31                impl BitPiece for [<u $bit_len>] {
32                    const BITS: usize = $bit_len;
33                    const ZEROES: Self = 0;
34                    const ONES: Self = !0;
35                    const MIN: Self = [<u $bit_len>]::MIN;
36                    const MAX: Self = [<u $bit_len>]::MAX;
37                    type Bits = Self;
38                    type Converter = [<BitPieceU $bit_len Converter>];
39                    fn try_from_bits(bits: Self::Bits) -> Option<Self> {
40                        <Self as BitPiece>::Converter::try_from_bits(bits)
41                    }
42                    fn from_bits(bits: Self::Bits) -> Self {
43                        <Self as BitPiece>::Converter::from_bits(bits)
44                    }
45                    fn to_bits(self) -> Self::Bits {
46                        <Self as BitPiece>::Converter::to_bits(self)
47                    }
48                }
49                impl BitPieceHasMutRef for [<u $bit_len>] {
50                    type MutRef<'s> = [<BitPieceU $bit_len MutRef>]<'s>;
51                }
52                impl BitPieceHasFields for [<u $bit_len>] {
53                    type Fields = Self;
54                    fn from_fields(fields: Self::Fields) -> Self {
55                        <Self as BitPiece>::Converter::from_fields(fields)
56                    }
57                    fn to_fields(self) -> Self::Fields {
58                        <Self as BitPiece>::Converter::to_fields(self)
59                    }
60                }
61                bitpiece_check_full_impl! { [<u $bit_len>], true }
62                bitpiece_define_mut_ref_type! { [<u $bit_len>], [<BitPieceU $bit_len MutRef>], pub }
63            }
64        )+
65    };
66}
67impl_bitpiece_for_unsigned_int_types! { 8, 16, 32, 64 }
68
69macro_rules! impl_bitpiece_for_signed_int_types {
70    { $($bit_len: literal),+ $(,)? } => {
71        $(
72            paste::paste! {
73                pub struct [<BitPieceI $bit_len Converter>];
74                impl [<BitPieceI $bit_len Converter>] {
75                    pub const fn from_fields(fields: [<i $bit_len>]) -> [<i $bit_len>] {
76                        fields
77                    }
78                    pub const fn to_fields(x: [<i $bit_len>]) -> [<i $bit_len>] {
79                        x
80                    }
81                    pub const fn try_from_bits(bits: [<u $bit_len>]) -> Option<[<i $bit_len>]> {
82                        Some(bits as [<i $bit_len>])
83                    }
84                    pub const fn from_bits(bits: [<u $bit_len>]) -> [<i $bit_len>] {
85                        bits as [<i $bit_len>]
86                    }
87                    pub const fn to_bits(x: [<i $bit_len>]) -> [<u $bit_len>] {
88                        x as [<u $bit_len>]
89                    }
90                    pub const fn const_eq(a: [<i $bit_len>], b: [<i $bit_len>]) -> bool {
91                        a == b
92                    }
93                }
94                impl BitPiece for [<i $bit_len>] {
95                    const BITS: usize = $bit_len;
96                    const ZEROES: Self = 0;
97                    const ONES: Self = !0;
98                    const MIN: Self = [<i $bit_len>]::MIN;
99                    const MAX: Self = [<i $bit_len>]::MAX;
100                    type Bits = [<u $bit_len>];
101                    type Converter = [<BitPieceI $bit_len Converter>];
102                    fn try_from_bits(bits: Self::Bits) -> Option<Self> {
103                        <Self as BitPiece>::Converter::try_from_bits(bits)
104                    }
105                    fn from_bits(bits: Self::Bits) -> Self {
106                        <Self as BitPiece>::Converter::from_bits(bits)
107                    }
108                    fn to_bits(self) -> Self::Bits {
109                        <Self as BitPiece>::Converter::to_bits(self)
110                    }
111                }
112                impl BitPieceHasMutRef for [<i $bit_len>] {
113                    type MutRef<'s> = [<BitPieceI $bit_len MutRef>]<'s>;
114                }
115                impl BitPieceHasFields for [<i $bit_len>] {
116                    type Fields = Self;
117                    fn from_fields(fields: Self::Fields) -> Self {
118                        <Self as BitPiece>::Converter::from_fields(fields)
119                    }
120                    fn to_fields(self) -> Self::Fields {
121                        <Self as BitPiece>::Converter::to_fields(self)
122                    }
123                }
124                bitpiece_check_full_impl! { [<i $bit_len>], true }
125                bitpiece_define_mut_ref_type! { [<i $bit_len>], [<BitPieceI $bit_len MutRef>], pub }
126            }
127        )+
128    };
129}
130impl_bitpiece_for_signed_int_types! { 8, 16, 32, 64 }