polymock/lib.rs
1//! A fast thread-safe bump allocation arena.
2//!
3//! `polymock` provides a fast allocation [`Arena`] using the bump allocation strategy. It's
4//! primary use case is high-throughput multi-threaded network applications which allocate and free
5//! buffers very frequently and cannot afford going through the global allocator.
6//!
7//! `polymock` only supports allocating `[u8]` buffers. It will never support allocating of
8//! different objects.
9//!
10//! # Using `bytes`
11//!
12//! `polymock` provides it's own [`Bytes`](crate::Bytes) type that is a mostly drop-in replacement
13//! for the equivalent type from the [`bytes`] crate.
14//!
15//! Once the construction using a custom vtable will be public, [`Bytes`](crate::Bytes) will be
16//! deprecated in favor of the [`bytes`] crate.
17//!
18//! # `no_std` support
19//!
20//! `polymock` supports `no_std`, but requires the `alloc` crate. To enable `no_std` support
21//! disable the default "std" feature:
22//!
23//! ```toml
24//! polymock = { version = "0.2.0", default-features = false }
25//! ```
26//!
27//! # Example
28//!
29//! ```
30//! use polymock::Arena;
31//!
32//! let mut arena = Arena::default();
33//!
34//! let mut buf = arena.alloc(1500);
35//!
36//! for b in buf.iter_mut() {
37//! *b = 1;
38//! }
39//!
40//! let buf1 = buf.freeze();
41//! let buf2 = buf1.clone();
42//!
43//! assert_eq!(buf1, buf2);
44//! ```
45//!
46//! [`bytes`]: https://docs.rs/bytes/latest/bytes/index.html
47
48#![no_std]
49#![deny(unsafe_op_in_unsafe_fn)]
50#![cfg_attr(docsrs, feature(doc_cfg))]
51
52extern crate alloc;
53
54#[cfg(any(feature = "std", test))]
55extern crate std;
56
57pub mod buf;
58
59pub(crate) mod arena;
60pub(crate) mod bytes;
61pub(crate) mod bytes_mut;
62pub(crate) mod loom;
63
64pub use arena::Arena;
65pub use buf::Buf;
66pub use bytes::Bytes;
67pub use bytes_mut::BytesMut;
68
69#[inline(never)]
70#[cold]
71pub(crate) fn abort() -> ! {
72 #[cfg(feature = "std")]
73 {
74 std::process::abort();
75 }
76
77 #[cfg(not(feature = "std"))]
78 {
79 struct Abort;
80 impl Drop for Abort {
81 fn drop(&mut self) {
82 panic!();
83 }
84 }
85
86 panic!("abort");
87 }
88}