rcbytes/
lib.rs

1// The code in this file is heavily based on [Carl Lerche's LRU implementation](https://github.com/tokio-rs/bytes).
2//
3// MIT License
4//
5// Copyright (c) 2022 Al Liu (https://github.com/al8n/rcbytes)
6//
7// Copyright (c) 2018 Carl Lerche (https://github.com/tokio-rs/bytes)
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15
16// The above copyright notice and this permission notice shall be included in all
17// copies or substantial portions of the Software.
18
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25// SOFTWARE.
26
27#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
28#![doc(test(
29    no_crate_inject,
30    attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
31))]
32#![no_std]
33
34//! Provides abstractions for working with bytes.
35//!
36//! The `bytes` crate provides an efficient byte buffer structure
37//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer
38//! implementations ([`Buf`], [`BufMut`]).
39//!
40//! [`Buf`]: trait.Buf.html
41//! [`BufMut`]: trait.BufMut.html
42//!
43//! # `Bytes`
44//!
45//! `Bytes` is an efficient container for storing and operating on contiguous
46//! slices of memory. It is intended for use primarily in networking code, but
47//! could have applications elsewhere as well.
48//!
49//! `Bytes` values facilitate zero-copy network programming by allowing multiple
50//! `Bytes` objects to point to the same underlying memory. This is managed by
51//! using a reference count to track when the memory is no longer needed and can
52//! be freed.
53//!
54//! A `Bytes` handle can be created directly from an existing byte store (such as `&[u8]`
55//! or `Vec<u8>`), but usually a `BytesMut` is used first and written to. For
56//! example:
57//!
58//! ```rust
59//! use rcbytes::{BytesMut, BufMut};
60//!
61//! let mut buf = BytesMut::with_capacity(1024);
62//! buf.put(&b"hello world"[..]);
63//! buf.put_u16(1234);
64//!
65//! let a = buf.split();
66//! assert_eq!(a, b"hello world\x04\xD2"[..]);
67//!
68//! buf.put(&b"goodbye world"[..]);
69//!
70//! let b = buf.split();
71//! assert_eq!(b, b"goodbye world"[..]);
72//!
73//! assert_eq!(buf.capacity(), 998);
74//! ```
75//!
76//! In the above example, only a single buffer of 1024 is allocated. The handles
77//! `a` and `b` will share the underlying buffer and maintain indices tracking
78//! the view into the buffer represented by the handle.
79//!
80//! See the [struct docs] for more details.
81//!
82//! [struct docs]: struct.Bytes.html
83//!
84//! # `Buf`, `BufMut`
85//!
86//! These two traits provide read and write access to buffers. The underlying
87//! storage may or may not be in contiguous memory. For example, `Bytes` is a
88//! buffer that guarantees contiguous memory, but a [rope] stores the bytes in
89//! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current
90//! position in the underlying byte storage. When bytes are read or written, the
91//! cursor is advanced.
92//!
93//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure)
94//!
95//! ## Relation with `Read` and `Write`
96//!
97//! At first glance, it may seem that `Buf` and `BufMut` overlap in
98//! functionality with `std::io::Read` and `std::io::Write`. However, they
99//! serve different purposes. A buffer is the value that is provided as an
100//! argument to `Read::read` and `Write::write`. `Read` and `Write` may then
101//! perform a syscall, which has the potential of failing. Operations on `Buf`
102//! and `BufMut` are infallible.
103
104extern crate alloc;
105
106#[cfg(feature = "std")]
107extern crate std;
108
109pub mod buf;
110pub use crate::buf::{Buf, BufMut};
111
112mod bytes;
113mod bytes_mut;
114mod fmt;
115pub use crate::bytes::Bytes;
116pub use crate::bytes_mut::BytesMut;
117
118// Optional Serde support
119#[cfg(feature = "serde")]
120mod serde;
121
122#[inline(never)]
123#[cold]
124fn abort() -> ! {
125    #[cfg(feature = "std")]
126    {
127        std::process::abort();
128    }
129
130    #[cfg(not(feature = "std"))]
131    {
132        struct Abort;
133        impl Drop for Abort {
134            fn drop(&mut self) {
135                panic!();
136            }
137        }
138        let _a = Abort;
139        panic!("abort");
140    }
141}