Fix conversion from BytesVec to BytesMut and back (BytesVec::with_bytes_mut())

This commit is contained in:
Nikolay Kim 2022-01-31 15:17:16 +06:00
parent 1baf305b1f
commit 767dd91b7e
6 changed files with 13 additions and 6 deletions

View file

@ -1,5 +1,9 @@
# Changes
## [0.1.11] (2022-01-31)
* Fix conversion from BytesVec to BytesMut and back (BytesVec::with_bytes_mut())
## [0.1.11] (2022-01-30)
* Add BytesVec type

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-bytes"
version = "0.1.11"
version = "0.1.12"
license = "MIT"
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Carl Lerche <me@carllerche.com>"]
description = "Types and traits for working with bytes (bytes crate fork)"

View file

@ -2730,7 +2730,9 @@ impl InnerVec {
)
} else if kind == KIND_VEC {
let ptr = buf.inner.shared_vec();
let offset = buf.inner.arc.as_ptr() as usize - ptr as usize;
let offset = buf.inner.ptr as usize - ptr as usize;
// we cannot use shared vec if BytesMut points to inside of vec
if buf.inner.cap < (*ptr).cap - offset {
InnerVec::from_slice(
buf.inner.capacity(),
@ -2738,11 +2740,11 @@ impl InnerVec {
buf.inner.pool(),
)
} else {
// BytesMut owns rest of the vec, so re-use
(*ptr).len = buf.len() as u32;
(*ptr).offset = offset as u32;
let inner = InnerVec(NonNull::new_unchecked(ptr));
// reuse bytes
mem::forget(buf);
mem::forget(buf); // reuse bytes
inner
}
} else {

View file

@ -824,6 +824,7 @@ fn bytes_vec_freeze() {
fn bytes_vec() {
let mut bytes = BytesVec::copy_from_slice(LONG);
bytes.with_bytes_mut(|buf| {
assert_eq!(buf, LONG);
assert_eq!(buf.split_to(4), &LONG[..4]);
});
assert_eq!(bytes, &LONG[4..]);

View file

@ -16,4 +16,4 @@ name = "ntex_codec"
path = "src/lib.rs"
[dependencies]
ntex-bytes = "0.1.11"
ntex-bytes = "0.1.12"

View file

@ -53,7 +53,7 @@ ntex-router = "0.5.1"
ntex-service = "0.3.1"
ntex-macros = "0.1.3"
ntex-util = "0.1.13"
ntex-bytes = "0.1.11"
ntex-bytes = "0.1.12"
ntex-tls = "0.1.3"
ntex-rt = "0.4.3"
ntex-io = "0.1.7"