@@ -22,17 +22,15 @@ pub enum FixedByteError {
22
22
}
23
23
24
24
impl FixedByteError {
25
- fn field_name ( & self ) -> & ' static str {
25
+ const fn field_name ( & self ) -> & ' static str {
26
26
match self {
27
- FixedByteError :: InvalidLength => "input" ,
27
+ Self :: InvalidLength => "input" ,
28
28
}
29
29
}
30
30
31
- fn field_data ( & self ) -> & ' static str {
31
+ const fn field_data ( & self ) -> & ' static str {
32
32
match self {
33
- FixedByteError :: InvalidLength => {
34
- "Cannot create fix byte array, input has invalid length."
35
- }
33
+ Self :: InvalidLength => "Cannot create fix byte array, input has invalid length." ,
36
34
}
37
35
}
38
36
}
@@ -82,7 +80,7 @@ impl<const N: usize> ByteArray<N> {
82
80
83
81
impl < const N : usize > From < [ u8 ; N ] > for ByteArray < N > {
84
82
fn from ( value : [ u8 ; N ] ) -> Self {
85
- ByteArray ( Bytes :: copy_from_slice ( & value) )
83
+ Self ( Bytes :: copy_from_slice ( & value) )
86
84
}
87
85
}
88
86
@@ -101,7 +99,7 @@ impl<const N: usize> TryFrom<Bytes> for ByteArray<N> {
101
99
if value. len ( ) != N {
102
100
return Err ( FixedByteError :: InvalidLength ) ;
103
101
}
104
- Ok ( ByteArray ( value) )
102
+ Ok ( Self ( value) )
105
103
}
106
104
}
107
105
@@ -112,7 +110,7 @@ impl<const N: usize> TryFrom<Vec<u8>> for ByteArray<N> {
112
110
if value. len ( ) != N {
113
111
return Err ( FixedByteError :: InvalidLength ) ;
114
112
}
115
- Ok ( ByteArray ( Bytes :: from ( value) ) )
113
+ Ok ( Self ( Bytes :: from ( value) ) )
116
114
}
117
115
}
118
116
@@ -142,11 +140,11 @@ impl<'de, const N: usize> Deserialize<'de> for ByteArrayVec<N> {
142
140
}
143
141
144
142
impl < const N : usize > ByteArrayVec < N > {
145
- pub fn len ( & self ) -> usize {
143
+ pub const fn len ( & self ) -> usize {
146
144
self . 0 . len ( ) / N
147
145
}
148
146
149
- pub fn is_empty ( & self ) -> bool {
147
+ pub const fn is_empty ( & self ) -> bool {
150
148
self . len ( ) == 0
151
149
}
152
150
@@ -162,16 +160,17 @@ impl<const N: usize> ByteArrayVec<N> {
162
160
///
163
161
/// # Panics
164
162
/// Panics if at > len.
163
+ #[ must_use]
165
164
pub fn split_off ( & mut self , at : usize ) -> Self {
166
165
Self ( self . 0 . split_off ( at * N ) )
167
166
}
168
167
}
169
168
170
169
impl < const N : usize > From < & ByteArrayVec < N > > for Vec < [ u8 ; N ] > {
171
170
fn from ( value : & ByteArrayVec < N > ) -> Self {
172
- let mut out = Vec :: with_capacity ( value. len ( ) ) ;
171
+ let mut out = Self :: with_capacity ( value. len ( ) ) ;
173
172
for i in 0 ..value. len ( ) {
174
- out. push ( value[ i] )
173
+ out. push ( value[ i] ) ;
175
174
}
176
175
177
176
out
@@ -181,11 +180,11 @@ impl<const N: usize> From<&ByteArrayVec<N>> for Vec<[u8; N]> {
181
180
impl < const N : usize > From < Vec < [ u8 ; N ] > > for ByteArrayVec < N > {
182
181
fn from ( value : Vec < [ u8 ; N ] > ) -> Self {
183
182
let mut bytes = BytesMut :: with_capacity ( N * value. len ( ) ) ;
184
- for i in value. into_iter ( ) {
185
- bytes. extend_from_slice ( & i)
183
+ for i in value {
184
+ bytes. extend_from_slice ( & i) ;
186
185
}
187
186
188
- ByteArrayVec ( bytes. freeze ( ) )
187
+ Self ( bytes. freeze ( ) )
189
188
}
190
189
}
191
190
@@ -197,25 +196,25 @@ impl<const N: usize> TryFrom<Bytes> for ByteArrayVec<N> {
197
196
return Err ( FixedByteError :: InvalidLength ) ;
198
197
}
199
198
200
- Ok ( ByteArrayVec ( value) )
199
+ Ok ( Self ( value) )
201
200
}
202
201
}
203
202
204
203
impl < const N : usize > From < [ u8 ; N ] > for ByteArrayVec < N > {
205
204
fn from ( value : [ u8 ; N ] ) -> Self {
206
- ByteArrayVec ( Bytes :: copy_from_slice ( value. as_slice ( ) ) )
205
+ Self ( Bytes :: copy_from_slice ( value. as_slice ( ) ) )
207
206
}
208
207
}
209
208
210
209
impl < const N : usize , const LEN : usize > From < [ [ u8 ; N ] ; LEN ] > for ByteArrayVec < N > {
211
210
fn from ( value : [ [ u8 ; N ] ; LEN ] ) -> Self {
212
211
let mut bytes = BytesMut :: with_capacity ( N * LEN ) ;
213
212
214
- for val in value. into_iter ( ) {
213
+ for val in value {
215
214
bytes. put_slice ( val. as_slice ( ) ) ;
216
215
}
217
216
218
- ByteArrayVec ( bytes. freeze ( ) )
217
+ Self ( bytes. freeze ( ) )
219
218
}
220
219
}
221
220
@@ -227,17 +226,20 @@ impl<const N: usize> TryFrom<Vec<u8>> for ByteArrayVec<N> {
227
226
return Err ( FixedByteError :: InvalidLength ) ;
228
227
}
229
228
230
- Ok ( ByteArrayVec ( Bytes :: from ( value) ) )
229
+ Ok ( Self ( Bytes :: from ( value) ) )
231
230
}
232
231
}
233
232
234
233
impl < const N : usize > Index < usize > for ByteArrayVec < N > {
235
234
type Output = [ u8 ; N ] ;
236
235
237
236
fn index ( & self , index : usize ) -> & Self :: Output {
238
- if ( index + 1 ) * N > self . 0 . len ( ) {
239
- panic ! ( "Index out of range, idx: {}, length: {}" , index, self . len( ) ) ;
240
- }
237
+ assert ! (
238
+ ( index + 1 ) * N <= self . 0 . len( ) ,
239
+ "Index out of range, idx: {}, length: {}" ,
240
+ index,
241
+ self . len( )
242
+ ) ;
241
243
242
244
self . 0 [ index * N ..( index + 1 ) * N ]
243
245
. as_ref ( )
0 commit comments