From d40a038c2f622df91201208d97100d8f3bd5f0cc Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 24 Sep 2020 02:06:53 +0200 Subject: [PATCH] config: switch to postcard encoding --- Cargo.lock | 51 +++++++++++++++++++++++++++++++++++++-------------- Cargo.toml | 2 +- src/config.rs | 16 +++++++--------- src/main.rs | 6 +++--- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a434fc..7c54454 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,10 +174,26 @@ dependencies = [ ] [[package]] -name = "half" -version = "1.6.0" +name = "hash32" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d36fab90f82edc3c747f9d438e06cf0a491055896f2a279638bb5beed6c40177" +checksum = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1" +dependencies = [ + "as-slice", + "generic-array 0.13.2", + "hash32", + "serde", + "stable_deref_trait", +] [[package]] name = "libm" @@ -257,6 +273,23 @@ dependencies = [ "cortex-m-semihosting", ] +[[package]] +name = "postcard" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3e3f5c2e9a91383c6594ec68aa2dfdfe19a3c86f34b088ba7203f2483d2682f" +dependencies = [ + "heapless", + "postcard-cobs", + "serde", +] + +[[package]] +name = "postcard-cobs" +version = "0.1.5-pre" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c68cb38ed13fd7bc9dd5db8f165b7c8d9c1a315104083a2b10f11354c2af97f" + [[package]] name = "proc-macro2" version = "1.0.20" @@ -320,16 +353,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde_cbor" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" -dependencies = [ - "half", - "serde", -] - [[package]] name = "serde_derive" version = "1.0.116" @@ -440,8 +463,8 @@ dependencies = [ "num-traits", "panic-abort", "panic-semihosting", + "postcard", "serde", - "serde_cbor", "smoltcp", "stm32-eth", "stm32f4xx-hal", diff --git a/Cargo.toml b/Cargo.toml index f9dda31..5c58044 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ nb = "0.1" uom = { version = "0.29", default-features = false, features = ["autoconvert", "si", "f64", "use_serde"] } eeprom24x = "0.3" serde = { version = "1.0", default-features = false, features = ["derive"] } -serde_cbor = { version = "0.11.1", default-features = false } +postcard = "0.5.1" [patch.crates-io] stm32f4xx-hal = { git = "https://github.com/stm32-rs/stm32f4xx-hal.git" } diff --git a/src/config.rs b/src/config.rs index 792998b..038de34 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,5 @@ use serde::{Serialize, Deserialize}; -use serde_cbor::Serializer; -use serde_cbor::ser::SliceWrite; +use postcard::to_slice; use crate::{ channel_state::ChannelState, channels::{CHANNELS, Channels}, @@ -25,13 +24,12 @@ impl Config { } } - pub fn encode(&self, buffer: &mut [u8]) -> Result { - let writer = SliceWrite::new(buffer); - let mut ser = Serializer::new(writer); - self.serialize(&mut ser)?; - let writer = ser.into_inner(); - let size = writer.bytes_written(); - Ok(size) + pub fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], postcard::Error> { + to_slice(self, buffer) + } + + pub fn decode(buffer: &[u8]) -> Result { + from_bytes(buffer) } } diff --git a/src/main.rs b/src/main.rs index f2ce5be..1e6fa1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -432,10 +432,10 @@ fn main() -> ! { Command::Load => {} Command::Save => { let config = Config::new(&mut channels); - let mut buf = [0; 0x200]; + let mut buf = [0; 128]; match config.encode(&mut buf) { - Ok(len) => { - let _ = writeln!(socket, "Encoded: {:?}", &buf[..len]); + Ok(buf) => { + let _ = writeln!(socket, "Encoded {}: {:?}", buf.len(), buf); } Err(e) => { let _ = writeln!(socket, "Error encoding configuration: {}", e);