Skip to content

Consider an unsplit method for Bytes #503

Open
@eloff

Description

I was looking for a way to join two contiguous Bytes into one, but there isn't such a thing.

It's common in parsing code to read multiple logical messages into a single BytesMut, split them off into individual Bytes for processing, but then it if they need to be written somewhere it can be advantageous to recombine them, if possible, so there are fewer syscalls to write. It's also possible to just use writev, but that typically involves an allocation for the iovec array and not all platforms support writev, and it's not an option if you're using a TLS wrapper around the stream. Using writev also requires much more code.

I think it is possible to do this safely:

impl Bytes {
    pub fn unsplit(self, other: Self) -> (Option<Self>, Option<Self>) {
        if is_contiguous(&self, &other) {
            return Some(merge(self, other)), None
        }
       (Some(self), Some(other))
    }
}

Does this seem like a worthwhile addition to the library?

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