Skip to content

#[msgpack(default)] breaks no_std compatibility #39

@paq

Description

@paq

When using #[msgpack(default)], the macro generates code that calls to_string method. This is usually not an issue, but in a no_std environment, the ToString trait may not be available, which can cause a compilation error like the following:

Example code:

#![no_std]

use zerompk::{FromMessagePack, ToMessagePack};

#[derive(ToMessagePack, FromMessagePack)]
#[msgpack(map)]
pub struct TestData {
    pub id: u8,
    #[msgpack(default)]
    pub retry: u8,
}

pub fn decode_packet(data: &[u8]) -> zerompk::Result<TestData> {
    zerompk::from_msgpack(data)
}

Error message:

error[E0599]: no method named `to_string` found for reference `&str` in the current scope
 --> src\lib.rs:5:25
  |
5 | #[derive(ToMessagePack, FromMessagePack)]
  |                         ^^^^^^^^^^^^^^^ method not found in `&str`
  |
  = help: items from traits can only be used if the trait is in scope
  = note: this error originates in the derive macro `FromMessagePack` (in Nightly builds, run with -Z macro-backtrace for more info)
help: trait `ToString` which provides `to_string` is implemented but not in scope; perhaps you want to import it
  |
3 + use alloc::string::ToString;
  |

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions