Skip to content

Commit

Permalink
bytemuck feature error
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed May 31, 2024
1 parent c81e400 commit d0ba1f9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ repository = "https://github.com/quartiq/stabilizer-streaming"
clap = { version = "4.3", features = ["derive"] }
num_enum = "0.7"
log = "0.4"
eframe = { version = "0.27", default-features = false, features = ["wayland", "x11", "glow", "default_fonts"] }
eframe = { version = "0.27", default-features = false, features = [
"wayland",
"x11",
"glow",
"default_fonts",
] }
egui_plot = "0.27"
env_logger = "0.11"
bytemuck = "1.13.1"
bytemuck = { version = "1.13.1", features = ["extern_crate_std"] }
thiserror = "1.0.47"
anyhow = "1.0.75"
socket2 = "0.5.4"
Expand Down
15 changes: 8 additions & 7 deletions src/de/data.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::Error;
use core::fmt::Debug;

pub trait Payload<'a> {
pub trait Payload<'a>: Debug {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error>
where
Self: Sized;
fn traces(&self) -> Result<Vec<(&'static str, Vec<f32>)>, Error>;
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct AdcDac<'a> {
data: &'a [[[[u8; 2]; 8]; 4]],
}
Expand All @@ -20,8 +22,7 @@ impl<'a> Payload<'a> for AdcDac<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
const CHANNELS: usize = 4;
const BATCH_SIZE: usize = 8;
let data: &[[[[u8; 2]; BATCH_SIZE]; CHANNELS]] =
bytemuck::try_cast_slice(data).map_err(Error::PayloadSize)?;
let data: &[[[[u8; 2]; BATCH_SIZE]; CHANNELS]] = bytemuck::try_cast_slice(data)?;
assert_eq!(data.len(), batches);
Ok(Self { data })
}
Expand Down Expand Up @@ -82,14 +83,14 @@ impl<'a> Payload<'a> for AdcDac<'a> {
}
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Fls<'a> {
data: &'a [[[[u8; 4]; 7]; 2]],
}

impl<'a> Payload<'a> for Fls<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
let data: &[[[[u8; 4]; 7]; 2]] =
bytemuck::try_cast_slice(data).map_err(Error::PayloadSize)?;
let data: &[[[[u8; 4]; 7]; 2]] = bytemuck::try_cast_slice(data)?;
// demod_re, demod_im, phase[2], ftw, pow_amp, pll
assert_eq!(batches, data.len());
Ok(Self { data })
Expand Down Expand Up @@ -140,14 +141,14 @@ impl<'a> Payload<'a> for Fls<'a> {
}
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct ThermostatEem<'a> {
data: &'a [[[u8; 4]; 16 + 4]],
}

impl<'a> Payload<'a> for ThermostatEem<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
let data: &[[[u8; 4]; 16 + 4]] =
bytemuck::try_cast_slice(data).map_err(Error::PayloadSize)?;
let data: &[[[u8; 4]; 16 + 4]] = bytemuck::try_cast_slice(data)?;
assert_eq!(batches, data.len());
Ok(Self { data })
}
Expand Down
25 changes: 10 additions & 15 deletions src/de/frame.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use super::data::{self, Payload};
use super::{Error, Format};

use std::convert::TryFrom;

// The magic word at the start of each stream frame.
const MAGIC_WORD: [u8; 2] = [0x7b, 0x05];

// The size of the frame header in bytes.
const HEADER_SIZE: usize = 8;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Header {
// The format code associated with the stream binary data.
pub format: Format,
Expand Down Expand Up @@ -40,26 +38,23 @@ impl Header {
}

/// A single stream frame contains multiple batches of data.
// #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug)]
pub struct Frame<'a> {
pub header: Header,
pub data: &'a [u8],
pub payload: Box<dyn Payload<'a> + 'a>,
}

impl<'a> Frame<'a> {
/// Parse a stream frame from a single UDP packet.
pub fn from_bytes(input: &'a [u8]) -> Result<Self, Error> {
let header = Header::parse(&input[..HEADER_SIZE].try_into().unwrap())?;
let data = &input[HEADER_SIZE..];
Ok(Self { header, data })
}

pub fn traces(&self) -> Result<Vec<(&'static str, Vec<f32>)>, Error> {
let batches = self.header.batches as _;
Ok(match self.header.format {
Format::AdcDac => data::AdcDac::new(batches, self.data)?.traces()?,
Format::Fls => data::Fls::new(batches, self.data)?.traces()?,
Format::ThermostatEem => data::ThermostatEem::new(batches, self.data)?.traces()?,
})
let batches = header.batches as _;
let payload: Box<dyn Payload> = match header.format {
Format::AdcDac => Box::new(data::AdcDac::new(batches, data)?),
Format::Fls => Box::new(data::Fls::new(batches, data)?),
Format::ThermostatEem => Box::new(data::ThermostatEem::new(batches, data)?),
};
Ok(Self { header, payload })
}
}
2 changes: 1 addition & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ pub enum Error {
#[error("Unknown format ID")]
UnknownFormat,
#[error("Payload size")]
PayloadSize(bytemuck::PodCastError),
PayloadSize(#[from] bytemuck::PodCastError),
}
4 changes: 2 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Source {
Ok(()) => {
let frame = Frame::from_bytes(&buf[..self.opts.frame_size])?;
self.loss.update(&frame);
break frame.traces()?;
break frame.payload.traces()?;
}
Err(e) if e.kind() == ErrorKind::UnexpectedEof && self.opts.repeat => {
fil.seek(std::io::SeekFrom::Start(0))?;
Expand All @@ -163,7 +163,7 @@ impl Source {
let len = socket.recv(unsafe { core::mem::transmute(&mut buf[..]) })?; // meh
let frame = Frame::from_bytes(&buf[..len])?;
self.loss.update(&frame);
frame.traces()?
frame.payload.traces()?
}
})
}
Expand Down

0 comments on commit d0ba1f9

Please sign in to comment.