Serialize trait issue on struct with a long array member

Hello guys, I have a problem with the Serialize trait and the following structure:

#[repr(C)]
#[derive(Default, AsBytes, FromZeroes, FromBytes, Debug, Serialize)]
struct MyMsg {
    cmd : u32,
    fpath : [u8,64],
}

The compiler complains that:

the trait Serialize is not implemented for [u8; 64]

How can I solve this issue ?

I noticed that if I declare

fpath : [u8;32]

the problem is no longer present, btw I need the member to have 32 elements.

One other solution is to use serde_bytes: serde_bytes - Rust (docs.rs) but I would prefer avoid adding another crate just for this if it's possible.

Thanks and have a nice day

F

#[repr(C)]
#[derive(Debug, Serialize)]
struct MyMsg {
    cmd : u32,
    #[serde(serialize_with = "<[u8]>::serialize")]
    fpath : [u8; 64],
}

A similar problem occurs with Default, but you can just implement that yourself easily.

2 Likes

This is an incorrect syntax. , is for separating values, and in a struct you declare types. ; is required here.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.