Introduce Bytes::to_mut (#188)

This commit is contained in:
Anthony Ramine
2018-03-12 17:51:10 +01:00
committed by Carl Lerche
parent 808f2fb8ec
commit fecf36f1c7
+11
View File
@@ -763,6 +763,17 @@ impl Bytes {
}
}
/// Acquires a mutable reference to the owned form of the data.
///
/// Clones the data if it is not already owned.
pub fn to_mut(&mut self) -> &mut BytesMut {
if !self.inner.is_mut_safe() {
let new = Bytes::from(&self[..]);
*self = new;
}
unsafe { &mut *(self as *mut Bytes as *mut BytesMut) }
}
/// Appends given bytes to this object.
///
/// If this `Bytes` object has not enough capacity, it is resized first.