google / google/zerocopy

Integration with the `bytes` crates

Open
#1,444 6 comments 0 reactions 0 assignees View on GitHub
customer-request
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

A lot of my networking code uses [`bytes`](https://docs.rs/bytes/latest/bytes/) to handle efficient storing of incoming and outgoing packets. I would like to transition more of that code to use `zerocopy`, but for that some integration would be required.

The following example is for the parsing side of what I would like to do, but this fails as `ByteSlice` is not implemented for `bytes::Bytes` or `bytes::BytesMut`.

I can't implement this in my own crate, as both the type and the trait are in 3rd party crates.

```rust
use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeroes, Ref, Unaligned};
use bytes::Bytes;

#[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
#[repr(C)]
struct UdpHeader {
src_port: [u8; 2],
dst_port: [u8; 2],
length: [u8; 2],
checksum: [u8; 2],
}

struct UdpPacket {
header: Ref,
body: Bytes,
}

impl UdpPacket {
fn parse(bytes: Bytes) -> Option {
let (header, body) = Ref::new_from_prefix(bytes)?;
Some(UdpPacket { header, body })
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.