Skip to content

WritableToBytes could be replaced by zerocopy::AsBytes #975

Open

Description

@joshlf

The WritableToBytes trait is an unsafe marker trait that is used to indicate that a type's underlying bytes may safely be accessed as a sequence of bytes. This trait appears to perform the same function as zerocopy::AsBytes. zerocopy is a crate dedicated to these sorts of memory access operations, and the AsBytes trait provides a custom derive, allowing code like the following:

#[derive(AsBytes)]
#[repr(C)]
struct Good {
    a: u16,
    b: u16,
}

#[derive(AsBytes)]    // Won't compile because of padding byte between `a` and `b`
#[repr(C)]
struct Bad {
    a: u8,
    b: u16,
}

It might be worth considering replacing WritableToBytes with AsBytes.

Disclosure: I'm the author of the zerocopy crate.


This requires submitting a proper patch to the LKML and the Rust for Linux mailing list. Please recall to test your changes (including generating the documentation if changed, running the Rust doctests if changed, etc.), to justify the commit well in the message ("what" & "why"), to use a proper title for the commit, to sign your commit under the Developer's Certificate of Origin and to add a Suggested-by: tag, and a Link: tag to this issue. Please see https://docs.kernel.org/process/submitting-patches.html and https://rust-for-linux.com/contributing for details.

Activity

  1. nbdd0121 commented on Feb 23, 2023

    @nbdd0121
    Member

    We currently don't use/vendor third-party crates.

  2. joshlf commented on Feb 23, 2023

    @joshlf
    Author

    Ah gotcha. Feel free to close this then.

  3. ojeda commented on Feb 23, 2023

    @ojeda
    Member

    In any case, thanks for contacting us and creating the issue -- if we start to vendor third-party crates in the future (which is not impossible -- there have been some discussions about it, but it will take time), then we will revisit this one as well as other crates.

  4. added
    unsoundThe possibility of UB in safe code.
    and removed
    unsoundThe possibility of UB in safe code.
    on Feb 23, 2023
  5. domenpk commented on May 23, 2023

    @domenpk

    I like the approach, since the trait's user apparently can't misuse it, and there is also no unsafe for safe code.

  6. added
    easyExpected to be an easy issue to resolve.
    mediumExpected to be an issue of medium difficulty to resolve.
    on Jun 8, 2026
  7. ojeda commented on Jun 8, 2026

    @ojeda
    Member

    We ended up with zerocopy in the tree and:

    • WritableToBytes got renamed to AsBytes to match zerocopy back then.
    • zerocopy 0.8.0 renamed AsBytes to IntoBytes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyExpected to be an easy issue to resolve.good first issueGood for newcomersmediumExpected to be an issue of medium difficulty to resolve.• libRelated to the `rust/` library.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions