@@ -465,6 +465,33 @@ impl BytesMut {
465465 self . storage . reserve ( additional) ;
466466 }
467467
468+ /// Reserves capacity for inserting additional bytes into the given `BytesMut`.
469+ ///
470+ /// This is equivalent to calling
471+ /// `BytesMut::reserve(capacity - BytesMut::remaining_mut())`.
472+ ///
473+ /// # Panics
474+ ///
475+ /// Panics if new capacity is greater than 60bit for 64bit systems
476+ /// and 28bit for 32bit systems
477+ ///
478+ /// # Examples
479+ ///
480+ /// In the following example, a new buffer is allocated.
481+ ///
482+ /// ```
483+ /// use ntex_bytes::BytesMut;
484+ ///
485+ /// let mut buf = BytesMut::copy_from_slice(&b"hello"[..]);
486+ /// buf.reserve_capacity(128);
487+ /// assert!(buf.capacity() >= 128);
488+ /// assert!(buf.len() >= 5);
489+ /// ```
490+ #[ inline]
491+ pub fn reserve_capacity ( & mut self , cap : usize ) {
492+ self . storage . reserve_capacity ( cap) ;
493+ }
494+
468495 /// Appends given bytes to this object.
469496 ///
470497 /// If this `BytesMut` object has not enough capacity, it is resized first.
@@ -527,7 +554,7 @@ impl Buf for BytesMut {
527554impl BufMut for BytesMut {
528555 #[ inline]
529556 fn remaining_mut ( & self ) -> usize {
530- self . capacity ( ) - self . len ( )
557+ self . storage . remaining ( )
531558 }
532559
533560 #[ inline]
@@ -538,12 +565,10 @@ impl BufMut for BytesMut {
538565
539566 #[ inline]
540567 fn chunk_mut ( & mut self ) -> & mut UninitSlice {
541- let len = self . len ( ) ;
542-
543568 unsafe {
544569 // This will never panic as `len` can never become invalid
545- let ptr = & mut self . storage . as_raw ( ) [ len.. ] ;
546- UninitSlice :: from_raw_parts_mut ( ptr. as_mut_ptr ( ) , self . capacity ( ) - len )
570+ let ptr = & mut self . storage . as_ptr ( ) ;
571+ UninitSlice :: from_raw_parts_mut ( ptr. add ( self . len ( ) ) , self . remaining_mut ( ) )
547572 }
548573 }
549574
@@ -566,7 +591,6 @@ impl BufMut for BytesMut {
566591
567592 #[ inline]
568593 fn put_i8 ( & mut self , n : i8 ) {
569- self . reserve ( 1 ) ;
570594 self . put_u8 ( n as u8 ) ;
571595 }
572596}
@@ -601,11 +625,13 @@ unsafe impl bytes::buf::BufMut for BytesMut {
601625
602626 #[ inline]
603627 fn chunk_mut ( & mut self ) -> & mut bytes:: buf:: UninitSlice {
604- let len = self . len ( ) ;
605628 unsafe {
606629 // This will never panic as `len` can never become invalid
607630 let ptr = self . storage . as_ptr ( ) ;
608- bytes:: buf:: UninitSlice :: from_raw_parts_mut ( ptr. add ( len) , self . capacity ( ) - len)
631+ bytes:: buf:: UninitSlice :: from_raw_parts_mut (
632+ ptr. add ( self . len ( ) ) ,
633+ BufMut :: remaining_mut ( self ) ,
634+ )
609635 }
610636 }
611637
0 commit comments