Skip to content

Commit e4af486

Browse files
authored
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`.
1 parent 0d4cc7f commit e4af486

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/bytes_mut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ impl BytesMut {
639639

640640
// Update the info
641641
self.ptr = vptr(v.as_mut_ptr().add(off));
642-
self.len = v.len() - off;
643642
self.cap = v.capacity() - off;
643+
debug_assert_eq!(self.len, v.len() - off);
644644
}
645645

646646
return;
@@ -746,8 +746,8 @@ impl BytesMut {
746746
let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC;
747747
self.data = invalid_ptr(data);
748748
self.ptr = vptr(v.as_mut_ptr());
749-
self.len = v.len();
750749
self.cap = v.capacity();
750+
debug_assert_eq!(self.len, v.len());
751751
}
752752

753753
/// Appends given bytes to this `BytesMut`.

0 commit comments

Comments
 (0)