From e1a69e69d1b71b2f655e2ae68893e11a9f9297d2 Mon Sep 17 00:00:00 2001
From: Federico Lolli <federico.lolli@skywarder.eu>
Date: Mon, 19 Feb 2024 07:52:25 +0100
Subject: [PATCH] add conversion traits for f32

---
 src/types.rs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/types.rs b/src/types.rs
index eb9173b..04b28f9 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -42,6 +42,18 @@ impl IntoRustType<f64> for MxArray {
     }
 }
 
+impl IntoRustType<f32> for MxArray {
+    fn into_rust(self) -> SResult<f32> {
+        let out = Numeric::<f32, _>::from_mx_array(self)
+            .mexerr(Error::InvalidMatlabType("cast to a single instead".into()))?
+            .data()
+            .first()
+            .mexerr(Error::Parse)?
+            .to_owned();
+        Ok(out)
+    }
+}
+
 impl IntoRustType<Vec<f64>> for MxArray {
     fn into_rust(self) -> SResult<Vec<f64>> {
         let out = Numeric::<f64, _>::from_mx_array(self)
@@ -69,6 +81,16 @@ impl IntoMatlabType for Vec<f64> {
     }
 }
 
+impl IntoMatlabType for Vec<f32> {
+    fn into_matlab(self) -> SResult<MxArray> {
+        let len = self.len();
+        Ok(Numeric::<f32, _>::new(self.into_boxed_slice(), &[len])
+            .unwrap()
+            .deref()
+            .to_owned())
+    }
+}
+
 /// Functions available to the user
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
 pub enum Mode {
-- 
GitLab