11use super :: { mask_impl, Mask , MaskElement } ;
22use crate :: simd:: { LaneCount , SupportedLaneCount } ;
3+ use core:: borrow:: { Borrow , BorrowMut } ;
34
45mod sealed {
56 pub trait Sealed { }
@@ -30,19 +31,26 @@ pub trait ToBitMask: Sealed {
3031/// Converts masks to and from byte array bitmasks.
3132///
3233/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
33- #[ cfg( feature = "generic_const_exprs" ) ]
3434pub trait ToBitMaskArray : Sealed {
35- /// The length of the bitmask array.
36- const BYTES : usize ;
35+ /// The bitmask array.
36+ type BitMaskArray : Copy
37+ + Unpin
38+ + Send
39+ + Sync
40+ + AsRef < [ u8 ] >
41+ + AsMut < [ u8 ] >
42+ + Borrow < [ u8 ] >
43+ + BorrowMut < [ u8 ] >
44+ + ' static ;
3745
3846 /// Converts a mask to a bitmask.
39- fn to_bitmask_array ( self ) -> [ u8 ; Self :: BYTES ] ;
47+ fn to_bitmask_array ( self ) -> Self :: BitMaskArray ;
4048
4149 /// Converts a bitmask to a mask.
42- fn from_bitmask_array ( bitmask : [ u8 ; Self :: BYTES ] ) -> Self ;
50+ fn from_bitmask_array ( bitmask : Self :: BitMaskArray ) -> Self ;
4351}
4452
45- macro_rules! impl_integer_intrinsic {
53+ macro_rules! impl_integer {
4654 { $( impl ToBitMask <BitMask =$int: ty> for Mask <_, $lanes: literal>) * } => {
4755 $(
4856 impl <T : MaskElement > ToBitMask for Mask <T , $lanes> {
@@ -62,7 +70,27 @@ macro_rules! impl_integer_intrinsic {
6270 }
6371}
6472
65- impl_integer_intrinsic ! {
73+ macro_rules! impl_array {
74+ { $( impl ToBitMaskArray <Bytes =$int: literal> for Mask <_, $lanes: literal>) * } => {
75+ $(
76+ impl <T : MaskElement > ToBitMaskArray for Mask <T , $lanes> {
77+ type BitMaskArray = [ u8 ; $int] ;
78+
79+ #[ inline]
80+ fn to_bitmask_array( self ) -> Self :: BitMaskArray {
81+ self . 0 . to_bitmask_array( )
82+ }
83+
84+ #[ inline]
85+ fn from_bitmask_array( bitmask: Self :: BitMaskArray ) -> Self {
86+ Self ( mask_impl:: Mask :: from_bitmask_array( bitmask) )
87+ }
88+ }
89+ ) *
90+ }
91+ }
92+
93+ impl_integer ! {
6694 impl ToBitMask <BitMask =u8 > for Mask <_, 1 >
6795 impl ToBitMask <BitMask =u8 > for Mask <_, 2 >
6896 impl ToBitMask <BitMask =u8 > for Mask <_, 4 >
@@ -72,27 +100,12 @@ impl_integer_intrinsic! {
72100 impl ToBitMask <BitMask =u64 > for Mask <_, 64 >
73101}
74102
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- }
103+ impl_array ! {
104+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 1 >
105+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 2 >
106+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 4 >
107+ impl ToBitMaskArray <Bytes =1 > for Mask <_, 8 >
108+ impl ToBitMaskArray <Bytes =2 > for Mask <_, 16 >
109+ impl ToBitMaskArray <Bytes =4 > for Mask <_, 32 >
110+ impl ToBitMaskArray <Bytes =8 > for Mask <_, 64 >
98111}
0 commit comments