Unable to derive zerocopy::AsBytes on Vec<T> for struct T

31 Views Asked by At

I'm trying to derive the zerocopy as_bytes() function on the struct Cycle below.


#![cfg_attr(not(test), no_std)]

extern crate alloc;
use alloc::vec::Vec;
use risc0_zkp::core::digest::Digest;
use serde::{Deserialize, Serialize};
use zerocopy::AsBytes;

pub type Address = [u8; 20];

#[derive(AsBytes, Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[repr(C)]
pub struct Obligation {
    pub from: Address,
    pub to: Address,
    pub value: u8,
    pub salt: [u8; 32],
}

#[derive(AsBytes, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[repr(packed)]
pub struct Cycle {
    pub obligations: Vec<Obligation>,
    pub setoff: u8,
}

The error is as follows.

error[E0277]: the trait bound `Vec<u8>: AsBytes` is not satisfied
  --> core/src/lib.rs:37:10
   |
37 | #[derive(AsBytes, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
   |          ^^^^^^^ the trait `AsBytes` is not implemented for `Vec<Obligation>`
   |
   = help: the following other types implement trait `AsBytes`:
             bool
             char
             isize
             i8
             i16
             i32
             i64
             i128
           and 52 others
   = help: see issue #48214
   = note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `mtcs-core` (lib) due to 2 previous errors

rust-compilation exited abnormally with code 101 at Sat Mar 30 01:35:02

I've tried to look for workarounds, but couldn't find any. Is this something to do with the large alignment of Vec (=8)? I even tried using arrayvec to have upper bounds on how large the Vec could get, but this error remains.

Writing a custom to_bytes function in impl Cycle was causing a whole new set of problems so I think this approach would work out best finally.

In case what I'm trying to do is impossible for some reason, please suggest alternative approaches.

0

There are 0 best solutions below