@@ -10,6 +10,10 @@ pub trait NumberOfBits: Copy + Clone + Debug + Default {
10
10
11
11
/// The numerical number of bits.
12
12
fn number_of_bits ( ) -> usize ;
13
+
14
+ fn byte_array_len ( ) -> usize {
15
+ <<Self :: Bytes as NumberOfBytes >:: AsBytes as ByteArray >:: len ( )
16
+ }
13
17
}
14
18
15
19
/// These bits are a multiple of 8
@@ -30,61 +34,57 @@ pub trait NumberOfBytes: Copy + Clone + Debug + Default {
30
34
}
31
35
32
36
/// Helper that allows us to cast a fixed size array into a byte slice.
33
- pub trait ByteArray {
37
+ pub trait ByteArray : Clone {
34
38
fn len ( ) -> usize ;
35
39
fn as_bytes_slice ( & self ) -> & [ u8 ] ;
36
40
fn as_mut_bytes_slice ( & mut self ) -> & mut [ u8 ] ;
37
41
fn rotate_right ( & mut self , bytes : usize ) ;
38
42
fn new ( value : u8 ) -> Self ;
39
43
}
40
44
41
- macro_rules! bytes_type {
42
- ( $T: ident, $N: expr) => {
43
- #[ derive( Copy , Clone , Debug , Default , PartialEq ) ]
44
- pub struct $T;
45
+ impl < const N : usize > ByteArray for [ u8 ; N ] {
46
+ #[ inline]
47
+ fn len ( ) -> usize {
48
+ N
49
+ }
45
50
46
- impl NumberOfBytes for $T {
47
- type AsBytes = [ u8 ; $N] ;
51
+ #[ inline]
52
+ fn as_bytes_slice ( & self ) -> & [ u8 ] {
53
+ & self [ ..]
54
+ }
48
55
49
- #[ inline]
50
- fn number_of_bytes( ) -> usize {
51
- $N
52
- }
53
- }
56
+ #[ inline]
57
+ fn as_mut_bytes_slice ( & mut self ) -> & mut [ u8 ] {
58
+ & mut self [ ..]
59
+ }
54
60
55
- impl ByteArray for [ u8 ; $N] {
56
- #[ inline]
57
- fn len( ) -> usize {
58
- $N
59
- }
61
+ #[ inline]
62
+ fn rotate_right ( & mut self , bytes : usize ) {
63
+ bytes_rotate_right ( self , bytes)
64
+ }
60
65
61
- # [ inline ]
62
- fn as_bytes_slice ( & self ) -> & [ u8 ] {
63
- & self [ .. ]
64
- }
66
+ fn new ( value : u8 ) -> Self {
67
+ [ value ; N ]
68
+ }
69
+ }
65
70
66
- #[ inline]
67
- fn as_mut_bytes_slice( & mut self ) -> & mut [ u8 ] {
68
- & mut self [ ..]
69
- }
71
+ #[ derive( Default , Debug , Copy , Clone , PartialEq ) ]
72
+ pub struct Bytes < const N : usize > ;
70
73
71
- #[ inline]
72
- fn rotate_right( & mut self , bytes: usize ) {
73
- bytes_rotate_right( self , bytes)
74
- }
74
+ impl < const N : usize > NumberOfBytes for Bytes < N > {
75
+ type AsBytes = [ u8 ; N ] ;
75
76
76
- fn new( value: u8 ) -> Self {
77
- [ value; $N]
78
- }
79
- }
77
+ #[ inline]
78
+ fn number_of_bytes ( ) -> usize {
79
+ N
80
80
}
81
81
}
82
82
83
- macro_rules! bits_type {
84
- ( $T: ident, $N: expr, $TB: ident, $TBK: ident) => {
85
- #[ derive( Copy , Clone , Debug , Default , PartialEq ) ]
86
- pub struct $T;
83
+ #[ derive( Default , Debug , Copy , Clone , PartialEq ) ]
84
+ pub struct Bits < const N : usize > ;
87
85
86
+ macro_rules! bits_type {
87
+ ( $T: ty, $N: expr, $TB: ty, $TBK: ident) => {
88
88
impl NumberOfBits for $T {
89
89
type Bytes = $TB;
90
90
0 commit comments