Don't set len in BytesMut::reserve (#682)

A fundamental invariant of `reserve` is that it can extend capacity
while the stored data remains the same, even if it's moved to a new
allocation. As a result, `len` can never change during a call to
`reserve`.
This commit is contained in:
Brad Dunbar
2024-04-09 14:35:54 +02:00
committed by GitHub
parent 0d4cc7ffed
commit e4af48633c
+2 -2
View File
@@ -639,8 +639,8 @@ impl BytesMut {
// Update the info // Update the info
self.ptr = vptr(v.as_mut_ptr().add(off)); self.ptr = vptr(v.as_mut_ptr().add(off));
self.len = v.len() - off;
self.cap = v.capacity() - off; self.cap = v.capacity() - off;
debug_assert_eq!(self.len, v.len() - off);
} }
return; return;
@@ -746,8 +746,8 @@ impl BytesMut {
let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC; let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC;
self.data = invalid_ptr(data); self.data = invalid_ptr(data);
self.ptr = vptr(v.as_mut_ptr()); self.ptr = vptr(v.as_mut_ptr());
self.len = v.len();
self.cap = v.capacity(); self.cap = v.capacity();
debug_assert_eq!(self.len, v.len());
} }
/// Appends given bytes to this `BytesMut`. /// Appends given bytes to this `BytesMut`.