Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with the bytes crates #1444

Open
dignifiedquire opened this issue Jun 21, 2024 · 6 comments
Open

Integration with the bytes crates #1444

dignifiedquire opened this issue Jun 21, 2024 · 6 comments
Labels
customer-request Documents customer requests.

Comments

@dignifiedquire
Copy link

dignifiedquire commented Jun 21, 2024

A lot of my networking code uses 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.

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<Bytes, UdpHeader>,
    body: Bytes,
}

impl UdpPacket {
    fn parse(bytes: Bytes) -> Option<UdpPacket> {
        let (header, body) = Ref::new_from_prefix(bytes)?;
        Some(UdpPacket { header, body })
    }
}
@dignifiedquire dignifiedquire added the customer-request Documents customer requests. label Jun 21, 2024
@joshlf
Copy link
Member

joshlf commented Jun 21, 2024

We'll have a chance to look at this issue in more detail later, but briefly off the top of my head: check out how we handle this in 0.8. The ByteSlice trait has been split up, and we've made the resulting traits unsealed so that external crates can implement them.

@dignifiedquire
Copy link
Author

and we've made the resulting traits unsealed so that external crates can implement them.

Unfortunately that doesn't help, as the orphan rule prevents us from implementing the trait for an external type :/

@matheus23
Copy link

It may be possible to workaround this via a newtype to experiment with this as a solution.

@somethingelseentirely
Copy link

somethingelseentirely commented Sep 18, 2024

The prevalence of bytes in the Rust ecosystem is extremely unfortunate, because the set of byte sources that it can operate over is essentially closed. The extensible approach used by minibytes from facebooks Sapling CMS and ownedbytes from the Tantivy fulltext search engine, is much better imho.

I've forked the somewhat dead minibytes into a new crate called anybytes.
It's still in early development but you might find the Packed types interesting. They provide read-only zerocopy views for scalars, slices and string over arbitrary byte sources (including bytes::bytes, ownedbytes::Bytes, Vec<T> where T: AsBytes, Box<T> where T: AsBytes, and memmap2::Mmap). Especially the latter are nice, because it allows you to not only read some (e.g.) memory mapped file as a PackedSlice<T>, but you can also just turn any existing Vec<T> into a Bytes/PackedSlice<T>.

joshlf added a commit to joshlf/bytes that referenced this issue Oct 11, 2024
joshlf added a commit to joshlf/bytes that referenced this issue Oct 11, 2024
@joshlf
Copy link
Member

joshlf commented Oct 11, 2024

Filed upstream: tokio-rs/bytes#738

@dignifiedquire
Copy link
Author

Looks like the last missing piece was published in 1.8: tokio-rs/bytes#741

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-request Documents customer requests.
Projects
None yet
Development

No branches or pull requests

4 participants