From 4dfc82e1f62d2c86689c171ee0cd0d73b3d02e83 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Tue, 2 Dec 2025 21:39:33 -0600 Subject: [PATCH] Decode array values --- Cargo.toml | 2 +- src/aligned.rs | 16 ++++++++++------ src/telemetry/mod.rs | 4 ++-- src/telemetry/{record.rs => sample.rs} | 24 +++++++++++++----------- 4 files changed, 26 insertions(+), 20 deletions(-) rename src/telemetry/{record.rs => sample.rs} (68%) diff --git a/Cargo.toml b/Cargo.toml index c53df3b..a2438db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" [dependencies] aligned-vec = "0.6.4" -bytemuck = { version = "1.24.0", features = ["derive"] } +bytemuck = { version = "1.24.0", features = ["derive", "extern_crate_alloc"] } chrono = "0.4.42" indexmap = "2.12.1" saphyr = "0.0.6" diff --git a/src/aligned.rs b/src/aligned.rs index 2e9a600..46e2a9f 100644 --- a/src/aligned.rs +++ b/src/aligned.rs @@ -1,3 +1,7 @@ +//! Helper for ensuring raw bytes are aligned + +use std::mem::size_of; + /// Wrapper for raw bytes that forces them to be aligned to `8`, the max alignment for SDK data /// types #[repr(align(8))] @@ -10,20 +14,20 @@ impl Aligned { } } -impl From> for u32 { - fn from(wrapper: Aligned<4>) -> Self { +impl From() }>> for u32 { + fn from(wrapper: Aligned<{ size_of::() }>) -> Self { *bytemuck::from_bytes(&wrapper.0) } } -impl From> for f32 { - fn from(wrapper: Aligned<4>) -> Self { +impl From() }>> for f32 { + fn from(wrapper: Aligned<{ size_of::() }>) -> Self { *bytemuck::from_bytes(&wrapper.0) } } -impl From> for f64 { - fn from(wrapper: Aligned<8>) -> Self { +impl From() }>> for f64 { + fn from(wrapper: Aligned<{ size_of::() }>) -> Self { *bytemuck::from_bytes(&wrapper.0) } } diff --git a/src/telemetry/mod.rs b/src/telemetry/mod.rs index a95aa43..3af9847 100644 --- a/src/telemetry/mod.rs +++ b/src/telemetry/mod.rs @@ -1,4 +1,4 @@ -mod record; +mod sample; mod var; use std::time::Duration; @@ -7,7 +7,7 @@ use chrono::{DateTime, Utc}; use crate::raw; -pub use record::{Record, Sample, Value}; +pub use sample::{Sample, Value}; pub use var::{VarHeader, VarSet, VarType}; #[derive(Clone, Debug)] diff --git a/src/telemetry/record.rs b/src/telemetry/sample.rs similarity index 68% rename from src/telemetry/record.rs rename to src/telemetry/sample.rs index 973c364..dc6c776 100644 --- a/src/telemetry/record.rs +++ b/src/telemetry/sample.rs @@ -1,3 +1,5 @@ +use bytemuck::pod_collect_to_vec; + use crate::{ aligned::align_cast, telemetry::{VarHeader, VarType}, @@ -10,33 +12,30 @@ impl<'data> Sample<'data> { Self(data) } - pub fn read_var(&self, var: &VarHeader) -> Record { + pub fn read_var(&self, var: &VarHeader) -> Value { let size = var.ty.size() * var.count; let slice = &self.0[var.offset..var.offset + size]; if var.count > 1 { - todo!("array values"); + match var.ty { + VarType::Int => Value::IntArray(pod_collect_to_vec(slice)), + VarType::Float => Value::FloatArray(pod_collect_to_vec(slice)), + _ => panic!("unsupported array type"), + } } else { - let value = match var.ty { + match var.ty { VarType::Char => Value::Char(slice[0] as char), VarType::Bool => Value::Bool(slice[0] != 0), VarType::Int => Value::Int(align_cast(slice)), VarType::Bitfield => Value::Bitfield(align_cast(slice)), VarType::Float => Value::Float(align_cast(slice)), VarType::Double => Value::Double(align_cast(slice)), - }; - Record::SingleValue(value) + } } } } #[derive(Clone, Debug)] -pub enum Record { - SingleValue(Value), - Array(Vec), -} - -#[derive(Clone, Copy, Debug)] pub enum Value { Char(char), Bool(bool), @@ -44,4 +43,7 @@ pub enum Value { Bitfield(u32), Float(f32), Double(f64), + + IntArray(Vec), + FloatArray(Vec), } -- 2.51.2