Skip to content

Commit

Permalink
hybrid-array: move non-size-related impls to toplevel (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored Dec 31, 2023
1 parent 6e3a1eb commit 78e7bf7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
64 changes: 2 additions & 62 deletions hybrid-array/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
use super::{Array, ArrayOps, ArraySize, AssociatedArraySize};

#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};

#[cfg(feature = "zeroize")]
impl<T, U> Zeroize for Array<T, U>
where
T: Zeroize,
U: ArraySize,
{
fn zeroize(&mut self) {
self.0.as_mut().iter_mut().zeroize()
}
}
//! Macros for defining various array sizes, and their associated invocations.
#[cfg(feature = "zeroize")]
impl<T, U> ZeroizeOnDrop for Array<T, U>
where
T: ZeroizeOnDrop,
U: ArraySize,
{
}
use super::{Array, ArrayOps, ArraySize, AssociatedArraySize};

macro_rules! impl_array_size {
($($len:expr => $ty:ident),+) => {
Expand Down Expand Up @@ -159,43 +139,3 @@ impl_array_size! {
4096 => U4096,
8192 => U8192
}

impl<T, const N: usize> ArrayOps<T, N> for [T; N]
where
Self: AssociatedArraySize,
{
const SIZE: usize = N;

#[inline]
fn as_core_array(&self) -> &[T; N] {
self
}

#[inline]
fn as_mut_core_array(&mut self) -> &mut [T; N] {
self
}

#[inline]
fn from_core_array(arr: [T; N]) -> Self {
arr
}

#[inline]
fn ref_from_core_array(array_ref: &[T; N]) -> &Self {
array_ref
}

#[inline]
fn ref_from_mut_core_array(array_ref: &mut [T; N]) -> &mut Self {
array_ref
}

#[inline]
fn map_to_core_array<F, U>(self, f: F) -> [U; N]
where
F: FnMut(T) -> U,
{
self.map(f)
}
}
64 changes: 63 additions & 1 deletion hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
unused_qualifications
)]

mod impls;

pub use typenum;
pub use typenum::consts;

Expand All @@ -39,7 +41,8 @@ use core::{
};
use typenum::{Diff, Sum, Unsigned};

mod impls;
#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Hybrid typenum-based and const generic array type.
///
Expand Down Expand Up @@ -532,6 +535,25 @@ where
}
}

#[cfg(feature = "zeroize")]
impl<T, U> Zeroize for Array<T, U>
where
T: Zeroize,
U: ArraySize,
{
fn zeroize(&mut self) {
self.0.as_mut().iter_mut().zeroize()
}
}

#[cfg(feature = "zeroize")]
impl<T, U> ZeroizeOnDrop for Array<T, U>
where
T: ZeroizeOnDrop,
U: ArraySize,
{
}

/// Generate a [`TryFromSliceError`] if the slice doesn't match the given length.
#[cfg_attr(debug_assertions, allow(clippy::panic_in_result_fn))]
fn check_slice_length<T, U: ArraySize>(slice: &[T]) -> Result<(), TryFromSliceError> {
Expand Down Expand Up @@ -587,6 +609,46 @@ pub trait ArrayOps<T, const N: usize>:
F: FnMut(T) -> U;
}

impl<T, const N: usize> ArrayOps<T, N> for [T; N]
where
Self: AssociatedArraySize,
{
const SIZE: usize = N;

#[inline]
fn as_core_array(&self) -> &[T; N] {
self
}

#[inline]
fn as_mut_core_array(&mut self) -> &mut [T; N] {
self
}

#[inline]
fn from_core_array(arr: [T; N]) -> Self {
arr
}

#[inline]
fn ref_from_core_array(array_ref: &[T; N]) -> &Self {
array_ref
}

#[inline]
fn ref_from_mut_core_array(array_ref: &mut [T; N]) -> &mut Self {
array_ref
}

#[inline]
fn map_to_core_array<F, U>(self, f: F) -> [U; N]
where
F: FnMut(T) -> U,
{
self.map(f)
}
}

/// Slice operations which don't have access to a const generic array size.
pub trait SliceOps<T>:
AsRef<[T]>
Expand Down

0 comments on commit 78e7bf7

Please sign in to comment.