Skip to main content

ArenaVec

Struct ArenaVec 

Source
pub struct ArenaVec<'alloc, T>(/* private fields */);
Expand description

A Vec without Drop, which stores its data in the arena allocator.

§No Drops

Objects allocated into Oxc memory arenas are never Dropped. Memory is released in bulk when the allocator is dropped, without dropping the individual objects in the arena.

Therefore, it would produce a memory leak if you allocated Drop types into the arena which own memory allocations outside the arena.

Static checks make this impossible to do. Vec::new_in and all other methods which create a Vec will refuse to compile if called with a Drop type.

Implementations§

Source§

impl<'alloc, T> Vec<'alloc, T>

Source

pub fn new_in(allocator: &impl GetAllocator<'alloc>) -> Self

Constructs a new, empty Vec<T>.

The vector will not allocate until elements are pushed onto it.

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let mut vec: Vec<i32> = Vec::new_in(&allocator);
assert!(vec.is_empty());
Source

pub fn with_capacity_in( capacity: usize, allocator: &impl GetAllocator<'alloc>, ) -> Self

Constructs a new, empty Vec<T> with at least the specified capacity with the provided allocator.

The vector will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity. If capacity is 0, the vector will not allocate.

It is important to note that although the returned vector has the minimum capacity specified, the vector will have a zero length.

For Vec<T> where T is a zero-sized type, there will be no allocation and the capacity will always be u32::MAX.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let mut vec = Vec::with_capacity_in(10, &allocator);

// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), 0);
assert_eq!(vec.capacity(), 10);

// These are all done without reallocating...
for i in 0..10 {
    vec.push(i);
}
assert_eq!(vec.len(), 10);
assert_eq!(vec.capacity(), 10);

// ...but this may make the vector reallocate
vec.push(11);
assert_eq!(vec.len(), 11);
assert!(vec.capacity() >= 11);

// A vector of a zero-sized type will always over-allocate, since no
// allocation is necessary
let vec_units = Vec::<()>::with_capacity_in(10, &allocator);
assert_eq!(vec_units.capacity(), usize::MAX);
Source

pub fn from_iter_in( iter: impl IntoIterator<Item = T>, allocator: &impl GetAllocator<'alloc>, ) -> Self

Create a new Vec whose elements are taken from an iterator and allocated in the given allocator.

This is behaviorially identical to FromIterator::from_iter.

Source

pub fn from_value_in(value: T, allocator: &impl GetAllocator<'alloc>) -> Self

Create a new Vec containing only a single value, allocated in the given allocator.

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let value = 123u32;
let vec = Vec::from_value_in(value, &allocator);
assert_eq!(vec, [123]);
Source

pub fn from_array_in<const N: usize>( array: [T; N], allocator: &impl GetAllocator<'alloc>, ) -> Self

Create a new Vec from a fixed-size array, allocated in the given allocator.

This is preferable to from_iter_in where source is an array, as size is statically known, and compiler is more likely to construct the values directly in arena, rather than constructing on stack and then copying to arena.

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let array: [u32; 4] = [1, 2, 3, 4];
let vec = Vec::from_array_in(array, &allocator);
Source

pub unsafe fn from_raw_parts_in( ptr: NonNull<T>, length: usize, capacity: usize, allocator: &impl GetAllocator<'alloc>, ) -> Self

Create a Vec<T> directly from a pointer, a length, and a capacity, allocated in the given allocator.

§SAFETY

This is highly unsafe, due to the number of invariants that aren’t checked:

  • ptr needs to have been previously allocated via Vec<T> in allocator’s arena (at least, it’s highly likely to be incorrect if it wasn’t).
  • ptr’s T needs to have the same size and alignment as it was allocated with.
  • length needs to be less than or equal to capacity.
  • capacity needs to be the capacity that the pointer was allocated with.
  • The memory must remain valid for the lifetime of the returned Vec - i.e. the Vec’s lifetime must not exceed allocator’s.

Violating these may cause problems like corrupting the allocator’s internal data structures. For example it is not safe to build a Vec<u8> from a pointer to a C char array and a size_t.

The ownership of ptr is effectively transferred to the Vec<T>, which may then reallocate or change the contents of the memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after calling this function.

§Examples
use oxc_allocator::{Allocator, Vec};
use std::{mem, ptr::{self, NonNull}};

let allocator = Allocator::default();
let allocator = &allocator;

let mut v = Vec::from_iter_in([1, 2, 3], &allocator);

// Pull out the various important pieces of information about `v`
let p = NonNull::new(v.as_mut_ptr()).unwrap();
let len = v.len();
let cap = v.capacity();

let rebuilt = unsafe {
    // Forget `v` so we are in complete control of the allocation to which `p` points.
    mem::forget(v);

    // Overwrite memory with 4, 5, 6
    for i in 0..len {
        p.add(i).write(4 + i);
    }

    // Put everything back together into a Vec
    Vec::from_raw_parts_in(p, len, cap, &allocator)
};
assert_eq!(rebuilt, [4, 5, 6]);
Source

pub fn into_boxed_slice(self) -> Box<'alloc, [T]>

Convert Vec<T> into [Box<[T]>].

Any spare capacity in the Vec is lost.

[Box<[T]>]: Box

Source

pub fn into_arena_slice(self) -> &'alloc [T]

Converts Vec<T> into [&'alloc [T]].

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let mut vec = Vec::from_iter_in([1, 2, 3], &allocator);
let slice = vec.into_arena_slice();
assert_eq!(slice, [1, 2, 3]);
Source

pub fn into_arena_slice_mut(self) -> &'alloc mut [T]

Converts Vec<T> into [&'alloc mut [T]].

§Examples
use oxc_allocator::{Allocator, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let vec = Vec::from_iter_in([1, 2, 3], &allocator);
let slice = vec.into_arena_slice_mut();
slice[0] = 4;
assert_eq!(slice, [4, 2, 3]);

Trait Implementations§

Source§

impl<'new_alloc, T, C> CloneIn<'new_alloc> for Vec<'_, T>
where T: CloneIn<'new_alloc, Cloned = C>, C: 'new_alloc,

Source§

type Cloned = Vec<'new_alloc, C>

The type of the cloned object. Read more
Source§

fn clone_in_impl( &self, with_semantic_ids: CloneInSemanticIds, allocator: &'new_alloc Allocator, ) -> Self::Cloned

Clone self into allocator, threading whether semantic ids should be preserved as a runtime with_semantic_ids flag rather than as two separate methods. Read more
Source§

fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned

Clone self into the given allocator. allocator may be the same one that self is already in.
Source§

fn clone_in_with_semantic_ids( &self, allocator: &'new_alloc Allocator, ) -> Self::Cloned

Almost identical as clone_in, but for some special type, it will also clone the semantic ids. Please use this method only if you make sure semantic info is synced with the ast node.
Source§

impl<T: Debug> Debug for Vec<'_, T>

Source§

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

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

impl<'alloc, T> Deref for Vec<'alloc, T>

Source§

type Target = Vec<'alloc, T, Arena>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'alloc, T> DerefMut for Vec<'alloc, T>

Source§

fn deref_mut(&mut self) -> &mut Vec<'alloc, T, Arena>

Mutably dereferences the value.
Source§

impl<'a, T> Dummy<'a> for Vec<'a, T>

Source§

fn dummy(allocator: &'a Allocator) -> Self

Create a dummy Vec.

Source§

impl<'alloc, T: Eq> Eq for Vec<'alloc, T>

Source§

impl<'a, T: 'a> From<Vec<'a, T>> for Box<'a, [T]>

Source§

fn from(v: Vec<'a, T>) -> Box<'a, [T]>

Converts to this type from the input type.
Source§

impl<'a, T, const N: usize> FromIn<'a, [T; N]> for Vec<'a, T>

Source§

fn from_in(array: [T; N], allocator: &'a Allocator) -> Self

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

impl<T: Hash> Hash for Vec<'_, T>

Source§

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

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

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<T, I> Index<I> for Vec<'_, T>
where I: SliceIndex<[T]>,

Source§

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, I> IndexMut<I> for Vec<'_, T>
where I: SliceIndex<[T]>,

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'alloc, T> IntoIterator for Vec<'alloc, T>

Source§

type IntoIter = <Vec<'alloc, T, Arena> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'i, T> IntoIterator for &'i Vec<'_, T>

Source§

type IntoIter = Iter<'i, T>

Which kind of iterator are we turning this into?
Source§

type Item = &'i T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'i, T> IntoIterator for &'i mut Vec<'_, T>

Source§

type IntoIter = IterMut<'i, T>

Which kind of iterator are we turning this into?
Source§

type Item = &'i mut T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq<U>, U, const N: usize> PartialEq<&[U; N]> for Vec<'_, T>

Source§

fn eq(&self, other: &&[U; N]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<&[U]> for Vec<'_, T>

Source§

fn eq(&self, other: &&[U]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U, const N: usize> PartialEq<&mut [U; N]> for Vec<'_, T>

Source§

fn eq(&self, other: &&mut [U; N]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<&mut [U]> for Vec<'_, T>

Source§

fn eq(&self, other: &&mut [U]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<Vec<'_, U>> for Vec<'_, T>

Source§

fn eq(&self, other: &Vec<'_, U>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<Vec<'_, U>> for [T]

Source§

fn eq(&self, other: &Vec<'_, U>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<Vec<'_, U>> for &[T]

Source§

fn eq(&self, other: &Vec<'_, U>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<Vec<'_, U>> for &mut [T]

Source§

fn eq(&self, other: &Vec<'_, U>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U; N]> for Vec<'_, T>

Source§

fn eq(&self, other: &[U; N]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq<U>, U> PartialEq<[U]> for Vec<'_, T>

Source§

fn eq(&self, other: &[U]) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a, T> ReplaceWith<'a> for Vec<'a, T>

Source§

fn replace_with(&mut self, replacer: impl FnOnce(Self) -> Self)

Replace the node in place with the value returned by replacer. Read more
Source§

impl<T: Sync> Sync for Vec<'_, T>

SAFETY: Even though Arena is not Sync, we can make Vec<T> Sync if T is Sync because:

  1. No public methods allow access to the &Arena that Vec contains (in RawVec), so user cannot illegally obtain 2 &Arenas on different threads via Vec.

  2. All internal methods which access the &Arena take a &mut self. &mut Vec cannot be transferred across threads, and nor can an owned Vec (Vec is not Send). Therefore these methods taking &mut self can be sure they’re not operating on a Vec which has been moved across threads.

Note: Vec CANNOT be Send, even if T is Send, because that would allow 2 Vecs on different threads to both allocate into same arena simultaneously. Arena is not thread-safe, and this would be undefined behavior.

Source§

impl<'a, T> TakeIn<'a> for Vec<'a, T>

Source§

fn take_in(&mut self, allocator_accessor: &impl GetAllocator<'a>) -> Self

Replace node with a dummy, and return the original. Read more
Source§

fn take_in_box( &mut self, allocator_accessor: &impl GetAllocator<'a>, ) -> Box<'a, Self>

Replace node with a dummy, allocate the original node into the arena, and return it as a Box<Self>. Read more

Auto Trait Implementations§

§

impl<'alloc, T> !RefUnwindSafe for Vec<'alloc, T>

§

impl<'alloc, T> !Send for Vec<'alloc, T>

§

impl<'alloc, T> !UnwindSafe for Vec<'alloc, T>

§

impl<'alloc, T> Freeze for Vec<'alloc, T>

§

impl<'alloc, T> Unpin for Vec<'alloc, T>

§

impl<'alloc, T> UnsafeUnpin for Vec<'alloc, T>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. 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
Source§

impl<T> From<T> for T

Source§

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.
Source§

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

Source§

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.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.