Giter Site home page Giter Site logo

embedded-msgpack's People

Contributors

jsoverson avatar sympatron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jsoverson

embedded-msgpack's Issues

embedded-msgpack drags in std; cannot build with no_std

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`

Lifetimes issue when deserializing

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?

Custom formatters seem to break deserialization when None

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),
        }
    }
}

Decoding messagepack timestamps

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.