Skip to content

Unable to use generics after updating to 0.8 from 0.7 #2302

@schultetwin1

Description

@schultetwin1

I am attempting to update my codebase to use zerocopy 0.8 for all the wins we get! However, I'm stuck on a build break that I'm having a hard time understanding.

Here is a simplified version of the code causing the issue:

use zerocopy::{FromBytes, Immutable, KnownLayout, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};

#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
#[repr(C, packed)]
struct Header {
    magic: u32,
    size: u32,
}

#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
#[repr(C, packed)]
struct Packet<P: PacketPayload> {
    header: Header,
    payload: P
}

trait PacketPayload: IntoBytes + FromBytes + KnownLayout + Immutable {}

#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
#[repr(C, packed)]
struct SimplePayload {
    name: [u8; 32]
}

impl PacketPayload for SimplePayload {}


fn main() {
    let packet = Packet {
        header: Header {
            magic: 0,
            size: 0
        },
        payload: SimplePayload {
            name: [0; 32]
        }
    };
    let bytes = packet.as_bytes();
    println!("Bytes: {bytes:?}");
}

This is the compilation error I'm seeing:

error[E0277]: the size for values of type `<P as KnownLayout>::MaybeUninit` cannot be known at compilation time
  --> src/main.rs:11:32
   |
11 | #[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
   |                                ^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `<P as KnownLayout>::MaybeUninit`
   = note: the last field of a packed struct may only have a dynamically sized type if it does not need drop to be run
   = help: change the field's type to have a statically known size
   = note: this error originates in the derive macro `KnownLayout` (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 `zerocopy_test` (bin "zerocopy_test") due to 1 previous error

The equivalent (in my mind) version of this code compiles in zerocopy 0.7:

use zerocopy::{FromBytes, FromZeroes, AsBytes};
use zerocopy_derive::{FromBytes, FromZeroes, AsBytes};

#[derive(AsBytes, FromBytes, FromZeroes)]
#[repr(C, packed)]
struct Header {
    magic: u32,
    size: u32,
}

#[derive(AsBytes, FromBytes, FromZeroes)]
#[repr(C, packed)]
struct Packet<P: PacketPayload> {
    header: Header,
    payload: P
}

trait PacketPayload: AsBytes + FromBytes + FromZeroes {}

#[derive(AsBytes, FromBytes, FromZeroes)]
#[repr(C, packed)]
struct SimplePayload {
    name: [u8; 32]
}

impl PacketPayload for SimplePayload {}


fn main() {
    let packet = Packet {
        header: Header {
            magic: 0,
            size: 0
        },
        payload: SimplePayload {
            name: [0; 32]
        }
    };
    let bytes = packet.as_bytes();
    println!("Bytes: {bytes:?}");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions