@@ -30,19 +30,18 @@ pub trait ToBitMask: Sealed {
3030/// Converts masks to and from byte array bitmasks.
3131///
3232/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
33- #[ cfg( feature = "generic_const_exprs" ) ]
3433pub trait ToBitMaskArray : Sealed {
35- /// The length of the bitmask array.
36- const BYTES : usize ;
34+ /// The bitmask array.
35+ type BitMaskArray ;
3736
3837 /// Converts a mask to a bitmask.
39- fn to_bitmask_array ( self ) -> [ u8 ; Self :: BYTES ] ;
38+ fn to_bitmask_array ( self ) -> Self :: BitMaskArray ;
4039
4140 /// Converts a bitmask to a mask.
42- fn from_bitmask_array ( bitmask : [ u8 ; Self :: BYTES ] ) -> Self ;
41+ fn from_bitmask_array ( bitmask : Self :: BitMaskArray ) -> Self ;
4342}
4443
45- macro_rules! impl_integer_intrinsic {
44+ macro_rules! impl_integer {
4645 { $( impl ToBitMask <BitMask =$int: ty> for Mask <_, $lanes: literal>) * } => {
4746 $(
4847 impl <T : MaskElement > ToBitMask for Mask <T , $lanes> {
@@ -62,7 +61,27 @@ macro_rules! impl_integer_intrinsic {
6261 }
6362}
6463
65- impl_integer_intrinsic ! {
64+ macro_rules! impl_array {
65+ { $( impl ToBitMaskArray <Bytes =$int: literal> for Mask <_, $lanes: literal>) * } => {
66+ $(
67+ impl <T : MaskElement > ToBitMaskArray for Mask <T , $lanes> {
68+ type BitMaskArray = [ u8 ; $int] ;
69+
70+ #[ inline]
71+ fn to_bitmask_array( self ) -> Self :: BitMaskArray {
72+ self . 0 . to_bitmask_array( )
73+ }
74+
75+ #[ inline]
76+ fn from_bitmask_array( bitmask: Self :: BitMaskArray ) -> Self {
77+ Self ( mask_impl:: Mask :: from_bitmask_array( bitmask) )
78+ }
79+ }
80+ ) *
81+ }
82+ }
83+
84+ impl_integer ! {
6685 impl ToBitMask <BitMask =u8 > for Mask <_, 1 >
6786 impl ToBitMask <BitMask =u8 > for Mask <_, 2 >
6887 impl ToBitMask <BitMask =u8 > for Mask <_, 4 >
@@ -72,27 +91,12 @@ impl_integer_intrinsic! {
7291 impl ToBitMask <BitMask =u64 > for Mask <_, 64 >
7392}
7493
75- /// Returns the minimum number of bytes in a bitmask with `lanes` lanes.
76- #[ cfg( feature = "generic_const_exprs" ) ]
77- #[ allow( clippy:: missing_inline_in_public_items) ]
78- pub const fn bitmask_len ( lanes : usize ) -> usize {
79- ( lanes + 7 ) / 8
80- }
81-
82- #[ cfg( feature = "generic_const_exprs" ) ]
83- impl < T : MaskElement , const LANES : usize > ToBitMaskArray for Mask < T , LANES >
84- where
85- LaneCount < LANES > : SupportedLaneCount ,
86- {
87- const BYTES : usize = bitmask_len ( LANES ) ;
88-
89- #[ inline]
90- fn to_bitmask_array ( self ) -> [ u8 ; Self :: BYTES ] {
91- self . 0 . to_bitmask_array ( )
92- }
93-
94- #[ inline]
95- fn from_bitmask_array ( bitmask : [ u8 ; Self :: BYTES ] ) -> Self {
96- Mask ( mask_impl:: Mask :: from_bitmask_array ( bitmask) )
97- }
94+ impl_array ! {
95+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 1 >
96+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 2 >
97+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 4 >
98+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 8 >
99+ impl ToBitMaskArray <Bytes =2 > for Mask <_, 16 >
100+ impl ToBitMaskArray <Bytes =4 > for Mask <_, 32 >
101+ impl ToBitMaskArray <Bytes =8 > for Mask <_, 64 >
98102}
0 commit comments