From abe41ed9a188d52be282421cd8ae501c0eaaec5a Mon Sep 17 00:00:00 2001
From: Russel Arbore <russel.jma@gmail.com>
Date: Sun, 16 Feb 2025 21:10:57 -0600
Subject: [PATCH] HerculesRefInto

---
 hercules_rt/src/lib.rs          | 16 ++++++++++++++++
 juno_samples/matmul/src/main.rs | 10 +++-------
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/hercules_rt/src/lib.rs b/hercules_rt/src/lib.rs
index 4cf9b51a..841c6f44 100644
--- a/hercules_rt/src/lib.rs
+++ b/hercules_rt/src/lib.rs
@@ -811,3 +811,19 @@ where
         self.as_cuda_ref()
     }
 }
+
+pub trait HerculesRefInto<'a> {
+    fn to(&'a self) -> HerculesCPURef<'a>;
+}
+
+impl<'a, T> HerculesRefInto<'a> for &'a [T] {
+    fn to(&'a self) -> HerculesCPURef<'a> {
+        HerculesCPURef::from_slice(self)
+    }
+}
+
+impl<'a, T> HerculesRefInto<'a> for Box<[T]> {
+    fn to(&'a self) -> HerculesCPURef<'a> {
+        HerculesCPURef::from_slice(self)
+    }
+}
diff --git a/juno_samples/matmul/src/main.rs b/juno_samples/matmul/src/main.rs
index 2eb2804b..3cb7d7f0 100644
--- a/juno_samples/matmul/src/main.rs
+++ b/juno_samples/matmul/src/main.rs
@@ -2,9 +2,9 @@
 
 use rand::random;
 
+use hercules_rt::{runner, HerculesRefInto};
 #[cfg(feature = "cuda")]
-use hercules_rt::CUDABox;
-use hercules_rt::{runner, HerculesCPURef};
+use hercules_rt::{CUDABox, HerculesCPURef};
 
 juno_build::juno!("matmul");
 
@@ -25,12 +25,8 @@ fn main() {
         }
         #[cfg(not(feature = "cuda"))]
         {
-            let a = HerculesCPURef::from_slice(&a);
-            let b = HerculesCPURef::from_slice(&b);
             let mut r = runner!(matmul);
-            let c = r
-                .run(I as u64, J as u64, K as u64, a.clone(), b.clone())
-                .await;
+            let c = r.run(I as u64, J as u64, K as u64, a.to(), b.to()).await;
             assert_eq!(c.as_slice::<i32>(), &*correct_c);
         }
         #[cfg(feature = "cuda")]
-- 
GitLab