]>
git.proxmox.com Git - cargo.git/blob - vendor/bytes/src/lib.rs
1 #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
4 attr(deny(warnings
, rust_2018_idioms
), allow(dead_code
, unused_variables
))
8 //! Provides abstractions for working with bytes.
10 //! The `bytes` crate provides an efficient byte buffer structure
11 //! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer
12 //! implementations ([`Buf`], [`BufMut`]).
14 //! [`Buf`]: trait.Buf.html
15 //! [`BufMut`]: trait.BufMut.html
19 //! `Bytes` is an efficient container for storing and operating on contiguous
20 //! slices of memory. It is intended for use primarily in networking code, but
21 //! could have applications elsewhere as well.
23 //! `Bytes` values facilitate zero-copy network programming by allowing multiple
24 //! `Bytes` objects to point to the same underlying memory. This is managed by
25 //! using a reference count to track when the memory is no longer needed and can
28 //! A `Bytes` handle can be created directly from an existing byte store (such as `&[u8]`
29 //! or `Vec<u8>`), but usually a `BytesMut` is used first and written to. For
33 //! use bytes::{BytesMut, BufMut};
35 //! let mut buf = BytesMut::with_capacity(1024);
36 //! buf.put(&b"hello world"[..]);
37 //! buf.put_u16(1234);
39 //! let a = buf.split();
40 //! assert_eq!(a, b"hello world\x04\xD2"[..]);
42 //! buf.put(&b"goodbye world"[..]);
44 //! let b = buf.split();
45 //! assert_eq!(b, b"goodbye world"[..]);
47 //! assert_eq!(buf.capacity(), 998);
50 //! In the above example, only a single buffer of 1024 is allocated. The handles
51 //! `a` and `b` will share the underlying buffer and maintain indices tracking
52 //! the view into the buffer represented by the handle.
54 //! See the [struct docs] for more details.
56 //! [struct docs]: struct.Bytes.html
60 //! These two traits provide read and write access to buffers. The underlying
61 //! storage may or may not be in contiguous memory. For example, `Bytes` is a
62 //! buffer that guarantees contiguous memory, but a [rope] stores the bytes in
63 //! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current
64 //! position in the underlying byte storage. When bytes are read or written, the
65 //! cursor is advanced.
67 //! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure)
69 //! ## Relation with `Read` and `Write`
71 //! At first glance, it may seem that `Buf` and `BufMut` overlap in
72 //! functionality with `std::io::Read` and `std::io::Write`. However, they
73 //! serve different purposes. A buffer is the value that is provided as an
74 //! argument to `Read::read` and `Write::write`. `Read` and `Write` may then
75 //! perform a syscall, which has the potential of failing. Operations on `Buf`
76 //! and `BufMut` are infallible.
80 #[cfg(feature = "std")]
84 pub use crate::buf
::{Buf, BufMut}
;
90 pub use crate::bytes
::Bytes
;
91 pub use crate::bytes_mut
::BytesMut
;
93 // Optional Serde support
94 #[cfg(feature = "serde")]
100 #[cfg(feature = "std")]
102 std
::process
::abort();
105 #[cfg(not(feature = "std"))]
108 impl Drop
for Abort
{