pub struct AstAlloc { /* private fields */ }Expand description
Owns the arenas required to allocate new AST nodes and provide builder methods to create them.
§Drop and arena allocation
The most popular choice for arena is the bumpalo crate, which is a fast bump allocator that
can handle heterogeneous data. However, it doesn’t support destructors, which is a problem
because some of the nodes in the AST owns heap allocated data and needs to be de-allocated
(numbers and parse errors currently).
Another choice is typed-arena and derivatives, which do run destructors, but can only store
one type of values. As the number of types that need to be dropped is relatively small, we use
a general bumpalo arena by default, and specialized typed arenas for stuff that need to be
dropped.
§Guarantees
AstAlloc guarantees that the memory that has been allocated won’t be moved until Self is deallocated.
Implementations§
Source§impl AstAlloc
impl AstAlloc
Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Return the current number of allocated bytes.
Sourcepub fn alloc<T: Allocable>(&self, value: T) -> &T
pub fn alloc<T: Allocable>(&self, value: T) -> &T
Allocates an AST component in the arena.
Self never guarantees that all destructors are going to be run when using such a generic allocation function. We don’t want to allocate values that need to be dropped through this method, typically because they own heap-allocated data, such as numbers or parse errors. That’s why we use a marker trait to specify which types can be allocated freely. Types that need to be dropped don’t implement Allocable and have a dedicated method for allocation.
Sourcepub fn alloc_many<T: Allocable, I>(&self, iter: I) -> &[T]
pub fn alloc_many<T: Allocable, I>(&self, iter: I) -> &[T]
Allocates a sequence of AST components in the arena.
See Self::alloc.
Sourcepub fn alloc_singleton<T: Allocable>(&self, value: T) -> &[T]
pub fn alloc_singleton<T: Allocable>(&self, value: T) -> &[T]
Allocates an array with exactly one element in the arena.
Sourcepub fn clone_from<'to, T: CloneTo>(&'to self, data: T::Data<'_>) -> T::Data<'to>
pub fn clone_from<'to, T: CloneTo>(&'to self, data: T::Data<'_>) -> T::Data<'to>
Deep clone an already allocated AST component from another arena to the current one.
Sourcepub fn clone_ref_from<'from, 'to, T>(
&'to self,
data: &'from T::Data<'from>,
) -> &'to T::Data<'to>
pub fn clone_ref_from<'from, 'to, T>( &'to self, data: &'from T::Data<'from>, ) -> &'to T::Data<'to>
Same as Self::clone_from but take an arena-allocated reference instead.
pub fn number(&self, number: Number) -> Node<'_>
pub fn alloc_number(&self, number: Number) -> &Number
pub fn string<'ast>(&'ast self, s: &str) -> Node<'ast>
pub fn string_chunks<'ast, I>(&'ast self, chunks: I) -> Node<'ast>
pub fn fun<'ast, I>(&'ast self, args: I, body: Ast<'ast>) -> Node<'ast>
pub fn unary_fun<'ast>( &'ast self, arg: Pattern<'ast>, body: Ast<'ast>, ) -> Node<'ast>
pub fn let_block<'ast, I>( &'ast self, bindings: I, body: Ast<'ast>, rec: bool, ) -> Node<'ast>
pub fn app<'ast, I>(&'ast self, head: Ast<'ast>, args: I) -> Node<'ast>
pub fn enum_variant<'ast>( &'ast self, tag: LocIdent, arg: Option<Ast<'ast>>, ) -> Node<'ast>
pub fn record<'ast>(&'ast self, record: Record<'ast>) -> Node<'ast>
pub fn record_data<'ast, Is, Ds>(
&'ast self,
includes: Is,
field_defs: Ds,
open: bool,
) -> &'ast Record<'ast>where
Ds: IntoIterator<Item = FieldDef<'ast>>,
Is: IntoIterator<Item = Include<'ast>>,
Ds::IntoIter: ExactSizeIterator,
Is::IntoIter: ExactSizeIterator,
pub fn if_then_else<'ast>( &'ast self, cond: Ast<'ast>, then_branch: Ast<'ast>, else_branch: Ast<'ast>, ) -> Node<'ast>
pub fn match_expr<'ast, I>(&'ast self, branches: I) -> Node<'ast>
pub fn array<'ast, I>(&'ast self, elts: I) -> Node<'ast>
pub fn prim_op<'ast, I>(&'ast self, op: PrimOp, args: I) -> Node<'ast>
pub fn annotated<'ast>( &'ast self, annot: Annotation<'ast>, inner: Ast<'ast>, ) -> Node<'ast>
pub fn annotation<'ast, I>( &'ast self, typ: Option<Type<'ast>>, contracts: I, ) -> Annotation<'ast>
pub fn import_path(&self, path: OsString, format: InputFormat) -> Node<'_>
pub fn import_package(&self, id: Ident) -> Node<'_>
pub fn typ<'ast>(&'ast self, typ: Type<'ast>) -> Node<'ast>
pub fn type_data<'ast>( &'ast self, typ: TypeUnr<'ast>, pos: TermPos, ) -> &'ast Type<'ast>
pub fn enum_rows<'ast>( &'ast self, erows: EnumRowsUnr<'ast>, ) -> &'ast EnumRows<'ast>
pub fn record_rows<'ast>( &'ast self, rrows: RecordRowsUnr<'ast>, ) -> &'ast RecordRows<'ast>
pub fn parse_error(&self, error: ParseError) -> Node<'_>
pub fn record_pattern<'ast, I>( &'ast self, patterns: I, tail: TailPattern, pos: TermPos, ) -> &'ast RecordPattern<'ast>
pub fn array_pattern<'ast, I>( &'ast self, patterns: I, tail: TailPattern, pos: TermPos, ) -> &'ast ArrayPattern<'ast>
pub fn or_pattern<'ast, I>( &'ast self, patterns: I, pos: TermPos, ) -> &'ast OrPattern<'ast>
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for AstAlloc
impl !RefUnwindSafe for AstAlloc
impl Send for AstAlloc
impl !Sync for AstAlloc
impl Unpin for AstAlloc
impl UnsafeUnpin for AstAlloc
impl !UnwindSafe for AstAlloc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more