Skip to content

Commit 6af196c

Browse files
authored
Add BytesMut::reserve_capacity() method (#720)
* Optimize metadata repr * Add BytesMut::reserve_capacity() method
1 parent 9c20884 commit 6af196c

File tree

20 files changed

+182
-102
lines changed

20 files changed

+182
-102
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ ntex-util = { path = "ntex-util" }
4141

4242
[workspace.dependencies]
4343
ntex = "3.0.0-pre.11"
44-
ntex-bytes = "1.3.0"
44+
ntex-bytes = "1.4.0"
4545
ntex-codec = "1.1.0"
46-
ntex-io = "3.4.0"
46+
ntex-io = "3.5.0"
4747
ntex-net = "3.5.1"
4848
ntex-http = "1.0.0"
4949
ntex-router = "1.0.0"

ntex-bytes/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changes
22

3+
## [1.4.0] (2026-01-21)
4+
5+
* Add BytesMut::reserve_capacity() method
6+
7+
* Optimize metadata repr
8+
39
## [1.3.0] (2026-01-17)
410

511
* Replace BytesMut with BytesVec

ntex-bytes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-bytes"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
55
description = "Types and traits for working with bytes (bytes crate fork)"
66
documentation = "https://docs.rs/ntex-bytes"

ntex-bytes/src/buf/buf_impl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,11 @@ mod tests {
911911
buf.copy_to_slice(&mut dst);
912912
assert_eq!(&b"hello"[..], &dst);
913913
assert_eq!(6, buf.remaining());
914+
buf.advance(1);
915+
assert_eq!(Buf::chunk(&buf), &b"world"[..]);
914916

917+
let mut buf = &b"hello world"[..];
918+
buf.advance(5);
915919
let mut buf = Box::new(buf);
916920
assert_eq!(buf.remaining(), 6);
917921
assert_eq!(buf.chunk(), b" world");

ntex-bytes/src/buf/buf_mut.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,11 @@ pub trait BufMut {
180180
assert!(self.remaining_mut() >= src.remaining());
181181

182182
while src.has_remaining() {
183-
let l;
183+
let s = src.chunk();
184+
let d = self.chunk_mut();
185+
let l = cmp::min(s.len(), d.len());
184186

185187
unsafe {
186-
let s = src.chunk();
187-
let d = self.chunk_mut();
188-
l = cmp::min(s.len(), d.len());
189-
190188
ptr::copy_nonoverlapping(s.as_ptr(), d.as_mut_ptr(), l);
191189
}
192190

ntex-bytes/src/bvec.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
527554
impl 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

ntex-bytes/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub use crate::bvec::BytesMut;
6868
pub use crate::bytes::Bytes;
6969
pub use crate::string::ByteString;
7070

71+
#[doc(hidden)]
72+
pub use crate::storage::METADATA_SIZE;
73+
7174
#[doc(hidden)]
7275
#[deprecated]
7376
pub type BytesVec = BytesMut;

0 commit comments

Comments
 (0)