Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message serialization refactoring #140

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions build/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl MavProfile {
#[allow(unused_imports)]
use heapless::Vec;

use crate::{Message, error::*, MAX_FRAME_SIZE, bytes::Bytes, bytes_mut::BytesMut};
use crate::{Message, error::*, bytes::Bytes, bytes_mut::BytesMut};

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -330,9 +330,9 @@ impl MavProfile {

fn emit_mav_message_serialize(&self, enums: &Vec<TokenStream>) -> TokenStream {
quote! {
fn ser(&self, version: MavlinkVersion) -> BytesMut<MAX_FRAME_SIZE> {
fn ser(&self, version: MavlinkVersion, bytes: &mut [u8]) -> usize {
match *self {
#(MavMessage::#enums(ref body) => body.ser(version),)*
#(MavMessage::#enums(ref body) => body.ser(version, bytes),)*
}
}
}
Expand Down Expand Up @@ -542,12 +542,14 @@ impl MavMessage {
fn emit_serialize_vars(&self) -> TokenStream {
let ser_vars = self.fields.iter().map(|f| f.rust_writer());
quote! {
let mut _tmp = BytesMut::new();
let mut _tmp = BytesMut::new(bytes);
#(#ser_vars)*
if matches!(version, MavlinkVersion::V2) {
crate::remove_trailing_zeroes(&mut _tmp);
let len = _tmp.len();
crate::remove_trailing_zeroes(&mut bytes[..len])
} else {
_tmp.len()
}
_tmp
}
}

Expand Down Expand Up @@ -612,7 +614,7 @@ impl MavMessage {
#deser_vars
}

pub fn ser(&self, version: MavlinkVersion) -> BytesMut<MAX_FRAME_SIZE> {
pub fn ser(&self, version: MavlinkVersion, bytes: &mut [u8]) -> usize {
#serialize_vars
}
}
Expand Down
30 changes: 6 additions & 24 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
pub struct BytesMut<const N: usize> {
data: [u8; N],
pub struct BytesMut<'a> {
data: &'a mut [u8],
len: usize,
}

impl<const N: usize> BytesMut<N> {
pub fn new() -> Self {
Self {
data: [0; N],
len: 0,
}
impl<'a> BytesMut<'a> {
pub fn new(data: &'a mut [u8]) -> Self {
Self { data, len: 0 }
}

#[inline]
Expand All @@ -18,13 +15,7 @@ impl<const N: usize> BytesMut<N> {

#[inline]
pub fn remaining(&self) -> usize {
N - self.len
}

pub fn set_len(&mut self, len: usize) {
assert!(len >= 1);
assert!(len <= N);
self.len = len;
self.data.len() - self.len
}

#[inline]
Expand Down Expand Up @@ -132,12 +123,3 @@ impl<const N: usize> BytesMut<N> {
self.len += SIZE;
}
}

impl<const N: usize> core::ops::Deref for BytesMut<N> {
type Target = [u8];

#[inline]
fn deref(&self) -> &[u8] {
&self.data[..self.len]
}
}
14 changes: 0 additions & 14 deletions src/embedded.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use byteorder::ByteOrder;

use crate::error::*;

/// Replacement for std::io::Read + byteorder::ReadBytesExt in no_std envs
pub trait Read {
fn read_u8(&mut self) -> Result<u8, MessageReadError>;

fn read_u16<B: ByteOrder>(&mut self) -> Result<u16, MessageReadError> {
let mut buffer = [0; 2];
self.read_exact(&mut buffer)?;
Ok(B::read_u16(&buffer))
}

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), MessageReadError> {
for i in 0..buf.len() {
buf[i] = self.read_u8()?;
Expand All @@ -30,12 +22,6 @@ impl<R: embedded_hal::serial::Read<u8>> Read for R {
/// Replacement for std::io::Write + byteorder::WriteBytesExt in no_std envs
pub trait Write {
fn write_all(&mut self, buf: &[u8]) -> Result<(), MessageWriteError>;

fn write_u16<B: ByteOrder>(&mut self, n: u16) -> Result<(), MessageWriteError> {
let mut buffer = [0; 2];
B::write_u16(&mut buffer, n);
self.write_all(&buffer)
}
}

impl<W: embedded_hal::serial::Write<u8>> Write for W {
Expand Down
Loading