Files
bytes/src/lib.rs
T

95 lines
3.1 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`
//!
//! `Bytes` is an efficient container for storing and operating on continguous
//! 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.
//!
2017-02-20 14:31:26 -08: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
//! example:
//!
//! ```rust
//! use bytes::{BytesMut, BufMut, BigEndian};
//!
//! let mut buf = BytesMut::with_capacity(1024);
//! buf.put(&b"hello world"[..]);
//! buf.put_u16::<BigEndian>(1234);
//!
//! let a = buf.drain();
//! assert_eq!(a, b"hello world\x04\xD2"[..]);
//!
//! buf.put(&b"goodbye world"[..]);
//!
//! let b = buf.drain();
//! 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
//! functionality with `std::io::Ready` and `std::io::Write`. However, they
//! 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.
2017-03-07 10:20:58 -08:00
#![deny(warnings, missing_docs, missing_debug_implementations)]
2017-02-28 14:39:18 -08:00
#![doc(html_root_url = "https://docs.rs/bytes/0.4")]
2015-04-07 23:40:00 -07:00
2016-11-01 08:14:31 -07:00
extern crate byteorder;
2017-02-28 11:15:35 -08:00
extern crate iovec;
2016-09-23 14:51:48 -07:00
2017-02-28 17:09:43 -08:00
pub mod buf;
2016-11-01 08:14:31 -07:00
pub use buf::{
Buf,
BufMut,
IntoBuf,
2017-02-28 17:09:43 -08:00
};
#[deprecated(since = "0.4.1", note = "moved to `buf` module")]
#[doc(hidden)]
pub use buf::{
2016-11-01 08:14:31 -07:00
Reader,
Writer,
2017-02-16 22:15:17 -08:00
Take,
2016-11-01 08:14:31 -07:00
};
2017-02-28 17:09:43 -08:00
mod bytes;
2016-11-01 08:14:31 -07:00
pub use bytes::{Bytes, BytesMut};
2017-02-28 17:09:43 -08:00
2017-02-16 22:15:17 -08:00
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};