1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
//! Data types for zone-based (also known as region-based or arena-based) data allocation.
//!
//! # Examples
//!
//! ## Linked List Nodes with [`Arena<T>`]
//! ```
//! use zone_alloc::Arena;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Node<'a, T> {
//! parent: Option<&'a Node<'a, T>>,
//! value: T,
//! }
//!
//! impl<'a, T> Node<'a, T> {
//! pub fn new(parent: Option<&'a Node<'a, T>>, value: T) -> Self {
//! Self { parent, value }
//! }
//! }
//!
//! fn main() {
//! let arena = Arena::new();
//! let node = arena.alloc(Node::new(None, 1));
//! let node = arena.alloc(Node::new(Some(node), 2));
//! let node = arena.alloc(Node::new(Some(node), 3));
//!
//! assert_eq!(node.value, 3);
//! assert_eq!(node.parent.unwrap().value, 2);
//! assert_eq!(node.parent.unwrap().parent.unwrap().value, 1);
//! assert_eq!(node.parent.unwrap().parent.unwrap().parent, None);
//! }
//! ```
//!
//! ## Circular References with [`Registry<T>`]
//! ```
//! use zone_alloc::{
//! Handle,
//! Registry,
//! };
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Node<T> {
//! parent: Option<Handle>,
//! value: T,
//! }
//!
//! impl<T> Node<T> {
//! pub fn new(parent: Option<Handle>, value: T) -> Self {
//! Self { parent, value }
//! }
//! }
//!
//! fn main() {
//! let registry = Registry::new();
//! let root_handle = registry.register(Node::new(None, "first"));
//! let handle = registry.register(Node::new(Some(root_handle), "second"));
//! let handle = registry.register(Node::new(Some(handle), "third"));
//! registry.get_mut_unchecked(root_handle).parent = Some(handle);
//!
//! let node = registry.get(handle).unwrap();
//! assert_eq!(node.value, "third");
//! let node = registry.get(node.parent.unwrap()).unwrap();
//! assert_eq!(node.value, "second");
//! let node = registry.get(node.parent.unwrap()).unwrap();
//! assert_eq!(node.value, "first");
//! let node = registry.get(node.parent.unwrap()).unwrap();
//! assert_eq!(node.value, "third");
//! }
//! ```
//!
//! ## Circular References with [`KeyedRegistry<T>`]
//! ```
//! #[cfg(not(feature = "std"))]
//! extern crate alloc;
//!
//! #[cfg(not(feature = "std"))]
//! use alloc::borrow::ToOwned;
//!
//! use zone_alloc::KeyedRegistry;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Node<K, V> {
//! parent: Option<K>,
//! value: V,
//! }
//!
//! impl<K, V> Node<K, V> {
//! pub fn new(parent: Option<K>, value: V) -> Self {
//! Self { parent, value }
//! }
//! }
//!
//! fn main() {
//! let registry = KeyedRegistry::new();
//! registry.register("node-1".to_owned(), Node::new(None, "first"));
//! registry.register(
//! "node-2".to_owned(),
//! Node::new(Some("node-1".to_owned()), "second"),
//! );
//! registry.register(
//! "node-3".to_owned(),
//! Node::new(Some("node-2".to_owned()), "third"),
//! );
//! registry.get_mut_unchecked("node-1").parent = Some("node-3".to_owned());
//!
//! let node = registry.get("node-3").unwrap();
//! assert_eq!(node.value, "third");
//! let node = registry.get(node.parent.as_ref().unwrap()).unwrap();
//! assert_eq!(node.value, "second");
//! let node = registry.get(node.parent.as_ref().unwrap()).unwrap();
//! assert_eq!(node.value, "first");
//! let node = registry.get(node.parent.as_ref().unwrap()).unwrap();
//! assert_eq!(node.value, "third");
//! }
//! ```
//!
//! ## Runtime Borrow Checking
//! ```
//! use zone_alloc::{
//! BorrowError,
//! Registry,
//! };
//!
//! fn main() {
//! let registry = Registry::new();
//! registry.register_extend(100..200);
//!
//! // Multiple immutable borrows on the same element.
//! let borrow_1 = registry.get(16);
//! let borrow_2 = registry.get(16);
//! let borrow_3 = registry.get(16);
//! assert!(borrow_1.as_ref().is_ok_and(|i| i.eq(&116)));
//! assert!(borrow_2.as_ref().is_ok_and(|i| i.eq(&116)));
//! assert!(borrow_3.as_ref().is_ok_and(|i| i.eq(&116)));
//!
//! // Mutable borrow fails.
//! assert_eq!(
//! registry.get_mut(16).err(),
//! Some(BorrowError::AlreadyBorrowed)
//! );
//!
//! // Another element can be borrowed independently.
//! let borrow_4 = registry.get(32);
//! assert!(borrow_4.as_ref().is_ok_and(|i| i.eq(&132)));
//! assert!(borrow_1.as_ref().is_ok_and(|i| i.eq(&116)));
//!
//! // Only one mutable borrow allowed.
//! let mut borrow_5 = registry.get_mut(64).unwrap();
//! assert!(borrow_5.eq(&164));
//! *borrow_5 *= 2;
//! assert!(borrow_5.eq(&328));
//! assert_eq!(
//! registry.get_mut(64).err(),
//! Some(BorrowError::AlreadyBorrowed)
//! );
//! assert_eq!(registry.get(64).err(), Some(BorrowError::AlreadyBorrowed));
//!
//! // Refetch to show updated value, and show that previous borrows are still valid.
//! drop(borrow_5);
//! let borrow_5 = registry.get(64);
//! assert!(borrow_5.as_ref().is_ok_and(|i| i.eq(&328)));
//! assert!(borrow_4.as_ref().is_ok_and(|i| i.eq(&132)));
//! assert!(borrow_1.as_ref().is_ok_and(|i| i.eq(&116)));
//! }
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(return_position_impl_trait_in_trait)]
#![feature(trait_alias)]
pub mod arena;
mod base_registry;
mod borrow_error;
pub mod element;
pub mod keyed_registry;
pub mod registry;
mod registry_container;
pub mod strong_registry;
pub use arena::Arena;
pub use borrow_error::BorrowError;
pub use element::{
ElementRef,
ElementRefMut,
};
pub use keyed_registry::KeyedRegistry;
pub use registry::{
Handle,
Registry,
};
pub use registry_container::Key;
pub use strong_registry::{
StrongHandle,
StrongRegistry,
};