Address

Struct Address 

Source
pub struct Address(/* private fields */);
Expand description

Memory address of an AST node in arena.

Implementations§

Source§

impl Address

Source

pub const DUMMY: Self

Dummy address.

Never equal to any real Address, but is equal to itself.

Source

pub unsafe fn from_ptr<T>(p: *const T) -> Self

Get the memory address of a pointer to an AST node in arena.

This method is an escape hatch only. Prefer using GetAddress::address or UnstableAddress::unstable_address instead, because they are more likely to produce a stable Address. (Yes even unstable_address is more likely to produce a stable Address than this function!)

If the AST node is in a Box, the address is guaranteed to be a unique identifier for the duration of the arena’s existence.

But if the node is in a Vec, then the Address may not remain accurate if the Vec is resized or has elements added or removed before this node.

The pointer must point to an AST node in the arena (not on the stack), or the returned Address will be meaningless.

If called with a reference, the reference must point to an AST node in the arena (not on the stack), or the returned Address will be meaningless. Be careful not to pass a double-reference to from_ptr, or the resulting Address will point to the reference itself, instead of the thing being referenced.

impl<'a> Visit<'a> for MyVisitor {
    fn visit_identifier_reference(&mut self, ident: &IdentifierReference<'a>) {
        // Correct - `address` is address of the `IdentifierReference`
        let address = Address::from_ptr(ident);
        // WRONG - `address` is address of `&IdentifierReference` reference itself, which is on the stack
        let address = Address::from_ptr(&ident);
    }
}
§SAFETY

Pointer must be non-null.

§Example

Demonstration of the difference between Address::from_ptr and GetAddress::address:

use oxc_allocator::{Address, GetAddress, Vec};
use oxc_span::SPAN;

// Create a `Vec<Statement>` containing a single `BlockStatement`
let mut stmts = Vec::with_capacity_in(1, &allocator);
stmts.push(ast_builder.statement_block(SPAN, Vec::new_in(&allocator)));

let block_address = stmts[0].address();
let stmt_address = Address::from_ptr(&stmts[0]);

// Add another `Statement` to the `Vec`.
// This causes the `Vec` to grow and reallocate.
stmts.push(ast_builder.statement_empty(SPAN));

let block_address_after_push = stmts[0].address();
let stmt_address_after_push = Address::from_ptr(&stmts[0]);

// Address of the `BlockStatement` is unchanged
// (because the `Box`'s pointer still points to same memory location)
assert!(block_address_after_push == block_address);
// Address of the `Statement` has changed
// (because the `Vec` reallocated, so its contents have moved in memory)
assert!(stmt_address_after_push != stmt_address);

// Insert a new `Statement` at start of the `Vec`.
// The `BlockStatement` is now at index 1.
stmts.insert(0, ast_builder.statement_empty(SPAN));

let block_address_after_insert = stmts[1].address();
let stmt_address_after_insert = Address::from_ptr(&stmts[1]);

// Address of the `BlockStatement` is still unchanged
assert!(block_address_after_insert == block_address_after_push);
// Address of the `Statement` has changed again
assert!(stmt_address_after_insert != stmt_address_after_push);

Trait Implementations§

Source§

impl Clone for Address

Source§

fn clone(&self) -> Address

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Address

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GetAddress for Address

Source§

fn address(&self) -> Address

Address of an Address is itself.

Source§

impl Hash for Address

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Address

Source§

fn eq(&self, other: &Address) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Address

Source§

impl Eq for Address

Source§

impl StructuralPartialEq for Address

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, T> FromIn<'a, T> for T

Source§

fn from_in(t: T, _: &'a Allocator) -> T

Converts to this type from the input type within the given allocator.
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'a, T, U> IntoIn<'a, U> for T
where U: FromIn<'a, T>,

Source§

fn into_in(self, allocator: &'a Allocator) -> U

Converts this type into the (usually inferred) input type within the given allocator.
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.