Fix Bytes::truncate losing the original Vec's capacity (#361)

This commit is contained in:
Sean McArthur
2020-01-22 16:37:53 -08:00
committed by Carl Lerche
parent 729bc7c208
commit e0eebde993
3 changed files with 85 additions and 2 deletions
+9 -1
View File
@@ -418,7 +418,15 @@ impl Bytes {
#[inline]
pub fn truncate(&mut self, len: usize) {
if len < self.len {
self.len = len;
// The Vec "promotable" vtables do not store the capacity,
// so we cannot truncate while using this repr. We *have* to
// promote using `split_off` so the capacity can be stored.
if self.vtable as *const Vtable == &PROMOTABLE_EVEN_VTABLE ||
self.vtable as *const Vtable == &PROMOTABLE_ODD_VTABLE {
drop(self.split_off(len));
} else {
self.len = len;
}
}
}