Skip to content

Commit

Permalink
Require structs that impl SbpMessage also impl WireFormat and Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrumley committed Jul 13, 2023
1 parent c000e0a commit 8f6aebf
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
5 changes: 2 additions & 3 deletions generator/sbpg/targets/resources/rust/sbp_messages_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod lib {
use lib::*;

/// Common functionality available to all SBP messages.
pub trait SbpMessage {
pub trait SbpMessage: WireFormat + Clone {
/// Get the message name.
fn message_name(&self) -> &'static str;
/// Get the message type.
Expand All @@ -102,8 +102,7 @@ pub trait SbpMessage {
/// Tells you if the message is valid or if it is not a valid message and may need to be
/// special cased at certain points.
fn is_valid(&self) -> bool;
fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid>
where Self: Sized;
fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid>;
}

/// Implemented by messages who's message name and type are known at compile time.
Expand Down
9 changes: 4 additions & 5 deletions rust/sbp/src/json/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use super::{JsonError, JsonOutput};
use crate::{
json::{CommonJson, HaskellishFloatFormatter, Json2JsonInput, Json2JsonOutput},
messages::Sbp,
wire_format::WireFormat,
SbpMessage, BUFLEN, CRC_LEN, HEADER_LEN, MIN_FRAME_LEN, PREAMBLE,
};

Expand All @@ -20,7 +19,7 @@ const BASE64_BUFLEN: usize = BUFLEN * 4;
pub fn to_writer<W, M>(mut writer: W, msg: &M) -> Result<(), JsonError>
where
W: io::Write,
M: SbpMessage + Serialize + WireFormat + Clone,
M: SbpMessage + Serialize,
{
let mut frame = BytesMut::with_capacity(BUFLEN);
let mut payload = String::with_capacity(BUFLEN);
Expand All @@ -39,7 +38,7 @@ where
/// Serialize the given message as a JSON byte vector.
pub fn to_vec<M>(msg: &M) -> Result<Vec<u8>, JsonError>
where
M: SbpMessage + Serialize + WireFormat + Clone,
M: SbpMessage + Serialize,
{
let mut frame = BytesMut::with_capacity(BUFLEN);
let mut payload = String::with_capacity(BUFLEN);
Expand All @@ -63,7 +62,7 @@ pub fn to_buffer<M, F>(
) -> Result<(), JsonError>
where
F: Formatter,
M: SbpMessage + Serialize + WireFormat + Clone,
M: SbpMessage + Serialize + Clone,
{
let output = JsonOutput {
common: get_common_fields(payload_buf, frame_buf, msg)?,
Expand Down Expand Up @@ -213,7 +212,7 @@ impl<F: Formatter + Clone> Encoder<Json2JsonInput> for Json2JsonEncoderInner<F>
}
}

fn get_common_fields<'a, M: SbpMessage + WireFormat + Clone>(
fn get_common_fields<'a, M: SbpMessage>(
payload_buf: &'a mut String,
frame_buf: &'a mut BytesMut,
msg: &M,
Expand Down
6 changes: 2 additions & 4 deletions rust/sbp/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ mod lib {
use lib::*;

/// Common functionality available to all SBP messages.
pub trait SbpMessage {
pub trait SbpMessage: WireFormat + Clone {
/// Get the message name.
fn message_name(&self) -> &'static str;
/// Get the message type.
Expand All @@ -350,9 +350,7 @@ pub trait SbpMessage {
/// Tells you if the message is valid or if it is not a valid message and may need to be
/// special cased at certain points.
fn is_valid(&self) -> bool;
fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid>
where
Self: Sized;
fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid>;
}

/// Implemented by messages who's message name and type are known at compile time.
Expand Down
9 changes: 3 additions & 6 deletions rust/sbp/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{BUFLEN, MAX_PAYLOAD_LEN, PREAMBLE};
pub fn to_writer<W, M>(mut writer: W, msg: &M) -> Result<(), Error>
where
W: io::Write,
M: SbpMessage + WireFormat,
M: SbpMessage,
{
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(&mut buf, msg)?;
Expand Down Expand Up @@ -64,16 +64,13 @@ where
/// Ok(())
/// }
/// ```
pub fn to_vec<M: SbpMessage + WireFormat>(msg: &M) -> Result<Vec<u8>, Error> {
pub fn to_vec<M: SbpMessage>(msg: &M) -> Result<Vec<u8>, Error> {
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(&mut buf, msg)?;
Ok(buf.to_vec())
}

pub fn to_buffer<M: SbpMessage + WireFormat>(
buf: &mut BytesMut,
msg: &M,
) -> Result<(), WriteFrameError> {
pub fn to_buffer<M: SbpMessage>(buf: &mut BytesMut, msg: &M) -> Result<(), WriteFrameError> {
if !msg.is_valid() {
msg.write(buf);
return Ok(());
Expand Down

0 comments on commit 8f6aebf

Please sign in to comment.