From 2c27ddaf7e860424071583bcf5913bcdf43e9637 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 12 Mar 2018 17:51:10 +0100 Subject: [PATCH] Introduce Bytes::to_mut (#188) --- src/bytes.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/bytes.rs b/src/bytes.rs index 50eaac6..60cf5d4 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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.