Files
bytes/src/lib.rs
T

92 lines
3.0 KiB
Rust
Raw Normal View History

2016-11-01 08:14:31 -07:00
//! Provides abstractions for working with bytes.
2017-02-16 11:15:46 -08:00
//!
//! The `bytes` crate provides an efficient byte buffer structure
//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer
2017-02-20 14:31:26 -08:00
//! implementations ([`Buf`], [`BufMut`]).
//!
//! [`Buf`]: trait.Buf.html
//! [`BufMut`]: trait.BufMut.html
2017-02-16 11:15:46 -08:00
//!
//! # `Bytes`
//!
2019-07-09 18:02:23 +07:00
//! `Bytes` is an efficient container for storing and operating on contiguous
2017-02-16 11:15:46 -08:00
//! 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.
//!
2018-07-12 19:05:02 -07:00
//! 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
2017-02-20 14:31:26 -08:00
//! example:
//!
//! ```rust
//! use bytes::{BytesMut, BufMut};
2017-02-20 14:31:26 -08:00
//!
//! let mut buf = BytesMut::with_capacity(1024);
//! buf.put(&b"hello world"[..]);
//! buf.put_u16(1234);
2017-02-20 14:31:26 -08:00
//!
//! let a = buf.split();
2017-02-20 14:31:26 -08:00
//! assert_eq!(a, b"hello world\x04\xD2"[..]);
//!
//! buf.put(&b"goodbye world"[..]);
//!
//! let b = buf.split();
2017-02-20 14:31:26 -08:00
//! 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.
//!
//! [struct docs]: struct.Bytes.html
2017-02-16 11:15:46 -08:00
//!
//! # `Buf`, `BufMut`
//!
//! These two traits provide read and write access to buffers. The underlying
//! storage may or may not be in contiguous memory. For example, `Bytes` is a
2017-02-20 14:31:26 -08:00
//! buffer that guarantees contiguous memory, but a [rope] stores the bytes in
//! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current
2017-02-16 11:15:46 -08:00
//! position in the underlying byte storage. When bytes are read or written, the
//! cursor is advanced.
//!
2017-02-20 14:31:26 -08:00
//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure)
//!
2017-02-16 11:15:46 -08:00
//! ## Relation with `Read` and `Write`
//!
//! At first glance, it may seem that `Buf` and `BufMut` overlap in
2017-05-02 15:35:06 -04:00
//! functionality with `std::io::Read` and `std::io::Write`. However, they
2017-02-16 11:15:46 -08:00
//! serve different purposes. A buffer is the value that is provided as an
//! argument to `Read::read` and `Write::write`. `Read` and `Write` may then
//! perform a syscall, which has the potential of failing. Operations on `Buf`
//! and `BufMut` are infallible.
2019-07-26 05:01:22 +09:00
#![deny(warnings, missing_docs, missing_debug_implementations, rust_2018_idioms)]
2018-01-29 09:40:45 -08:00
#![doc(html_root_url = "https://docs.rs/bytes/0.5.0")]
2015-04-07 23:40:00 -07:00
2017-02-28 17:09:43 -08:00
pub mod buf;
2019-07-26 05:01:22 +09:00
pub use crate::buf::{
2016-11-01 08:14:31 -07:00
Buf,
BufMut,
IntoBuf,
2017-02-28 17:09:43 -08:00
};
mod bytes;
mod debug;
2019-07-26 05:01:22 +09:00
pub use crate::bytes::{Bytes, BytesMut};
2017-02-28 17:09:43 -08:00
// Optional Serde support
#[cfg(feature = "serde")]
2019-06-07 12:31:44 -07:00
mod serde;
2018-09-03 10:23:00 -07:00
// Optional `Either` support
#[cfg(feature = "either")]
mod either;