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

Feature request: fallible version of BytesMut::unsplit (i.e. make BytesMut::try_unsplit public) #702

Open
aochagavia opened this issue Apr 30, 2024 · 2 comments

Comments

@aochagavia
Copy link

I'm using BytesMut::unsplit in a project and would like to raise an error when the bytes cannot be unsplitted, instead of silently copying. An Err(_) would let me know I messed up somewhere, so I can go fix the problem.

Reading the source code, I came across try_unsplit, which seems to do exactly what I need, but is private. Have you considered making it public? Or is there a suitable workaround for people like me, who want to guarantee that no data is ever copied from one BytesMut to the other?

In case it's relevant, below is the code for try_unsplit that I think could be made public:

bytes/src/bytes_mut.rs

Lines 908 to 926 in cb7f844

fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
if other.capacity() == 0 {
return Ok(());
}
let ptr = unsafe { self.ptr.as_ptr().add(self.len) };
if ptr == other.ptr.as_ptr()
&& self.kind() == KIND_ARC
&& other.kind() == KIND_ARC
&& self.data == other.data
{
// Contiguous blocks, just combine directly
self.len += other.len;
self.cap += other.cap;
Ok(())
} else {
Err(other)
}
}

@Darksonn
Copy link
Contributor

Duplicate of #287.

@aochagavia
Copy link
Author

Just in case: the linked issue seems to be about Bytes, so I assumed it didn't apply to BytesMut.

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

No branches or pull requests

2 participants