mirror of
https://github.com/openharmony/third_party_rust_bytes.git
synced 2026-07-21 04:15:24 -04:00
f686ca69643c25d272df4388a1fefa6ca0a9d5e4
advance on Bytes and BytesMut (#166)
* Compact Bytes original capacity representation In order to avoid unnecessary allocations, a `Bytes` structure remembers the capacity with which it was first created. When a reserve operation is issued, this original capacity value is used to as a baseline for reallocating new storage. Previously, this original capacity value was stored in its raw form. In other words, the original capacity `usize` was stored as is. In order to reclaim some `Bytes` internal storage space for additional features, this original capacity value is compressed from requiring 16 bits to 3. To do this, instead of storing the exact original capacity. The original capacity is rounded down to the nearest power of two. If the original capacity is less than 1024, then it is rounded down to zero. This roughly means that the original capacity is now stored as a table: 0 => 0 1 => 1k 2 => 2k 3 => 4k 4 => 8k 5 => 16k 6 => 32k 7 => 64k For the purposes that the original capacity feature was introduced, this is sufficient granularity. * Provide `advance` on Bytes and BytesMut This is the `advance` function that would be part of a `Buf` implementation. However, `Bytes` and `BytesMut` cannot impl `Buf` until the next breaking release. The implementation uses the additional storage made available by the previous commit to store the number of bytes that the view was advanced. The `ptr` pointer will point to the start of the window, avoiding any pointer arithmetic when dereferencing the `Bytes` handle.
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%