Cbor deserialize big array

hello, i am new to rust.

i have tried with serde_cbor to deserialise into a struct that contains variable of [u8; 64].
if i use [u8;32] it compiles (does not work cause i am expecting hash of type &[u8; 64]).

any ideas how to implement deserialise ??

i also tryied with minicbor and i am not sure how to implement a deserialise for my struct type. so any help on that will be great

i saw some solutions for "big array" deserialisetion but for some reason the do not compile (can't find crate for 'serde_derive' or if i add this a pth ../serde in the tomel i get an error that failed to load serde. any ideas on that will also be welcomed :slight_smile:

For now, let’s assume the other solutions you’ve found will work, once you’re including serde_derive. I don’t know what your serde dependency looks like in your cargo.toml, but unless you’ve got some special stuff enabled, I suspect it looks like this:

serde = { version = "YourVersionHere” }

I think, looking at the documentation, that you can add serde_derive into your serde build if you change that as follows:

serde = { version = "YourVersionHere", features = [“derive”] }

Might I suggest you give that a try and see if that allows your other solutions to compile? Alternatively, serde_derive is a crate in itself, so you could try adding that itself. See: https://crates.io/crates/serde_derive.

If that doesn’t work, let me know.

i found out this :

which helped, but unfortunately my type in the struct which i am trying to deserialise to is actualy a reference
&[u8; 64]
so i try to use
#[derive(Serialize, Deserialize, Debug)]
struct Mascot {
slot: u8,
version: String,
#[serde(with = "serde_bytes")]
hash: &'static [u8; 64],
.. }
but i get
serde bytes::Deserialize<'_>is not implemented for&[u8; 64]
do i am not sure what to do right now .. maybe a box instead of &'static, not sure if that will work
any ideas ??

You can use ``` to create code blocks, which will keep formatting such as indentation and will make your post much more readable.

use serde_with::*;
#[derive(Serialize, Deserialize, Debug)]
struct Mascot {
    #[serde_as(as = "Bytes")]
    hash: &'static [u8; 64],

This code above compiles for me. But it is probably not what you want. The 'static lifetime makes it such that you can only deserialize from things with a 'static lifetime themselves. This mean you either need to leak data or can only use string/byte literals.

You can introduce a lifetime to Mascot or put the array inside a Box or pointer like Rc/Arc.

struct Mascot<'a> {
    hash: &'a [u8; 64],
1 Like

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.