cowbytes
A clone-on-write bytes type whose non-borrowed variant is bytes::Bytes.
Unlike Cow<'a, [u8]>, whose owned side is a Vec<u8>, cloning and slicing a shared value are reference count operations rather than copies.
Unlike Bytes, which can only borrow &'static data, an arbitrary slice can be held without copying it, at the cost of a lifetime.
Example
use CowBytes;
// Borrowing does not allocate.
let borrowed: = from;
// A `Vec` is adopted without copying.
let shared: = from;
// Equality is by contents, not by variant.
assert_eq!;
// Slicing shared bytes keeps the same allocation.
let sliced: = shared.slice;
assert_eq!;
// `into_vec` hands the allocation back while it is unshared, rather than copying.
let bytes = vec!;
let ptr = bytes.as_ptr;
let owned: = from.into_vec;
assert_eq!;
Feature flags
std(default) — enablesbytes/std. Disable forno_std(requiresalloc).serde— implementsSerializeandDeserialize. Add#[serde(borrow)]to struct fields holdingCowBytes<'a>to allow zero-copy deserialization from input data.
Licence
Licensed under either of
- Apache Licence, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT Licence (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.