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

rust: support encoding borrowed messages #986

Merged
merged 5 commits into from
May 18, 2021
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
2 changes: 1 addition & 1 deletion generator/sbpg/targets/resources/sbp-cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ version = "0.3"
optional = true

[dependencies.dencode]
version = "0.1.0"
version = "0.3.0"
default-features = false

[dev-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion generator/sbpg/targets/resources/sbp2json-cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ path = "../sbp"
features = ["json"]

[dependencies]
dencode = "0.1"
env_logger = "0.8"
serde_json = "1.0"
structopt = "0.3"

[dependencies.dencode]
version = "0.3.0"
default-features = false

[target.'cfg(all(not(windows), not(target_env = "musl")))'.dependencies]
jemallocator = "0.3"

Expand Down
2 changes: 1 addition & 1 deletion rust/sbp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ version = "0.3"
optional = true

[dependencies.dencode]
version = "0.1.0"
version = "0.3.0"
default-features = false

[dev-dependencies]
Expand Down
27 changes: 17 additions & 10 deletions rust/sbp/src/codec/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::{borrow::Borrow, collections::HashMap};

use bytes::{Buf, BufMut, BytesMut};
use dencode::{Decoder, Encoder};
use dencode::{Decoder, Encoder, FramedWrite};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{ser::Formatter, Deserializer, Serializer, Value};

Expand Down Expand Up @@ -156,6 +156,10 @@ impl<F: Formatter + Clone> JsonEncoder<F> {
formatter,
}
}

pub fn framed<W>(writer: W, formatter: F) -> FramedWrite<W, Self> {
FramedWrite::new(writer, Self::new(formatter))
}
}

#[derive(Debug, Serialize)]
Expand All @@ -164,16 +168,20 @@ struct JsonOutput<'a> {
common: CommonJson<'a>,

#[serde(flatten)]
msg: SBP,
msg: &'a SBP,
}

impl<F: Formatter + Clone> Encoder for JsonEncoder<F> {
type Item = SBP;
impl<F, T> Encoder<T> for JsonEncoder<F>
where
F: Formatter + Clone,
T: Borrow<SBP>,
{
type Error = Error;

fn encode(&mut self, msg: SBP, dst: &mut BytesMut) -> Result<()> {
fn encode(&mut self, msg: T, dst: &mut BytesMut) -> Result<()> {
let msg = msg.borrow();
let formatter = self.formatter.clone();
let common = get_common_fields(&mut self.payload_buf, &mut self.frame_buf, &msg)?;
let common = get_common_fields(&mut self.payload_buf, &mut self.frame_buf, msg)?;
let output = JsonOutput { common, msg };

let mut ser = Serializer::with_formatter(dst.writer(), formatter);
Expand Down Expand Up @@ -208,8 +216,7 @@ impl<F: Formatter + Clone> Json2JsonEncoder<F> {
}
}

impl<F: Formatter + Clone> Encoder for Json2JsonEncoder<F> {
type Item = Json2JsonInput;
impl<F: Formatter + Clone> Encoder<Json2JsonInput> for Json2JsonEncoder<F> {
type Error = Error;

fn encode(&mut self, input: Json2JsonInput, dst: &mut BytesMut) -> Result<()> {
Expand All @@ -227,7 +234,7 @@ impl<F: Formatter + Clone> Encoder for Json2JsonEncoder<F> {
let output = Json2JsonOutput {
data: JsonOutput {
common: get_common_fields(&mut self.payload_buf, &mut self.frame_buf, &msg)?,
msg,
msg: &msg,
},
other: input.other,
};
Expand Down
2 changes: 2 additions & 0 deletions rust/sbp/src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use dencode;

#[cfg(feature = "json")]
pub mod json;
pub mod sbp;
18 changes: 13 additions & 5 deletions rust/sbp/src/codec/sbp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Borrow;

use bytes::{Buf, BufMut, BytesMut};
use dencode::{Decoder, Encoder, FramedRead};
use dencode::{Decoder, Encoder, FramedRead, FramedWrite};

use crate::{
messages::{SBPMessage, SBP},
Expand Down Expand Up @@ -71,15 +73,21 @@ impl SbpEncoder {
frame: Vec::with_capacity(MAX_FRAME_LENGTH),
}
}

pub fn framed<W>(writer: W) -> FramedWrite<W, Self> {
FramedWrite::new(writer, Self::new())
}
}

impl Encoder for SbpEncoder {
type Item = SBP;
impl<T> Encoder<T> for SbpEncoder
where
T: Borrow<SBP>,
{
type Error = Error;

fn encode(&mut self, msg: SBP, dst: &mut BytesMut) -> Result<()> {
fn encode(&mut self, msg: T, dst: &mut BytesMut) -> Result<()> {
self.frame.clear();
match msg.write_frame(&mut self.frame) {
match msg.borrow().write_frame(&mut self.frame) {
Ok(_) => dst.put_slice(self.frame.as_slice()),
Err(err) => log::error!("Error converting sbp message to frame: {}", err),
}
Expand Down
5 changes: 4 additions & 1 deletion rust/sbp2json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ path = "../sbp"
features = ["json"]

[dependencies]
dencode = "0.1"
env_logger = "0.8"
serde_json = "1.0"
structopt = "0.3"

[dependencies.dencode]
version = "0.3.0"
default-features = false

[target.'cfg(all(not(windows), not(target_env = "musl")))'.dependencies]
jemallocator = "0.3"

Expand Down
8 changes: 4 additions & 4 deletions rust/sbp2json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where
W: Write,
{
let source = FramedRead::new(input, JsonDecoder::new());
let sink = FramedWrite::new(output, SbpEncoder::new());
let sink = SbpEncoder::framed(output);

maybe_send_buffered(source, sink, buffered)?;

Expand Down Expand Up @@ -45,7 +45,7 @@ where
F: Formatter + Clone,
{
let source = FramedRead::new(input, SbpDecoder {});
let sink = FramedWrite::new(output, JsonEncoder::new(formatter));
let sink = JsonEncoder::framed(output, formatter);

maybe_send_buffered(source, sink, buffered)?;

Expand All @@ -60,8 +60,8 @@ fn maybe_send_buffered<R, W, D, E>(
where
R: Read,
W: Write,
D: Decoder<Item = E::Item, Error = Error>,
E: Encoder<Error = Error>,
D: Decoder<Error = Error>,
E: Encoder<D::Item, Error = Error>,
{
if buffered {
sink.send_all(source)?;
Expand Down