1
1
use super :: { mask_impl, Mask , MaskElement } ;
2
2
use crate :: simd:: { LaneCount , SupportedLaneCount } ;
3
+ use core:: borrow:: { Borrow , BorrowMut } ;
3
4
4
5
mod sealed {
5
6
pub trait Sealed { }
@@ -30,19 +31,26 @@ pub trait ToBitMask: Sealed {
30
31
/// Converts masks to and from byte array bitmasks.
31
32
///
32
33
/// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
33
- #[ cfg( feature = "generic_const_exprs" ) ]
34
34
pub 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 ;
37
45
38
46
/// Converts a mask to a bitmask.
39
- fn to_bitmask_array ( self ) -> [ u8 ; Self :: BYTES ] ;
47
+ fn to_bitmask_array ( self ) -> Self :: BitMaskArray ;
40
48
41
49
/// 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 ;
43
51
}
44
52
45
- macro_rules! impl_integer_intrinsic {
53
+ macro_rules! impl_integer {
46
54
{ $( impl ToBitMask <BitMask =$int: ty> for Mask <_, $lanes: literal>) * } => {
47
55
$(
48
56
impl <T : MaskElement > ToBitMask for Mask <T , $lanes> {
@@ -62,7 +70,27 @@ macro_rules! impl_integer_intrinsic {
62
70
}
63
71
}
64
72
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 ! {
66
94
impl ToBitMask <BitMask =u8 > for Mask <_, 1 >
67
95
impl ToBitMask <BitMask =u8 > for Mask <_, 2 >
68
96
impl ToBitMask <BitMask =u8 > for Mask <_, 4 >
@@ -72,27 +100,12 @@ impl_integer_intrinsic! {
72
100
impl ToBitMask <BitMask =u64 > for Mask <_, 64 >
73
101
}
74
102
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 >
98
111
}
0 commit comments