-
Notifications
You must be signed in to change notification settings - Fork 287
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
Bytes: Add try_into_mut. #687
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,7 +15,7 @@ use crate::buf::IntoIter; | |||||||||
#[allow(unused)] | ||||||||||
use crate::loom::sync::atomic::AtomicMut; | ||||||||||
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; | ||||||||||
use crate::Buf; | ||||||||||
use crate::{Buf, BytesMut}; | ||||||||||
|
||||||||||
/// A cheaply cloneable and sliceable chunk of contiguous memory. | ||||||||||
/// | ||||||||||
|
@@ -507,6 +507,28 @@ impl Bytes { | |||||||||
self.truncate(0); | ||||||||||
} | ||||||||||
|
||||||||||
/// Try to convert self into `BytesMut`. | ||||||||||
/// | ||||||||||
/// If `self` is unique, this will succeed and return a `BytesMut` with | ||||||||||
/// the contents of `self` without copying. If `self` is not unique, this | ||||||||||
/// will fail and return self. | ||||||||||
/// | ||||||||||
/// # Examples | ||||||||||
/// | ||||||||||
/// ``` | ||||||||||
/// use bytes::{Bytes, BytesMut}; | ||||||||||
/// | ||||||||||
/// let bytes = Bytes::from(b"hello".to_vec()); | ||||||||||
/// assert_eq!(bytes.try_into_mut(), Ok(BytesMut::from(&b"hello"[..]))); | ||||||||||
/// ``` | ||||||||||
pub fn try_into_mut(self) -> std::result::Result<BytesMut, Bytes> { | ||||||||||
if self.is_unique() { | ||||||||||
Ok(BytesMut::from_vec(self.into())) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this does avoid allocating a new vector, it can still result in a copy of the data when it's not stored at the beginning of the allocation. For example, this happens here: Lines 996 to 997 in 4eb62b9
and here: Lines 1127 to 1128 in 4eb62b9
Actually avoiding the copy will be a more involved change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably unavoidable right? Or are the slices of bytes aware of their parents? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha I read the code and I understand, so essentially if the user did a slice, dropped the other references and then calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, exactly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-reading this, |
||||||||||
} else { | ||||||||||
Err(self) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
#[inline] | ||||||||||
pub(crate) unsafe fn with_vtable( | ||||||||||
ptr: *const u8, | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This use of
std
fails in builds without the standard library.