Newer
Older
//! Provides abstractions for working with bytes.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//!
//! The `bytes` crate provides an efficient byte buffer structure
//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer
//! implementations ([`Buf`](trait.Buf.html), [`BufMut`](trait.BufMut.html)).
//!
//! # `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.
//!
//! See the [struct docs](struct.Bytes.html) for more details.
//!
//! # `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
//! buffer that guarantees contiguous memory, but a
//! [rope](https://en.wikipedia.org/wiki/Rope_(data_structure)) stores the bytes
//! in disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current
//! position in the underlying byte storage. When bytes are read or written, the
//! cursor is advanced.
//!
//! ## 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.
//!
//! # Example
//!
//! ```
//! use bytes::{BytesMut, Buf, BufMut};
//! use std::io::Cursor;
//! use std::thread;
//!
//! // Allocate a buffer capable of holding 1024 bytes.
//! let mut buf = BytesMut::with_capacity(1024);
//!
//! // Write some data
//! buf.put("Hello world");
//! buf.put(b'-');
//! buf.put("goodbye");
//!
//! // Freeze the buffer, enabling concurrent access
//! let b1 = buf.freeze();
//! let b2 = b1.clone();
//!
//! thread::spawn(move || {
//! assert_eq!(&b1[..], b"Hello world-goodbye");
//! });
//!
//! let mut buf = Cursor::new(b2);
//! assert_eq!(b'H', buf.get_u8());
//! ```