sympatron / embedded-msgpack Goto Github PK
View Code? Open in Web Editor NEWMessagePack serialization implementation for Rust optimized for embedded/no_std environments
License: Apache License 2.0
MessagePack serialization implementation for Rust optimized for embedded/no_std environments
License: Apache License 2.0
I am evaluating this crate for use in an embedded environment.
Unfortunately, it seems to be dragging in the std
crate when the serde
feature is enabled, which is a showstopper when embedded applications don't get the std
crate.
error[E0463]: can't find crate for `std`
--> /home/orion/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-msgpack-0.2.1/src/decode/serde/mod.rs:19:5
|
19 | extern crate std;
| ^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `thumbv7em-none-eabihf` target may not support the standard library
Further, it cannot be compiled without the serde
feature, since it wouldn't get linked to serde_bytes
, which is linked to unconditionally.
error[E0432]: unresolved import `serde_bytes`
--> /home/orion/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-msgpack-0.2.1/src/lib.rs:17:9
|
17 | pub use serde_bytes::Bytes;
| ^^^^^^^^^^^ use of undeclared crate or module `serde_bytes`
I'm able to use embedded-messagepack successfully for encoding/serializing some payload struct (in my case some iot sensor data, so no_std
).
Decoding poses a problem and it only seems to work when the read buffer has a static lifetime (i.e. is a &'static [u8]
).
This fails:
fn main() {
let mut buffer = [0u8; 256];
let mut pl = MyStruct::default();
embedded_msgpack::encode::serde::to_array(&pl, &mut buffer).ok();
let decoded: MyStruct = embedded_msgpack::decode::from_slice(&buffer).unwrap();
}
And this works (the buffer being a static)
static PL: &[u8] = &[ 0x84, .... ];
let pl: MyStruct = embedded_msgpack::decode::from_slice(PL).unwrap();
How to handle this nicely?
I have a struct with optional fields of type [u8; N]
, these arrays need to be encoded into a message pack as a raw blob (string). For this I have written the following custom formatter:
mod optional_array_as_bytes {
use serde::{de::Error, Deserialize, Deserializer, Serializer};
pub fn serialize<S, const N: usize>(field: &Option<[u8; N]>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match field {
Some(pl) => s.serialize_bytes(pl),
None => s.serialize_none(),
}
}
pub fn deserialize<'de, D, const N: usize>(deserializer: D) -> Result<Option<[u8; N]>, D::Error>
where
D: Deserializer<'de>,
{
let slice = <&[u8]>::deserialize(deserializer)?;
if slice.len() == N {
let mut ary = [0u8; N];
ary.copy_from_slice(slice);
Ok(Some(ary))
} else {
Err(Error::custom("Invalid array length"))
}
}
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct TestStruct {
#[serde(rename = "rev")]
#[serde(with = "optional_array_as_bytes")]
#[serde(skip_serializing_if = "Option::is_none")]
pub git_revision: Option<[u8; 4]>,
pub i: Option<u32>,
}
This works as it should, but when the git_revision
field is set to None
it fails.
fn main() {
let mut buffer = [0u8; 256];
let test_struct = TestStruct {
git_revision: Some([1, 2, 3, 4]), // setting to `None` fails!
number: Some(42),
};
if let Ok(len) = embedded_msgpack::encode::serde::to_array(&test_struct, &mut buffer) {
println!("Serialized into: {:?}", &buffer[..len]);
let decoded: Result<TestStruct, _> = embedded_msgpack::decode::from_slice(&buffer);
match decoded {
Ok(p) => println!("Decoded TestStruct: {:?}", p),
Err(e) => println!("Decode ERROR: {}", e),
}
}
}
Is there an example for decoding messagepack timestamps? It seems like it's implemented but I can't find out how to make it work. decoding/encoding the Timestamp
struct encodes it as a map and embedded-msppack doesn't seem to decode timestamps encoded in the messagepack extension format.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.