From 8aab22de6fad1d3336065e9bc8e83b2a6e45e960 Mon Sep 17 00:00:00 2001
From: Martin Larralde <martin.larralde@embl.de>
Date: Sun, 16 Jun 2024 18:09:22 +0200
Subject: [PATCH] Manually implement `Clone` for `DenseMatrix` to enforce
 memory alignment

---
 lightmotif/src/dense.rs | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lightmotif/src/dense.rs b/lightmotif/src/dense.rs
index 7487b2a..f16ef3a 100644
--- a/lightmotif/src/dense.rs
+++ b/lightmotif/src/dense.rs
@@ -42,7 +42,7 @@ impl MatrixCoordinates {
 // --- DenseMatrix -------------------------------------------------------------
 
 /// A memory-aligned dense matrix with a constant number of columns.
-#[derive(Clone, Eq)]
+#[derive(Eq)]
 pub struct DenseMatrix<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo = DefaultAlignment>
 {
     data: Vec<T>,
@@ -202,6 +202,18 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C,
     }
 }
 
+impl<T: Default + Copy + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Clone
+    for DenseMatrix<T, C, A>
+{
+    fn clone(&self) -> Self {
+        let mut clone = unsafe { Self::uninitialized(self.rows) };
+        let l = self.rows() * self.stride();
+        clone.data[clone.offset..clone.offset + l]
+            .copy_from_slice(&self.data[self.offset..self.offset + l]);
+        clone
+    }
+}
+
 impl<T: Default + Copy + Debug, C: Unsigned, A: Unsigned + PowerOfTwo> Debug
     for DenseMatrix<T, C, A>
 {
-- 
GitLab