diff options
author | 2020-01-22 16:37:53 -0800 | |
---|---|---|
committer | 2020-01-22 16:37:53 -0800 | |
commit | e0eebde9938d03eb60fe97c4a41f464e4c83d414 (patch) | |
tree | 60c04481aa9ad492d8b3f7bd425bdc06372fb5ee /src | |
parent | 729bc7c2084a42fda2c62da6933951fa7ac875aa (diff) | |
download | bytes-e0eebde9938d03eb60fe97c4a41f464e4c83d414.tar.gz bytes-e0eebde9938d03eb60fe97c4a41f464e4c83d414.tar.zst bytes-e0eebde9938d03eb60fe97c4a41f464e4c83d414.zip |
Fix Bytes::truncate losing the original Vec's capacity (#361)
Diffstat (limited to 'src')
-rw-r--r-- | src/bytes.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/bytes.rs b/src/bytes.rs index 93ab84b..8ecc853 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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; + } } } |