@@ -94,15 +94,15 @@ impl BytesVec {
9494
9595 /// Creates a new `BytesVec` from slice, by copying it.
9696 pub fn copy_from_slice < T : AsRef < [ u8 ] > > ( src : T ) -> Self {
97+ let slice = src. as_ref ( ) ;
9798 BytesVec {
98- storage : StorageVec :: from_slice ( src . as_ref ( ) ) ,
99+ storage : StorageVec :: from_slice ( slice . len ( ) , slice ) ,
99100 }
100101 }
101102
102103 /// Creates a new `BytesVec` with default capacity.
103104 ///
104105 /// Resulting object has length 0 and unspecified capacity.
105- /// This function does not allocate.
106106 ///
107107 /// # Examples
108108 ///
@@ -121,7 +121,7 @@ impl BytesVec {
121121 #[ inline]
122122 pub fn new ( ) -> BytesVec {
123123 BytesVec {
124- storage : StorageVec :: with_capacity ( 0 ) ,
124+ storage : StorageVec :: with_capacity ( 104 ) ,
125125 }
126126 }
127127
@@ -202,7 +202,7 @@ impl BytesVec {
202202 }
203203
204204 /// Removes the bytes from the current view, returning them in a new
205- /// `Bytes ` instance.
205+ /// `BytesMut ` instance.
206206 ///
207207 /// Afterwards, `self` will be empty, but will retain any additional
208208 /// capacity that it had before the operation. This is identical to
@@ -219,15 +219,47 @@ impl BytesVec {
219219 /// let mut buf = BytesVec::with_capacity(1024);
220220 /// buf.put(&b"hello world"[..]);
221221 ///
222- /// let other = buf.split ();
222+ /// let other = buf.take ();
223223 ///
224224 /// assert!(buf.is_empty());
225225 /// assert_eq!(1013, buf.capacity());
226226 ///
227227 /// assert_eq!(other, b"hello world"[..]);
228228 /// ```
229+ #[ inline]
230+ pub fn take ( & mut self ) -> BytesMut {
231+ BytesMut {
232+ storage : self . storage . split_to ( self . len ( ) ) ,
233+ }
234+ }
235+
236+ /// Removes the bytes from the current view, returning them in a new
237+ /// `Bytes` handle.
238+ ///
239+ /// This is identical to `self.take().freeze()`.
240+ ///
241+ /// # Examples
242+ ///
243+ /// ```
244+ /// use ntex_bytes::{BytesMut, BufMut};
245+ ///
246+ /// let mut buf = BytesMut::with_capacity(1024);
247+ /// buf.put(&b"hello world"[..]);
248+ ///
249+ /// let other = buf.take_bytes();
250+ ///
251+ /// assert_eq!(other, b"hello world"[..]);
252+ /// ```
253+ pub fn take_bytes ( & mut self ) -> Bytes {
254+ Bytes {
255+ storage : self . storage . split_to ( self . len ( ) ) ,
256+ }
257+ }
258+
259+ #[ doc( hidden) ]
260+ #[ deprecated]
229261 pub fn split ( & mut self ) -> BytesMut {
230- self . split_to ( self . len ( ) )
262+ self . take ( )
231263 }
232264
233265 /// Splits the buffer into two at the given index.
@@ -255,18 +287,76 @@ impl BytesVec {
255287 /// # Panics
256288 ///
257289 /// Panics if `at > len`.
290+ #[ inline]
258291 pub fn split_to ( & mut self , at : usize ) -> BytesMut {
259292 self . split_to_checked ( at)
260293 . expect ( "at value must be <= self.len()`" )
261294 }
262295
296+ /// Splits the buffer into two at the given index.
297+ ///
298+ /// Same as .split_to() but returns `Bytes` instance.
299+ ///
300+ /// # Examples
301+ ///
302+ /// ```
303+ /// use ntex_bytes::BytesVec;
304+ ///
305+ /// let mut a = BytesVec::copy_from_slice(&b"hello world"[..]);
306+ /// let mut b = a.split_to_bytes(5);
307+ ///
308+ /// a[0] = b'!';
309+ ///
310+ /// assert_eq!(&a[..], b"!world");
311+ /// assert_eq!(&b[..], b"hello");
312+ /// ```
313+ ///
314+ /// # Panics
315+ ///
316+ /// Panics if `at > len`.
317+ #[ inline]
318+ pub fn split_to_bytes ( & mut self , at : usize ) -> Bytes {
319+ Bytes {
320+ storage : self . split_to ( at) . storage ,
321+ }
322+ }
323+
324+ /// Advance the internal cursor.
325+ ///
326+ /// Afterwards `self` contains elements `[cnt, len)`.
327+ /// This is an `O(1)` operation.
328+ ///
329+ /// # Examples
330+ ///
331+ /// ```
332+ /// use ntex_bytes::BytesVec;
333+ ///
334+ /// let mut a = BytesVec::copy_from_slice(&b"hello world"[..]);
335+ /// a.advance_to(5);
336+ ///
337+ /// a[0] = b'!';
338+ ///
339+ /// assert_eq!(&a[..], b"!world");
340+ /// ```
341+ ///
342+ /// # Panics
343+ ///
344+ /// Panics if `cnt > len`.
345+ #[ inline]
346+ pub fn advance_to ( & mut self , cnt : usize ) {
347+ unsafe {
348+ self . storage . set_start ( cnt as u32 ) ;
349+ }
350+ }
351+
263352 /// Splits the bytes into two at the given index.
264353 ///
265354 /// Does nothing if `at > len`.
355+ #[ inline]
266356 pub fn split_to_checked ( & mut self , at : usize ) -> Option < BytesMut > {
267357 if at <= self . len ( ) {
268358 Some ( BytesMut {
269- storage : self . storage . split_to ( at, false ) ,
359+ storage : self . storage . split_to ( at) ,
270360 } )
271361 } else {
272362 None
@@ -293,6 +383,7 @@ impl BytesVec {
293383 /// ```
294384 ///
295385 /// [`split_off`]: #method.split_off
386+ #[ inline]
296387 pub fn truncate ( & mut self , len : usize ) {
297388 self . storage . truncate ( len) ;
298389 }
@@ -308,6 +399,7 @@ impl BytesVec {
308399 /// buf.clear();
309400 /// assert!(buf.is_empty());
310401 /// ```
402+ #[ inline]
311403 pub fn clear ( & mut self ) {
312404 self . truncate ( 0 ) ;
313405 }
@@ -419,7 +511,7 @@ impl BytesVec {
419511 /// buf.put(&[0; 64][..]);
420512 ///
421513 /// let ptr = buf.as_ptr();
422- /// let other = buf.split ();
514+ /// let other = buf.take ();
423515 ///
424516 /// assert!(buf.is_empty());
425517 /// assert_eq!(buf.capacity(), 64);
@@ -503,15 +595,7 @@ impl Buf for BytesVec {
503595
504596 #[ inline]
505597 fn advance ( & mut self , cnt : usize ) {
506- assert ! (
507- cnt <= self . storage. len( ) ,
508- "cannot advance past `remaining` len:{} delta:{}" ,
509- self . storage. len( ) ,
510- cnt
511- ) ;
512- unsafe {
513- self . storage . set_start ( cnt as u32 ) ;
514- }
598+ self . advance_to ( cnt) ;
515599 }
516600}
517601
@@ -575,7 +659,7 @@ impl bytes::buf::Buf for BytesVec {
575659
576660 #[ inline]
577661 fn advance ( & mut self , cnt : usize ) {
578- Buf :: advance ( self , cnt)
662+ self . advance_to ( cnt) ;
579663 }
580664}
581665
@@ -760,7 +844,10 @@ impl Extend<u8> for BytesVec {
760844 let ( lower, _) = iter. size_hint ( ) ;
761845 self . reserve ( lower) ;
762846
763- for b in iter {
847+ for ( idx, b) in iter. enumerate ( ) {
848+ if idx >= lower {
849+ self . reserve ( 1 ) ;
850+ }
764851 self . put_u8 ( b) ;
765852 }
766853 }
@@ -913,7 +1000,7 @@ impl From<BytesVec> for BytesMut {
9131000 #[ inline]
9141001 fn from ( src : BytesVec ) -> BytesMut {
9151002 BytesMut {
916- storage : src. storage . into_storage ( ) ,
1003+ storage : src. storage . freeze ( ) ,
9171004 }
9181005 }
9191006}
0 commit comments