mirror of
https://github.com/openharmony/third_party_rust_bytes.git
synced 2026-07-20 20:06:46 -04:00
7110d57b2f0bc1e4dc21988f2e395ba1478eb230
Round up to power of 2 is not necessary, because `reserve` already
doubles previous capacity in
```
new_cap = cmp::max(
cmp::max(v.capacity() << 1, new_cap),
original_capacity);
```
which makes `reserve` calls constant in average. Avoiding rounding
up prevents `reserve` from wasting space when caller knows exactly
what space they need.
Patch adds three tests which would fail before this test. The most
important is this:
```
#[test]
fn reserve_in_arc_unique_does_not_overallocate() {
let mut bytes = BytesMut::with_capacity(1000);
bytes.take();
// now bytes is Arc and refcount == 1
assert_eq!(1000, bytes.capacity());
bytes.reserve(2001);
assert_eq!(2001, bytes.capacity());
}
```
It asserts that when user requests more than double of current
capacity, exactly the requested amount of memory is allocated and
is not wasted to next power of two.
Bytes
A utility library for working with bytes.
Usage
To use bytes, first add this to your Cargo.toml:
[dependencies]
bytes = "0.4"
Next, add this to your crate:
extern crate bytes;
use bytes::{Bytes, BytesMut, Buf, BufMut};
Serde support
Serde support is optional and disabled by default. To enable use the feature serde.
[dependencies]
bytes = { version = "0.4", features = ["serde"] }
License
bytes is primarily distributed under the terms of both the MIT license and the
Apache License (Version 2.0), with portions covered by various BSD-like
licenses.
See LICENSE-APACHE, and LICENSE-MIT for details.
Languages
Rust
99.3%
Shell
0.7%