Skip to content

Commit d676f1e

Browse files
committed
feat(allocator): introduce Address
1 parent 62f759c commit d676f1e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

crates/oxc_allocator/src/arena.rs

+15
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ impl<'alloc, T: Hash> Hash for Vec<'alloc, T> {
212212
}
213213
}
214214

215+
/// Memory address of an AST node in arena.
216+
///
217+
/// `Address` is generated from a `Box<T>`.
218+
/// AST nodes in a `Box` in an arena are guaranteed to never move in memory,
219+
/// so this address acts as a unique identifier for the duration of the arena's existence.
220+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
221+
pub struct Address(usize);
222+
223+
impl<'a, T> Box<'a, T> {
224+
#[inline]
225+
pub fn address(&self) -> Address {
226+
Address(ptr::addr_of!(**self) as usize)
227+
}
228+
}
229+
215230
#[cfg(test)]
216231
mod test {
217232
use std::hash::{DefaultHasher, Hash, Hasher};

crates/oxc_allocator/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod convert;
99

1010
use bumpalo::Bump;
1111

12-
pub use arena::{Box, String, Vec};
12+
pub use arena::{Address, Box, String, Vec};
1313
pub use clone_in::CloneIn;
1414
pub use convert::{FromIn, IntoIn};
1515

0 commit comments

Comments
 (0)