#buffers #zero-copy #io

ntex-bytes

Types and traits for working with bytes (bytes crate fork)

16 releases

0.1.15 Jun 20, 2022
0.1.14 Feb 6, 2022
0.1.12 Jan 31, 2022
0.1.8 Dec 18, 2021
0.1.4 Jun 27, 2021

#92 in Data structures

Download history 576/week @ 2022-03-14 562/week @ 2022-03-21 582/week @ 2022-03-28 415/week @ 2022-04-04 368/week @ 2022-04-11 531/week @ 2022-04-18 501/week @ 2022-04-25 698/week @ 2022-05-02 443/week @ 2022-05-09 741/week @ 2022-05-16 469/week @ 2022-05-23 605/week @ 2022-05-30 490/week @ 2022-06-06 462/week @ 2022-06-13 760/week @ 2022-06-20 530/week @ 2022-06-27

2,321 downloads per month
Used in 26 crates (13 directly)

MIT license

230KB
4K SLoC

Bytes

A utility library for working with bytes. This is fork of bytes crate (https://github.com/tokio-rs/bytes)

Crates.io

Documentation

Usage

To use bytes, first add this to your Cargo.toml:

[dependencies]
ntex-bytes = "0.1"

Next, add this to your crate:

use ntex_bytes::{Bytes, BytesMut, Buf, BufMut};

Serde support

Serde support is optional and disabled by default. To enable use the feature serde.

[dependencies]
ntex-bytes = { version = "0.1", features = ["serde"] }

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ntex-bytes by you, shall be licensed as MIT, without any additional terms or conditions.


lib.rs:

Provides abstractions for working with bytes.

This is fork of bytes crate

The ntex-bytes crate provides an efficient byte buffer structure (Bytes) and traits for working with buffer implementations (Buf, BufMut).

Bytes

Bytes is an efficient container for storing and operating on contiguous slices of memory. It is intended for use primarily in networking code, but could have applications elsewhere as well.

Bytes values facilitate zero-copy network programming by allowing multiple Bytes objects to point to the same underlying memory. This is managed by using a reference count to track when the memory is no longer needed and can be freed.

A Bytes handle can be created directly from an existing byte store (such as &[u8] or Vec<u8>), but usually a BytesMut is used first and written to. For example:

use ntex_bytes::{BytesMut, BufMut};

let mut buf = BytesMut::with_capacity(1024);
buf.put(&b"hello world"[..]);
buf.put_u16(1234);

let a = buf.split();
assert_eq!(a, b"hello world\x04\xD2"[..]);

buf.put(&b"goodbye world"[..]);

let b = buf.split();
assert_eq!(b, b"goodbye world"[..]);

assert_eq!(buf.capacity(), 998);

In the above example, only a single buffer of 1024 is allocated. The handles a and b will share the underlying buffer and maintain indices tracking the view into the buffer represented by the handle.

See the struct docs for more details.

Dependencies

~295–485KB