fixed_typed_arena/
lib.rs

1/*
2 * Copyright (C) 2021-2022 taylor.fish <contact@taylor.fish>
3 *
4 * This file is part of fixed-typed-arena.
5 *
6 * fixed-typed-arena is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * fixed-typed-arena is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with fixed-typed-arena. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#![no_std]
21#![cfg_attr(feature = "dropck_eyepatch", feature(dropck_eyepatch))]
22#![deny(unsafe_op_in_unsafe_fn)]
23#![warn(clippy::pedantic)]
24#![allow(clippy::default_trait_access)]
25#![allow(clippy::module_name_repetitions)]
26#![allow(clippy::must_use_candidate)]
27
28//! An arena that allocates values of a single type (similar to [typed-arena])
29//! using chunks of memory that have a configurable fixed size. This enables it
30//! to perform allocations in non-amortized O(1) (constant) time.
31//!
32//! Other arena implementations, like [typed-arena], are optimized for
33//! throughput: they allocate chunks of memory with exponentially increasing
34//! sizes, which results in *amortized* constant-time allocations.
35//!
36//! [typed-arena]: https://docs.rs/typed-arena
37//!
38//! **fixed-typed-arena** is optimized for latency: it allocates chunks of
39//! memory with a fixed, configurable size, and individual value allocations
40//! are performed in non-amortized constant time.
41//!
42//! This crate depends only on [`core`] and [`alloc`], so it can be used in
43//! `no_std` environments that support [`alloc`].
44//!
45//! [`core`]: https://doc.rust-lang.org/core/
46//! [`alloc`]: https://doc.rust-lang.org/alloc/
47//!
48//! Example
49//! -------
50//!
51//! ```rust
52//! use fixed_typed_arena::Arena;
53//! struct Item(u64);
54//!
55//! let arena = Arena::<_, 128>::new();
56//! let item1 = arena.alloc(Item(1));
57//! let item2 = arena.alloc(Item(2));
58//! item1.0 += item2.0;
59//!
60//! assert_eq!(item1.0, 3);
61//! assert_eq!(item2.0, 2);
62//! ```
63//!
64//! References
65//! ----------
66//!
67//! Items allocated by an [`Arena`] can contain references with the same life
68//! as the arena itself, including references to other items, but the crate
69//! feature `dropck_eyepatch` must be enabled. This requires Rust nightly, as
70//! fixed-typed-arena must use the [eponymous unstable language feature][drop].
71//!
72//! [drop]: https://github.com/rust-lang/rust/issues/34761
73//!
74//! Alternatively, you may be able to use a [`ManuallyDropArena`] instead.
75//!
76//! ManuallyDropArena
77//! -----------------
78//!
79//! This crate also provides [`ManuallyDropArena`], which is like [`Arena`] but
80//! returns references of any lifetime, including `'static`. The advantage of
81//! this type is that it can be used without being borrowed, but it comes with
82//! the tradeoff that it will leak memory unless the unsafe [`drop`] method is
83//! called.
84//!
85//! Iteration
86//! ---------
87//!
88//! fixed-typed-arena’s arena types allow iteration over all allocated items.
89//! Safe mutable iteration is provided for [`Arena`], and safe immutable
90//! iteration is provided for all arena types if [`Options::Mutable`] is false.
91//! Unsafe mutable and immutable iteration is provided for all arena types
92//! regardless of options.
93//!
94//! [`Arena`]: arena::Arena
95//! [`ManuallyDropArena`]: manually_drop::ManuallyDropArena
96//! [`drop`]: manually_drop::ManuallyDropArena::drop
97
98extern crate alloc;
99
100mod chunk;
101pub mod options;
102#[cfg(test)]
103mod tests;
104
105pub mod arena;
106pub mod manually_drop;
107pub use options::{ArenaOptions, Options};
108
109/// Arena iterators.
110pub mod iter {
111    pub use super::manually_drop::iter::*;
112}
113
114#[rustfmt::skip]
115/// Convenience alias for [`arena::Arena`].
116pub type Arena<
117    T,
118    const CHUNK_SIZE: usize = 16,
119    const SUPPORTS_POSITIONS: bool = false,
120    const MUTABLE: bool = true,
121> = arena::Arena<
122    T,
123    Options<
124        CHUNK_SIZE,
125        SUPPORTS_POSITIONS,
126        MUTABLE,
127    >,
128>;
129
130#[rustfmt::skip]
131/// Convenience alias for [`manually_drop::ManuallyDropArena`].
132pub type ManuallyDropArena<
133    T,
134    const CHUNK_SIZE: usize = 16,
135    const SUPPORTS_POSITIONS: bool = false,
136    const MUTABLE: bool = true,
137> = manually_drop::ManuallyDropArena<
138    T,
139    Options<
140        CHUNK_SIZE,
141        SUPPORTS_POSITIONS,
142        MUTABLE,
143    >,
144>;