Skip to content

Commit 677b4df

Browse files
committed
hybrid-array: impl Default for Array
1 parent 17f5d34 commit 677b4df

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

hybrid-array/src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ where
244244
}
245245
}
246246

247+
/// Generate a [`TryFromSliceError`] if the slice doesn't match the given length.
248+
fn check_slice_length<T, U: Unsigned>(slice: &[T]) -> Result<(), TryFromSliceError> {
249+
if slice.len() != U::USIZE {
250+
// Hack: `TryFromSliceError` lacks a public constructor
251+
<&[T; 1]>::try_from([].as_slice())?;
252+
}
253+
254+
Ok(())
255+
}
256+
247257
/// Byte array type.
248258
pub type ByteArray<U> = Array<u8, U>;
249259

@@ -398,6 +408,15 @@ macro_rules! impl_array_size {
398408
}
399409
}
400410

411+
impl<T> Default for Array<T, typenum::$ty>
412+
where
413+
T: Default
414+
{
415+
fn default() -> Self {
416+
Self([(); $len].map(|_| T::default()))
417+
}
418+
}
419+
401420
impl<T> IntoIterator for Array<T, typenum::$ty> {
402421
type Item = T;
403422
type IntoIter = IntoIter<T, $len>;
@@ -512,13 +531,3 @@ impl_array_size! {
512531
4096 => U4096,
513532
8192 => U8192
514533
}
515-
516-
/// Generate a [`TryFromSliceError`] if the slice doesn't match the given length.
517-
fn check_slice_length<T, U: Unsigned>(slice: &[T]) -> Result<(), TryFromSliceError> {
518-
if slice.len() != U::USIZE {
519-
// Hack: `TryFromSliceError` lacks a public constructor
520-
<&[T; 1]>::try_from([].as_slice())?;
521-
}
522-
523-
Ok(())
524-
}

0 commit comments

Comments
 (0)