Skip to main content

ntex_bytes/
lib.rs

1//! Provides abstractions for working with bytes.
2//!
3//! The `ntex-bytes` crate provides an efficient byte buffer structure
4//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer
5//! implementations ([`Buf`], [`BufMut`]).
6//!
7//! [`Buf`]: trait.Buf.html
8//! [`BufMut`]: trait.BufMut.html
9//!
10//! # `Bytes`
11//!
12//! `Bytes` is an efficient container for storing and operating on contiguous
13//! slices of memory. It is intended for use primarily in networking code, but
14//! could have applications elsewhere as well.
15//!
16//! `Bytes` values facilitate zero-copy network programming by allowing multiple
17//! `Bytes` objects to point to the same underlying memory. This is managed by
18//! using a reference count to track when the memory is no longer needed and can
19//! be freed.
20//!
21//! A `Bytes` handle can be created directly from an existing `BytesMut` store
22//! is used first and written to. For example:
23//!
24//! ```rust
25//! use ntex_bytes::{BytesMut, BufMut};
26//!
27//! let mut buf = BytesMut::with_capacity(1024);
28//! buf.put(&b"hello world"[..]);
29//! buf.put_u16(1234);
30//!
31//! let a = buf.take();
32//! assert_eq!(a, b"hello world\x04\xD2"[..]);
33//!
34//! buf.put(&b"goodbye world"[..]);
35//!
36//! let b = buf.take();
37//! assert_eq!(b, b"goodbye world"[..]);
38//!
39//! assert_eq!(buf.capacity(), 998);
40//! ```
41//!
42//! In the above example, only a single buffer of 1024 is allocated. The handles
43//! `a` and `b` will share the underlying buffer and maintain indices tracking
44//! the view into the buffer represented by the handle.
45//!
46//! See the [struct docs] for more details.
47//!
48//! [struct docs]: struct.Bytes.html
49//!
50#![doc(html_root_url = "https://docs.rs/ntex-bytes/")]
51#![deny(clippy::pedantic)]
52#![allow(
53    unsafe_op_in_unsafe_fn,
54    clippy::must_use_candidate,
55    clippy::return_self_not_must_use,
56    clippy::cast_sign_loss,
57    clippy::cast_possible_wrap,
58    clippy::cast_possible_truncation
59)]
60
61extern crate alloc;
62
63pub mod buf;
64pub use crate::buf::{Buf, BufMut};
65
66mod bvec;
67mod bytes;
68mod debug;
69mod hex;
70mod serde;
71mod storage;
72mod string;
73
74pub use crate::bvec::BytesMut;
75pub use crate::bytes::Bytes;
76pub use crate::string::ByteString;
77
78#[doc(hidden)]
79pub use crate::storage::METADATA_SIZE;
80
81#[doc(hidden)]
82#[deprecated]
83pub type BytesVec = BytesMut;
84
85#[doc(hidden)]
86pub mod info {
87    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
88    pub struct Info {
89        pub id: usize,
90        pub refs: u32,
91        pub kind: Kind,
92        pub capacity: usize,
93    }
94
95    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
96    pub enum Kind {
97        Arc,
98        Inline,
99        Static,
100        Vec,
101    }
102}