Vec

Struct Vec 

Source
pub struct Vec<'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: &'alloc Allocator) -> 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 arena = Allocator::default();

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

pub fn with_capacity_in(capacity: usize, allocator: &'alloc Allocator) -> 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 arena = Allocator::default();

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

// 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, &arena);
assert_eq!(vec_units.capacity(), usize::MAX);
Source

pub fn from_iter_in<I: IntoIterator<Item = T>>( iter: I, allocator: &'alloc Allocator, ) -> 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_array_in<const N: usize>( array: [T; N], allocator: &'alloc Allocator, ) -> 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 array: [u32; 4] = [1, 2, 3, 4];
let vec = Vec::from_array_in(array, &allocator);
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

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(&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, Bump>

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, Bump>

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<'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<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<'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<'alloc, T> IntoIterator for Vec<'alloc, T>

Source§

type IntoIter = <Vec<'alloc, T, Bump> 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<'alloc, T: PartialEq> PartialEq for Vec<'alloc, T>

Source§

fn eq(&self, other: &Vec<'alloc, T>) -> bool

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

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<'a, T> TakeIn<'a> for Vec<'a, T>

Source§

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

Replace node with a dummy.
Source§

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

Replace node with a boxed dummy.
Source§

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

Source§

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

Source§

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

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

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

  2. All internal methods which access the &Bump 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. Bump is not thread-safe, and this would be undefined behavior.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'alloc, T> !UnwindSafe 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.