Add Clone specialization for `Copy` types because it's really trivial to do
@@ -44,7 +44,7 @@ impl<T, const N: usize> AsRef<[T]> for StaticVec<T, { N }> {
impl<T: Clone, const N: usize> Clone for StaticVec<T, { N }> {
#[inline]
- fn clone(&self) -> Self {
+ default fn clone(&self) -> Self {
let mut res = Self::new();
for i in 0..self.length {
// Safety: `self` has the same capacity as `res`, and `res` is
@@ -64,7 +64,7 @@ impl<T: Clone, const N: usize> Clone for StaticVec<T, { N }> {
}
#[inline]
- fn clone_from(&mut self, rhs: &Self) {
+ default fn clone_from(&mut self, rhs: &Self) {
self.truncate(rhs.length);
for i in 0..self.length {
// Safety: after the truncate, self.len <= rhs.len, which means that for
@@ -89,6 +89,28 @@ impl<T: Clone, const N: usize> Clone for StaticVec<T, { N }> {
}
}
+impl<T: Copy, const N: usize> Clone for StaticVec<T, { N }> {
+ #[inline(always)]
+ fn clone(&self) -> Self {
+ //If `self.length` is 0, the `copy_to_nonoverlapping just won't do anything, so this is fine.
+ let mut res = Self::new();
+ unsafe {
+ self.as_ptr().copy_to_nonoverlapping(res.as_mut_ptr(), self.length);
+ }
+ res.length = self.length;
+ res
+ }
+
+ #[inline(always)]
+ fn clone_from(&mut self, rhs: &Self) {
+ //If `rhs.length` is 0, the `copy_to_nonoverlapping just won't do anything, so this is fine.
+ unsafe {
+ rhs.as_ptr().copy_to_nonoverlapping(self.as_mut_ptr(), rhs.length);
+ }
+ self.length = rhs.length;
+ }
+}
+
impl<T: Debug, const N: usize> Debug for StaticVec<T, { N }> {
#[inline(always)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
