Skip to content

Commit

Permalink
hybrid-array: looser Clone and Copy bounds on Array
Browse files Browse the repository at this point in the history
Switches from derived impls of `Clone` and `Copy` to handwritten ones
which use the minimal required bounds for the underlying types.
  • Loading branch information
tarcieri committed Sep 4, 2023
1 parent 1a917fa commit f4df5e1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use typenum::Unsigned;
///
/// Provides the flexibility of typenum-based expressions while also
/// allowing interoperability and a transition path to const generics.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Array<T, U: ArraySize>(pub U::ArrayType<T>);

Expand Down Expand Up @@ -170,6 +170,24 @@ where
}
}

impl<T, U> Clone for Array<T, U>
where
T: Clone,
U: ArraySize,
{
fn clone(&self) -> Self {
Self(U::ArrayType::<T>::from_fn(|n| self.0.as_ref()[n].clone()))
}
}

impl<T, U> Copy for Array<T, U>
where
T: Copy,
U: ArraySize,
U::ArrayType<T>: Copy,
{
}

impl<T, U> Default for Array<T, U>
where
T: Default,
Expand Down

0 comments on commit f4df5e1

Please sign in to comment.