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#![deny(warnings, missing_debug_implementations, rust_2018_idioms)]
51#![doc(html_root_url = "https://docs.rs/ntex-bytes/")]
52#![allow(unsafe_op_in_unsafe_fn)]
53
54extern crate alloc;
55
56pub mod buf;
57pub use crate::buf::{Buf, BufMut};
58
59mod bvec;
60mod bytes;
61mod debug;
62mod hex;
63mod serde;
64mod storage;
65mod string;
66
67pub use crate::bvec::BytesMut;
68pub use crate::bytes::Bytes;
69pub use crate::string::ByteString;
70
71#[doc(hidden)]
72pub use crate::storage::METADATA_SIZE;
73
74#[doc(hidden)]
75#[deprecated]
76pub type BytesVec = BytesMut;
77
78#[doc(hidden)]
79pub mod info {
80 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
81 pub struct Info {
82 pub id: usize,
83 pub refs: u32,
84 pub kind: Kind,
85 pub capacity: usize,
86 }
87
88 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
89 pub enum Kind {
90 Arc,
91 Inline,
92 Static,
93 Vec,
94 }
95}