foundation_arena/lib.rs
1// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
2// SPDX-License-Identifier: GPL-3.0-or-later
3//
4// Based on the code of typed-arena:
5//
6// SPDX-FileCopyrightText: © 2016 The typed-arena developers <https://github.com/thomcc/rust-typed-arena>
7// SPDX-License-Identifier: MIT
8
9//! # Foundation Arena.
10//!
11//! This crate provides an alternative to the [`typed_arena`] crate that does
12//! not use the heap. Instead, the [`Arena`] type statically allocates
13//! memory at compile-time by passing the `N` type parameter.
14//!
15//! # Examples
16//!
17//! ```rust
18//! use foundation_arena::Arena;
19//!
20//! let arena: Arena<u32, 8> = Arena::new();
21//! let one: &mut u32 = arena.alloc(1).unwrap();
22//! let two: &mut u32 = arena.alloc(2).unwrap();
23//!
24//! println!("{one} {two}");
25//! ```
26
27#![no_std]
28
29use core::{cell::RefCell, mem::MaybeUninit};
30
31pub mod boxed;
32
33/// An arena of objects of type `T`.
34pub struct Arena<T, const N: usize> {
35 storage: RefCell<Chunk<T, N>>,
36}
37
38impl<T, const N: usize> Arena<T, N> {
39 /// Construct a new arena.
40 pub const fn new() -> Self {
41 Self {
42 storage: RefCell::new(Chunk::new()),
43 }
44 }
45
46 /// Allocates an item in the arena, returning a mutable reference to that
47 /// item.
48 ///
49 /// If there's not enough space left in the arena, then the item is
50 /// returned as-is.
51 pub fn alloc(&self, item: T) -> Result<&mut T, T> {
52 let mut storage = self.storage.borrow_mut();
53 let len = storage.len();
54 storage.push(item)?;
55 Ok(unsafe { &mut *storage.as_mut_ptr().add(len) })
56 }
57}
58
59struct Chunk<T, const N: usize> {
60 buffer: [MaybeUninit<T>; N],
61 len: usize,
62}
63
64impl<T, const N: usize> Chunk<T, N> {
65 const ELEM: MaybeUninit<T> = MaybeUninit::uninit();
66 const INIT: [MaybeUninit<T>; N] = [Self::ELEM; N];
67
68 pub const fn new() -> Self {
69 Self {
70 buffer: Self::INIT,
71 len: 0,
72 }
73 }
74
75 pub const fn len(&self) -> usize {
76 self.len
77 }
78
79 pub fn push(&mut self, item: T) -> Result<(), T> {
80 if self.len < N {
81 unsafe {
82 *self.buffer.get_unchecked_mut(self.len) = MaybeUninit::new(item);
83 self.len += 1;
84 }
85 Ok(())
86 } else {
87 Err(item)
88 }
89 }
90
91 pub fn as_mut_ptr(&mut self) -> *mut T {
92 self.buffer.as_mut_ptr() as *mut T
93 }
94}