From 08e5d6343ba1607b728df02fd8a2c5f69e8284f7 Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Mon, 19 Feb 2024 00:24:42 +0100 Subject: [PATCH] fixed unsafe use --- src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5f1c86a..1d201eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,9 +149,11 @@ fn read_from_serial(mut outputs: Output<'_>, args: Args<'_>) -> SResult<()> { let n_doubles = arg as usize; // Read n_doubles from the serial port - let bytes = SERIAL.read().unwrap().read_n_bytes(n_doubles)?; - // Here we use unsafe to cast the bytes to a slice of f64 (maybe a safer alternative helps) - let doubles = unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const f64, n_doubles) }; + let bytes = SERIAL.read().unwrap().read_n_bytes(n_doubles * 8)?; + let doubles = bytes + .chunks_exact(8) + .map(|chunk| f64::from_be_bytes(chunk.try_into().unwrap())) + .collect::<Vec<f64>>(); warn_debug!("Read {} bytes from serial port", n_doubles); outputs.set(doubles.to_vec())?; -- GitLab