| 1 | //! The arena, a fast but limited type of allocator. |
| 2 | //! |
| 3 | //! **A fast (but limited) allocation arena for values of a single type.** |
| 4 | //! |
| 5 | //! Allocated objects are destroyed all at once, when the arena itself is |
| 6 | //! destroyed. There is no deallocation of individual objects while the arena |
| 7 | //! itself is still alive. The flipside is that allocation is fast: typically |
| 8 | //! just a vector push. |
| 9 | //! |
| 10 | //! There is also a method `into_vec()` to recover ownership of allocated |
| 11 | //! objects when the arena is no longer required, instead of destroying |
| 12 | //! everything. |
| 13 | //! |
| 14 | //! ## Example |
| 15 | //! |
| 16 | //! ``` |
| 17 | //! use typed_arena::Arena; |
| 18 | //! |
| 19 | //! struct Monster { |
| 20 | //! level: u32, |
| 21 | //! } |
| 22 | //! |
| 23 | //! let monsters = Arena::new(); |
| 24 | //! |
| 25 | //! let goku = monsters.alloc(Monster { level: 9001 }); |
| 26 | //! assert!(goku.level > 9000); |
| 27 | //! ``` |
| 28 | //! |
| 29 | //! ## Safe Cycles |
| 30 | //! |
| 31 | //! All allocated objects get the same lifetime, so you can safely create cycles |
| 32 | //! between them. This can be useful for certain data structures, such as graphs |
| 33 | //! and trees with parent pointers. |
| 34 | //! |
| 35 | //! ``` |
| 36 | //! use std::cell::Cell; |
| 37 | //! use typed_arena::Arena; |
| 38 | //! |
| 39 | //! struct CycleParticipant<'a> { |
| 40 | //! other: Cell<Option<&'a CycleParticipant<'a>>>, |
| 41 | //! } |
| 42 | //! |
| 43 | //! let arena = Arena::new(); |
| 44 | //! |
| 45 | //! let a = arena.alloc(CycleParticipant { other: Cell::new(None) }); |
| 46 | //! let b = arena.alloc(CycleParticipant { other: Cell::new(None) }); |
| 47 | //! |
| 48 | //! a.other.set(Some(b)); |
| 49 | //! b.other.set(Some(a)); |
| 50 | //! ``` |
| 51 | |
| 52 | // Potential optimizations: |
| 53 | // 1) add and stabilize a method for in-place reallocation of vecs. |
| 54 | // 2) add and stabilize placement new. |
| 55 | // 3) use an iterator. This may add far too much unsafe code. |
| 56 | |
| 57 | #![deny(missing_docs)] |
| 58 | #![cfg_attr(not(any(feature = "std", test)), no_std)] |
| 59 | |
| 60 | #[cfg(not(feature = "std"))] |
| 61 | extern crate alloc; |
| 62 | |
| 63 | #[cfg(any(feature = "std", test))] |
| 64 | extern crate corerust-analyzer cargo typed-arena 2.0.2 core/; |
| 65 | |
| 66 | #[cfg(not(feature = "std"))] |
| 67 | use alloc::vec::Vec; |
| 68 | |
| 69 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#; |
| 70 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 71 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/; |
| 72 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 73 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/; |
| 74 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/; |
| 75 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/; |
| 76 | |
| 77 | use memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#; |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod testrust-analyzer cargo typed-arena 2.0.2 test/; |
| 81 | |
| 82 | // Initial size in bytes. |
| 83 | const INITIAL_SIZErust-analyzer cargo typed-arena 2.0.2 INITIAL_SIZE.: usize = 1024; |
| 84 | // Minimum capacity. Must be larger than 0. |
| 85 | const MIN_CAPACITYrust-analyzer cargo typed-arena 2.0.2 MIN_CAPACITY.: usize = 1; |
| 86 | |
| 87 | /// An arena of objects of type `T`. |
| 88 | /// |
| 89 | /// ## Example |
| 90 | /// |
| 91 | /// ``` |
| 92 | /// use typed_arena::Arena; |
| 93 | /// |
| 94 | /// struct Monster { |
| 95 | /// level: u32, |
| 96 | /// } |
| 97 | /// |
| 98 | /// let monsters = Arena::new(); |
| 99 | /// |
| 100 | /// let vegeta = monsters.alloc(Monster { level: 9001 }); |
| 101 | /// assert!(vegeta.level > 9000); |
| 102 | /// ``` |
| 103 | pub struct Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<T> { |
| 104 | chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<ChunkListrust-analyzer cargo typed-arena 2.0.2 ChunkList#<T>>, |
| 105 | } |
| 106 | |
| 107 | struct ChunkListrust-analyzer cargo typed-arena 2.0.2 ChunkList#<T> { |
| 108 | currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, |
| 109 | restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>>, |
| 110 | } |
| 111 | |
| 112 | impl<T> Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<T> { |
| 113 | /// Construct a new arena. |
| 114 | /// |
| 115 | /// ## Example |
| 116 | /// |
| 117 | /// ``` |
| 118 | /// use typed_arena::Arena; |
| 119 | /// |
| 120 | /// let arena = Arena::new(); |
| 121 | /// # arena.alloc(1); |
| 122 | /// ``` |
| 123 | pub fn newrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]new().() -> Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<T> { |
| 124 | let size = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(1, memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>()); |
| 125 | Arenarust-analyzer cargo typed-arena 2.0.2 Arena#::with_capacityrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]with_capacity().(INITIAL_SIZErust-analyzer cargo typed-arena 2.0.2 INITIAL_SIZE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().size) |
| 126 | } |
| 127 | |
| 128 | /// Construct a new arena with capacity for `n` values pre-allocated. |
| 129 | /// |
| 130 | /// ## Example |
| 131 | /// |
| 132 | /// ``` |
| 133 | /// use typed_arena::Arena; |
| 134 | /// |
| 135 | /// let arena = Arena::with_capacity(1337); |
| 136 | /// # arena.alloc(1); |
| 137 | /// ``` |
| 138 | pub fn with_capacityrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]with_capacity().(n: usize) -> Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<T> { |
| 139 | let n = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(MIN_CAPACITYrust-analyzer cargo typed-arena 2.0.2 MIN_CAPACITY., n); |
| 140 | Arenarust-analyzer cargo typed-arena 2.0.2 Arena# { |
| 141 | chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(ChunkListrust-analyzer cargo typed-arena 2.0.2 ChunkList# { |
| 142 | currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(n), |
| 143 | restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 144 | }), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Return the size of the arena |
| 149 | /// |
| 150 | /// This is useful for using the size of previous typed arenas to build new typed arenas with large enough spaces. |
| 151 | /// |
| 152 | /// ## Example |
| 153 | /// |
| 154 | /// ``` |
| 155 | /// use typed_arena::Arena; |
| 156 | /// |
| 157 | /// let arena = Arena::with_capacity(0); |
| 158 | /// let a = arena.alloc(1); |
| 159 | /// let b = arena.alloc(2); |
| 160 | /// |
| 161 | /// assert_eq!(arena.len(), 2); |
| 162 | /// ``` |
| 163 | pub fn lenrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]len().(&self) -> usize { |
| 164 | let chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().(); |
| 165 | |
| 166 | let mut res = 0; |
| 167 | for vec in chunks.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 168 | res rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().vec.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() |
| 169 | } |
| 170 | |
| 171 | res rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() |
| 172 | } |
| 173 | |
| 174 | /// Allocates a value in the arena, and returns a mutable reference |
| 175 | /// to that value. |
| 176 | /// |
| 177 | /// ## Example |
| 178 | /// |
| 179 | /// ``` |
| 180 | /// use typed_arena::Arena; |
| 181 | /// |
| 182 | /// let arena = Arena::new(); |
| 183 | /// let x = arena.alloc(42); |
| 184 | /// assert_eq!(*x, 42); |
| 185 | /// ``` |
| 186 | #[inline] |
| 187 | pub fn allocrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc().(&self, value: T) -> &mut T { |
| 188 | self.alloc_fast_pathrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_fast_path().(value) |
| 189 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|value| self.alloc_slow_pathrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_slow_path().(value)) |
| 190 | } |
| 191 | |
| 192 | #[inline] |
| 193 | fn alloc_fast_pathrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_fast_path().(&self, value: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut T, T> { |
| 194 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 195 | let len = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 196 | if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() { |
| 197 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(value); |
| 198 | // Avoid going through `Vec::deref_mut`, which overlaps |
| 199 | // other references we have already handed out! |
| 200 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(len <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); // bounds check |
| 201 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(len) }) |
| 202 | } else { |
| 203 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(value) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | fn alloc_slow_pathrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_slow_path().(&self, value: T) -> &mut T { |
| 208 | &mut self.alloc_extendrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_extend().(iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/once/once().(value))[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). |
| 209 | } |
| 210 | |
| 211 | /// Uses the contents of an iterator to allocate values in the arena. |
| 212 | /// Returns a mutable slice that contains these values. |
| 213 | /// |
| 214 | /// ## Example |
| 215 | /// |
| 216 | /// ``` |
| 217 | /// use typed_arena::Arena; |
| 218 | /// |
| 219 | /// let arena = Arena::new(); |
| 220 | /// let abc = arena.alloc_extend("abcdefg".chars().take(3)); |
| 221 | /// assert_eq!(abc, ['a', 'b', 'c']); |
| 222 | /// ``` |
| 223 | pub fn alloc_extendrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_extend().<I>(&self, iterable: I) -> &mut [T] |
| 224 | where |
| 225 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = T>, |
| 226 | { |
| 227 | let mut iter = iterable.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().(); |
| 228 | |
| 229 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 230 | |
| 231 | let iter_min_len = iter.size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().().0; |
| 232 | let mut next_item_index; |
| 233 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 234 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() >= chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 235 | "capacity is always greater than or equal to len, so we don't need to worry about underflow" |
| 236 | ); |
| 237 | if iter_min_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 238 | chunks.reserverust-analyzer cargo typed-arena 2.0.2 impl#[`ChunkList<T>`]reserve().(iter_min_len); |
| 239 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(iter); |
| 240 | next_item_index = 0; |
| 241 | } else { |
| 242 | next_item_index = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 243 | let mut i = 0; |
| 244 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(elem) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 245 | if chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() { |
| 246 | // The iterator was larger than we could fit into the current chunk. |
| 247 | let chunks = &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefMut<'_, T>`][DerefMut]deref_mut().chunks; |
| 248 | // Create a new chunk into which we can freely push the entire iterator into |
| 249 | chunks.reserverust-analyzer cargo typed-arena 2.0.2 impl#[`ChunkList<T>`]reserve().(i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 250 | let previous_chunk = chunks.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..last_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 251 | let previous_chunk_len = previous_chunk.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 252 | // Move any elements we put into the previous chunk into this new chunk |
| 253 | chunks |
| 254 | .currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current. |
| 255 | .extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(previous_chunk.drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(previous_chunk_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#)); |
| 256 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(elem); |
| 257 | // And the remaining elements in the iterator |
| 258 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(iter); |
| 259 | next_item_index = 0; |
| 260 | break; |
| 261 | } else { |
| 262 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(elem); |
| 263 | } |
| 264 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Extend the lifetime from that of `chunks_borrow` to that of `self`. |
| 269 | // This is OK because we’re careful to never move items |
| 270 | // by never pushing to inner `Vec`s beyond their initial capacity. |
| 271 | // The returned reference is unique (`&mut`): |
| 272 | // the `Arena` never gives away references to existing items. |
| 273 | unsafe { |
| 274 | let new_len = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().next_item_index; |
| 275 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(next_item_index), new_len) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Allocates space for a given number of values, but doesn't initialize it. |
| 280 | /// |
| 281 | /// ## Safety |
| 282 | /// |
| 283 | /// After calling this method, the arena considers the elements initialized. If you fail to |
| 284 | /// initialize them (which includes because of panicking during the initialization), the arena |
| 285 | /// will run destructors on the uninitialized memory. Therefore, you must initialize them. |
| 286 | /// |
| 287 | /// Considering how easy it is to cause undefined behaviour using this, you're advised to |
| 288 | /// prefer the other (safe) methods, like [`alloc_extend`][Arena::alloc_extend]. |
| 289 | /// |
| 290 | /// ## Example |
| 291 | /// |
| 292 | /// ```rust |
| 293 | /// use std::mem::{self, MaybeUninit}; |
| 294 | /// use std::ptr; |
| 295 | /// use typed_arena::Arena; |
| 296 | /// |
| 297 | /// // Transmute from MaybeUninit slice to slice of initialized T. |
| 298 | /// // It is a separate function to preserve the lifetime of the reference. |
| 299 | /// unsafe fn transmute_uninit<A>(r: &mut [MaybeUninit<A>]) -> &mut [A] { |
| 300 | /// mem::transmute(r) |
| 301 | /// } |
| 302 | /// |
| 303 | /// let arena: Arena<bool> = Arena::new(); |
| 304 | /// let slice: &mut [bool]; |
| 305 | /// unsafe { |
| 306 | /// let uninitialized = arena.alloc_uninitialized(10); |
| 307 | /// for elem in uninitialized.iter_mut() { |
| 308 | /// ptr::write(elem.as_mut_ptr(), true); |
| 309 | /// } |
| 310 | /// slice = transmute_uninit(uninitialized); |
| 311 | /// } |
| 312 | /// ``` |
| 313 | /// |
| 314 | /// ## Alternative allocation pattern |
| 315 | /// |
| 316 | /// To avoid the problem of dropping assumed to be initialized elements on panic, it is also |
| 317 | /// possible to combine the [`reserve_extend`][Arena::reserve_extend] with |
| 318 | /// [`uninitialized_array`][Arena::uninitialized_array], initialize the elements and confirm |
| 319 | /// them by this method. In such case, when there's a panic during initialization, the already |
| 320 | /// initialized elements would leak but it wouldn't cause UB. |
| 321 | /// |
| 322 | /// ```rust |
| 323 | /// use std::mem::{self, MaybeUninit}; |
| 324 | /// use std::ptr; |
| 325 | /// use typed_arena::Arena; |
| 326 | /// |
| 327 | /// unsafe fn transmute_uninit<A>(r: &mut [MaybeUninit<A>]) -> &mut [A] { |
| 328 | /// mem::transmute(r) |
| 329 | /// } |
| 330 | /// |
| 331 | /// const COUNT: usize = 2; |
| 332 | /// |
| 333 | /// let arena: Arena<String> = Arena::new(); |
| 334 | /// |
| 335 | /// arena.reserve_extend(COUNT); |
| 336 | /// let slice: &mut [String]; |
| 337 | /// unsafe { |
| 338 | /// // Perform initialization before we claim the memory. |
| 339 | /// let uninitialized = arena.uninitialized_array(); |
| 340 | /// assert!((*uninitialized).len() >= COUNT); // Ensured by the reserve_extend |
| 341 | /// for elem in &mut (*uninitialized)[..COUNT] { |
| 342 | /// ptr::write(elem.as_mut_ptr(), "Hello".to_owned()); |
| 343 | /// } |
| 344 | /// let addr = (*uninitialized).as_ptr() as usize; |
| 345 | /// |
| 346 | /// // The alloc_uninitialized returns the same memory, but "confirms" its allocation. |
| 347 | /// slice = transmute_uninit(arena.alloc_uninitialized(COUNT)); |
| 348 | /// assert_eq!(addr, slice.as_ptr() as usize); |
| 349 | /// assert_eq!(slice, &["Hello".to_owned(), "Hello".to_owned()]); |
| 350 | /// } |
| 351 | /// ``` |
| 352 | pub unsafe fn alloc_uninitializedrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_uninitialized().(&self, num: usize) -> &mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] { |
| 353 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 354 | |
| 355 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 356 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() >= chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 357 | "capacity is always greater than or equal to len, so we don't need to worry about underflow" |
| 358 | ); |
| 359 | if num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 360 | chunks.reserverust-analyzer cargo typed-arena 2.0.2 impl#[`ChunkList<T>`]reserve().(num); |
| 361 | } |
| 362 | |
| 363 | // At this point, the current chunk must have free capacity. |
| 364 | let next_item_index = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 365 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..set_lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]set_len().(next_item_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().num); |
| 366 | |
| 367 | // Go through pointers, to make sure we never create a reference to uninitialized T. |
| 368 | let start = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(next_item_index as isize); |
| 369 | let start_uninit = start as *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; |
| 370 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(start_uninit, num) |
| 371 | } |
| 372 | |
| 373 | /// Makes sure there's enough continuous space for at least `num` elements. |
| 374 | /// |
| 375 | /// This may save some work if called before [`alloc_extend`][Arena::alloc_extend]. It also |
| 376 | /// allows somewhat safer use pattern of [`alloc_uninitialized`][Arena::alloc_uninitialized]. |
| 377 | /// On the other hand this might waste up to `n - 1` elements of space. In case new allocation |
| 378 | /// is needed, the unused ones in current chunk are never used. |
| 379 | pub fn reserve_extendrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]reserve_extend().(&self, num: usize) { |
| 380 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 381 | |
| 382 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 383 | chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() >= chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 384 | "capacity is always greater than or equal to len, so we don't need to worry about underflow" |
| 385 | ); |
| 386 | if num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 387 | chunks.reserverust-analyzer cargo typed-arena 2.0.2 impl#[`ChunkList<T>`]reserve().(num); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /// Returns unused space. |
| 392 | /// |
| 393 | /// *This unused space is still not considered "allocated".* Therefore, it |
| 394 | /// won't be dropped unless there are further calls to `alloc`, |
| 395 | /// [`alloc_uninitialized`][Arena::alloc_uninitialized], or |
| 396 | /// [`alloc_extend`][Arena::alloc_extend] which is why the method is safe. |
| 397 | /// |
| 398 | /// It returns a raw pointer to avoid creating multiple mutable references to the same place. |
| 399 | /// It is up to the caller not to dereference it after any of the `alloc_` methods are called. |
| 400 | pub fn uninitialized_arrayrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]uninitialized_array().(&self) -> *mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] { |
| 401 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 402 | let len = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 403 | let next_item_index = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 404 | |
| 405 | unsafe { |
| 406 | // Go through pointers, to make sure we never create a reference to uninitialized T. |
| 407 | let start = chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(next_item_index as isize); |
| 408 | let start_uninit = start as *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; |
| 409 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::slice_from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/slice_from_raw_parts_mut().(start_uninit, len) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /// Convert this `Arena` into a `Vec<T>`. |
| 414 | /// |
| 415 | /// Items in the resulting `Vec<T>` appear in the order that they were |
| 416 | /// allocated in. |
| 417 | /// |
| 418 | /// ## Example |
| 419 | /// |
| 420 | /// ``` |
| 421 | /// use typed_arena::Arena; |
| 422 | /// |
| 423 | /// let arena = Arena::new(); |
| 424 | /// |
| 425 | /// arena.alloc("a"); |
| 426 | /// arena.alloc("b"); |
| 427 | /// arena.alloc("c"); |
| 428 | /// |
| 429 | /// let easy_as_123 = arena.into_vec(); |
| 430 | /// |
| 431 | /// assert_eq!(easy_as_123, vec!["a", "b", "c"]); |
| 432 | /// ``` |
| 433 | pub fn into_vecrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]into_vec().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> { |
| 434 | let mut chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..into_innerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]into_inner().(); |
| 435 | // keep order of allocation in the resulting Vec |
| 436 | let n = chunks |
| 437 | .restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest. |
| 438 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 439 | .foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]fold().(chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |a, v| a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().v.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 440 | let mut result = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(n); |
| 441 | for mut vec in chunks.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest. { |
| 442 | result.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut vec); |
| 443 | } |
| 444 | result.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current.); |
| 445 | result |
| 446 | } |
| 447 | |
| 448 | /// Returns an iterator that allows modifying each value. |
| 449 | /// |
| 450 | /// Items are yielded in the order that they were allocated. |
| 451 | /// |
| 452 | /// ## Example |
| 453 | /// |
| 454 | /// ``` |
| 455 | /// use typed_arena::Arena; |
| 456 | /// |
| 457 | /// #[derive(Debug, PartialEq, Eq)] |
| 458 | /// struct Point { x: i32, y: i32 }; |
| 459 | /// |
| 460 | /// let mut arena = Arena::new(); |
| 461 | /// |
| 462 | /// arena.alloc(Point { x: 0, y: 0 }); |
| 463 | /// arena.alloc(Point { x: 1, y: 1 }); |
| 464 | /// |
| 465 | /// for point in arena.iter_mut() { |
| 466 | /// point.x += 10; |
| 467 | /// } |
| 468 | /// |
| 469 | /// let points = arena.into_vec(); |
| 470 | /// |
| 471 | /// assert_eq!(points, vec![Point { x: 10, y: 0 }, Point { x: 11, y: 1 }]); |
| 472 | /// |
| 473 | /// ``` |
| 474 | /// |
| 475 | /// ## Immutable Iteration |
| 476 | /// |
| 477 | /// Note that there is no corresponding `iter` method. Access to the arena's contents |
| 478 | /// requries mutable access to the arena itself. |
| 479 | /// |
| 480 | /// ```compile_fail |
| 481 | /// use typed_arena::Arena; |
| 482 | /// |
| 483 | /// let mut arena = Arena::new(); |
| 484 | /// let x = arena.alloc(1); |
| 485 | /// |
| 486 | /// // borrow error! |
| 487 | /// for i in arena.iter_mut() { |
| 488 | /// println!("i: {}", i); |
| 489 | /// } |
| 490 | /// |
| 491 | /// // borrow error! |
| 492 | /// *x = 2; |
| 493 | /// ``` |
| 494 | #[inline] |
| 495 | pub fn iter_mutrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]iter_mut().(&mut self) -> IterMutrust-analyzer cargo typed-arena 2.0.2 IterMut#<T> { |
| 496 | let chunks = self.chunksrust-analyzer cargo typed-arena 2.0.2 Arena#chunks..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]get_mut().(); |
| 497 | let position = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().chunks.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 498 | let index = 0; |
| 499 | let inner_iter = chunks.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().(); |
| 500 | // Extend the lifetime of the individual elements to that of the arena. |
| 501 | // This is OK because we borrow the arena mutably to prevent new allocations |
| 502 | // and we take care here to never move items inside the arena while the |
| 503 | // iterator is alive. |
| 504 | let inner_iter = unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(inner_iter) }; |
| 505 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListRestrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest# { indexrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#index., inner_iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#inner_iter. } |
| 506 | } else { |
| 507 | // Extend the lifetime of the individual elements to that of the arena. |
| 508 | let iter = unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(chunks.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) }; |
| 509 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListCurrentrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent# { iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent#iter. } |
| 510 | }; |
| 511 | IterMutrust-analyzer cargo typed-arena 2.0.2 IterMut# { |
| 512 | chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks., |
| 513 | staterust-analyzer cargo typed-arena 2.0.2 IterMut#state.: position, |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | impl Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<u8> { |
| 519 | /// Allocates a string slice and returns a mutable reference to it. |
| 520 | /// |
| 521 | /// This is on `Arena<u8>`, because string slices use byte slices (`[u8]`) as their backing |
| 522 | /// storage. |
| 523 | /// |
| 524 | /// # Example |
| 525 | /// |
| 526 | /// ``` |
| 527 | /// use typed_arena::Arena; |
| 528 | /// |
| 529 | /// let arena: Arena<u8> = Arena::new(); |
| 530 | /// let hello = arena.alloc_str("Hello world"); |
| 531 | /// assert_eq!("Hello world", hello); |
| 532 | /// ``` |
| 533 | #[inline] |
| 534 | pub fn alloc_strrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<u8>`]alloc_str().(&self, s: &str) -> &mut str { |
| 535 | let buffer = self.alloc_extendrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]alloc_extend().(s.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().()); |
| 536 | // Can't fail the utf8 validation, it already came in as utf8 |
| 537 | unsafe { strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8_unchecked_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked_mut().(buffer) } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | impl<T> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Arenarust-analyzer cargo typed-arena 2.0.2 Arena#<T> { |
| 542 | fn defaultrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`][Default]default().() -> Selfrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`][Default] { |
| 543 | Selfrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`][Default]::newrust-analyzer cargo typed-arena 2.0.2 impl#[`Arena<T>`]new().() |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | impl<T> ChunkListrust-analyzer cargo typed-arena 2.0.2 ChunkList#<T> { |
| 548 | #[inline(never)] |
| 549 | #[cold] |
| 550 | fn reserverust-analyzer cargo typed-arena 2.0.2 impl#[`ChunkList<T>`]reserve().(&mut self, additional: usize) { |
| 551 | let double_cap = self |
| 552 | .currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current. |
| 553 | .capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() |
| 554 | .checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_mul().(2) |
| 555 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("capacity overflow"); |
| 556 | let required_cap = additional |
| 557 | .checked_next_power_of_tworust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_next_power_of_two().() |
| 558 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("capacity overflow"); |
| 559 | let new_capacity = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(double_cap, required_cap); |
| 560 | let chunk = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(&mut self.currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current., Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(new_capacity)); |
| 561 | self.restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(chunk); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | enum IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#<'a, T> { |
| 566 | ChunkListRestrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest# { |
| 567 | indexrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#index.: usize, |
| 568 | inner_iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#inner_iter.: slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::IterMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/IterMut#<'a, T>, |
| 569 | }, |
| 570 | ChunkListCurrentrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent# { |
| 571 | iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent#iter.: slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::IterMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/IterMut#<'a, T>, |
| 572 | }, |
| 573 | } |
| 574 | |
| 575 | /// Mutable arena iterator. |
| 576 | /// |
| 577 | /// This struct is created by the [`iter_mut`](struct.Arena.html#method.iter_mut) method on [Arenas](struct.Arena.html). |
| 578 | pub struct IterMutrust-analyzer cargo typed-arena 2.0.2 IterMut#<'a, T: 'a> { |
| 579 | chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks.: &'a mut ChunkListrust-analyzer cargo typed-arena 2.0.2 ChunkList#<T>, |
| 580 | staterust-analyzer cargo typed-arena 2.0.2 IterMut#state.: IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#<'a, T>, |
| 581 | } |
| 582 | |
| 583 | impl<'a, T> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for IterMutrust-analyzer cargo typed-arena 2.0.2 IterMut#<'a, T> { |
| 584 | type Itemrust-analyzer cargo typed-arena 2.0.2 impl#[`IterMut<'a, T>`][Iterator]Item# = &'a mut T; |
| 585 | fn nextrust-analyzer cargo typed-arena 2.0.2 impl#[`IterMut<'a, T>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a mut T> { |
| 586 | loop { |
| 587 | self.staterust-analyzer cargo typed-arena 2.0.2 IterMut#state. = match self.staterust-analyzer cargo typed-arena 2.0.2 IterMut#state. { |
| 588 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListRestrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest# { |
| 589 | mut indexrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#index., |
| 590 | ref mut inner_iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#inner_iter., |
| 591 | } => { |
| 592 | match inner_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]next().() { |
| 593 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) => return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item), |
| 594 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 595 | index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 596 | if index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 597 | let inner_iter = self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().(); |
| 598 | // Extend the lifetime of the individual elements to that of the arena. |
| 599 | let inner_iter = unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(inner_iter) }; |
| 600 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListRestrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest# { indexrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#index., inner_iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListRest#inner_iter. } |
| 601 | } else { |
| 602 | let iter = self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().(); |
| 603 | // Extend the lifetime of the individual elements to that of the arena. |
| 604 | let iter = unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(iter) }; |
| 605 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListCurrentrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent# { iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent#iter. } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | IterMutStaterust-analyzer cargo typed-arena 2.0.2 IterMutState#::ChunkListCurrentrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent# { ref mut iterrust-analyzer cargo typed-arena 2.0.2 IterMutState#ChunkListCurrent#iter. } => return iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]next().(), |
| 611 | }; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | fn size_hintrust-analyzer cargo typed-arena 2.0.2 impl#[`IterMut<'a, T>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 616 | let current_len = self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 617 | let current_cap = self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..currentrust-analyzer cargo typed-arena 2.0.2 ChunkList#current..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(); |
| 618 | if self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 619 | (current_len, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_len)) |
| 620 | } else { |
| 621 | let rest_len = self.chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks..restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 622 | let last_chunk_len = self |
| 623 | .chunksrust-analyzer cargo typed-arena 2.0.2 IterMut#chunks. |
| 624 | .restrust-analyzer cargo typed-arena 2.0.2 ChunkList#rest. |
| 625 | .lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().() |
| 626 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|chunk| chunk.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) |
| 627 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0); |
| 628 | |
| 629 | let min = current_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().last_chunk_len; |
| 630 | let max = min rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().(rest_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().current_cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().rest_len); |
| 631 | |
| 632 | (min, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(max)) |
| 633 | } |
| 634 | } |
| 635 | } |