Skip to main content

Arena

Struct Arena 

Source
pub struct Arena {
Show 17 fields pub root: u32, pub data_types: Column<u8>, pub parents: Column<u32>, pub next_sibling: Column<u32>, pub previous_sibling: Column<u32>, pub first_token: Column<u32>, pub last_token: Column<u32>, pub first_child: Column<u32>, pub last_child: Column<u32>, pub name: Column<u32>, pub string_value: Column<u32>, pub numeric_value: Column<f64>, pub unit: Column<u32>, pub flags: Column<u32>, pub color_type: Column<u8>, pub spans: Column<u64>, pub errors: RefCell<HashMap<u32, Vec<ErrorDescription>>>, /* private fields */
}
Expand description

Arena memory manager.

The node data is stored column-wise in vectors allocated for the capacity of the arena (62 bytes per node). The arena grows by doubling its capacity and never frees memory by itself: detached nodes keep their memory and their ids stay valid. Arena::release, Arena::compact, Arena::trim and Arena::compact_strings reclaim the memory.

Fields§

§root: u32

root node

§data_types: Column<u8>§parents: Column<u32>§next_sibling: Column<u32>§previous_sibling: Column<u32>§first_token: Column<u32>§last_token: Column<u32>§first_child: Column<u32>§last_child: Column<u32>§name: Column<u32>

interned name (nam) of rules / declarations / at-rules

§string_value: Column<u32>§numeric_value: Column<f64>§unit: Column<u32>§flags: Column<u32>§color_type: Column<u8>§spans: Column<u64>

packed spans: 8 bits for the source, 28 bits for the start, 28 bits for the end

§errors: RefCell<HashMap<u32, Vec<ErrorDescription>>>

validation errors by node id

Implementations§

Source§

impl Arena

Source

pub fn new(size: usize) -> Self

Source

pub fn current(&self) -> u32

last allocated node id

Source

pub fn set_current(&self, current: u32)

Source

pub fn generation(&self) -> u32

incremented when the columns are reallocated

Source

pub fn generation_ptr(&self) -> *const u32

address of the generation counter: the javascript views read it to detect a growth of the columns

Source

pub fn column_raw(&self, index: u32) -> (*const u8, usize)

pointer and length (in elements) of a column, by index of arenaColumns. the column views of javascript

Source

pub fn transferred(&self) -> bool

true when the column buffers have been transferred: the arena can no longer be used

Source

pub fn allocate(&self) -> u32

Source

pub fn recycle(&self, id: u32)

recycle a detached leaf node that is no longer referenced: its id is reused by the next allocation

Source

pub fn intern_string(&self, str: &str) -> u32

intern a string, returns its id

Source

pub fn intern_rc(&self, str: &Rc<str>) -> u32

intern a shared string without copying it

Source

pub fn intern_shared(&self, str: &SharedStr) -> u32

intern a shared string: a shared Rc<str> is not copied

Source

pub fn intern_range( &self, codes: &CodeUnits, start: usize, end: usize, source: i32, ) -> u32

intern a range of code units of a source (or of the canonical text / the pool, see StringSource)

Source

pub fn intern_units(&self, codes: &[u16]) -> u32

intern utf-16 code units

Source

pub fn intern_canonical(&self, start: u32, end: u32) -> u32

intern a range of the canonical text

Source

pub fn set_source_text(&self, source: usize, codes: CodeUnits)

set the code units of a source: the strings interned from the source are ranges of it

Source

pub fn release_source_codes(&self, source: usize)

Source

pub fn source_text(&self, source: usize) -> Option<CodeUnits>

code units of a source

Source

pub fn string_id(&self, str: &str) -> i32

id of an interned string, -1 when the string is not interned

Source

pub fn string_length(&self, id: u32) -> usize

Source

pub fn string_code_at(&self, id: u32, index: usize) -> u32

Source

pub fn string_index_of(&self, id: u32, code: u32) -> isize

Source

pub fn string_equals(&self, id: u32, str: &str) -> bool

Source

pub fn string_equals_ignore_case(&self, id: u32, str: &str) -> bool

Source

pub fn string_starts_with(&self, id: u32, str: &str) -> bool

Source

pub fn lower_string_id(&self, id: u32) -> u32

id of the lowercase version of an interned string

Source

pub fn copy_string_ascii(&self, id: u32, target: &mut Vec<u8>) -> isize

append an interned string to a byte buffer when it is ascii. -1 when it is not ascii

Source

pub fn string_flag( &self, id: u32, flag: StringFlag, compute: impl FnOnce(&str) -> bool, ) -> bool

a boolean property of an interned string, computed once per string id

Source

pub fn string_flag_id( &self, id: u32, flag: StringFlag, compute: impl FnOnce(u32) -> bool, ) -> bool

a boolean property of an interned string, computed once per string id from the id

Source

pub fn string_derived<T: Clone + 'static>( &self, id: u32, slot: StringDerived, compute: impl FnOnce(u32) -> T, ) -> T

a value derived from an interned string, computed once per string id

Source

pub fn copy_string(&self, id: u32, target: &mut Vec<u16>) -> usize

append the code units of an interned string to a buffer

Source

pub fn append_string<'b>( &self, builder: &'b mut StringBuilder, id: u32, ) -> &'b mut StringBuilder

append an interned string to a builder

Source

pub fn intern_builder(&self, builder: &StringBuilder) -> u32

intern the string built by a builder

Source

pub fn string_epoch(&self) -> u64

identity of the string ids: changed when the strings are renumbered

Source

pub fn with_string<R>(&self, id: u32, f: impl FnOnce(&str) -> R) -> R

run a function with an interned string. ascii ranges of a source are borrowed: no string is created

Source

pub fn resolve_string(&self, id: u32) -> SharedStr

the string of an id (created and cached the first time it is read)

Source

pub fn get_type(&self, id: u32) -> u8

Source

pub fn type_of(&self, id: Option<u32>) -> Option<u8>

type of a node or a token. None when the id is None

Source

pub fn set_type(&self, id: u32, typ: u8)

Source

pub fn set_name(&self, id: u32, name_id: u32)

Source

pub fn get_name(&self, id: u32) -> SharedStr

Source

pub fn set_value(&self, id: u32, value_id: u32)

Source

pub fn get_value(&self, id: u32) -> SharedStr

Source

pub fn set_numeric_value(&self, id: u32, value: f64)

Source

pub fn set_unit(&self, id: u32, unit: u32)

Source

pub fn get_unit(&self, id: u32) -> SharedStr

Source

pub fn get_numeric_value(&self, id: u32) -> f64

Source

pub fn set_parent(&self, id: u32, parent: u32)

Source

pub fn get_first_child(&self, id: u32) -> u32

Source

pub fn get_next_sibling(&self, id: u32) -> u32

Source

pub fn get_previous_sibling(&self, id: u32) -> u32

Source

pub fn get_first_token(&self, id: u32) -> u32

Source

pub fn get_last_token(&self, id: u32) -> u32

Source

pub fn get_capacity(&self) -> usize

number of nodes the columns can hold

Source

pub fn get_last_id(&self) -> u32

Source

pub fn memory_size(&self) -> usize

approximate number of bytes held by the arena: the columns (62 bytes per node of the capacity) and the string table

Source

pub fn get_string_count(&self) -> u32

Source

pub fn set_first_child(&self, id: u32, first_child_id: u32)

replace the first child

Source

pub fn get_last_child(&self, id: u32) -> u32

Source

pub fn set_last_child(&self, id: u32, last_child_id: u32)

replace the last child

Source

pub fn set_next_sibling(&self, id: u32, next_sibling_id: u32)

insert a node after another one

Source

pub fn set_previous_sibling(&self, id: u32, previous_sibling_id: u32)

insert a node before another one

Source

pub fn set_first_token(&self, id: u32, first_token_id: u32)

replace the first token

Source

pub fn set_color_type(&self, id: u32, color_type: u8)

Source

pub fn get_color_type(&self, id: u32) -> u8

Source

pub fn set_flags(&self, id: u32, flags: u32)

Source

pub fn get_flags(&self, id: u32) -> u32

Source

pub fn has_flags(&self, id: u32, flag: u32) -> bool

Source

pub fn get_child_at(&self, id: u32, index: u32) -> Option<u32>

child at an index

Source

pub fn get_token_at(&self, id: u32, index: u32) -> Option<u32>

token at an index of the token list

Source

pub fn set_child_at(&self, id: u32, index: u32, child: u32)

replace the child at an index. the node is appended when the index is out of range

Source

pub fn field_list(&self, id: u32, field: u32) -> u32

list node holding the tokens of a structured token field. created when missing

Source

pub fn has_field(&self, id: u32, field: u32) -> bool

true if the field exists and is not empty

Source

pub fn field(&self, id: u32, field: u32) -> u32

first token of a structured token field

Source

pub fn field_tokens(&self, id: u32, field: u32) -> Vec<u32>

tokens of a structured token field

Source

pub fn set_field(&self, id: u32, field: u32, tokens: &[u32])

set the tokens of a structured token field

Source

pub fn append_field(&self, id: u32, field: u32, tokens: &[u32])

append tokens to a structured token field

Source

pub fn set_left(&self, id: u32, children: &[u32])

Source

pub fn get_left(&self, id: u32) -> Option<Vec<u32>>

Source

pub fn set_right(&self, id: u32, right: &[u32])

Source

pub fn get_right(&self, id: u32) -> Option<Vec<u32>>

Source

pub fn set_op(&self, id: u32, op: u32)

Source

pub fn get_op(&self, id: u32) -> u32

Source

pub fn set_op2(&self, id: u32, op2: u32)

Source

pub fn get_op2(&self, id: u32) -> u32

Source

pub fn set_span(&self, id: u32, source_id: u32, start: u32, end: u32)

Source

pub fn copy_span(&self, target: u32, source: u32)

copy the packed location of a node

Source

pub fn get_source_id(&self, id: u32) -> u32

Source

pub fn get_span_start(&self, id: u32) -> u32

Source

pub fn set_span_start(&self, id: u32, start: u32)

(sic: the typescript name is setSpandStart)

Source

pub fn get_span_end(&self, id: u32) -> u32

Source

pub fn set_span_end(&self, id: u32, end: u32)

Source

pub fn get_parent(&self, id: u32) -> u32

Source

pub fn detach(&self, id: u32)

detach a node from its parent

Source

pub fn walk<'f>( &self, node: u32, filter: Option<WalkerFilter<'f>>, reverse: bool, ) -> NodeWalker<'f>

walk the ast nodes: the node and its descendants, in document order

Source

pub fn walk_with<'f>( &self, node: u32, options: WalkerOptions<'f>, ) -> NodeWalker<'f>

walk the ast nodes with options

Source

pub fn walk_values<'f>( &self, node: u32, options: WalkerValueOptions<'f>, reverse: bool, ) -> ValueWalker<'f>

walk the value tokens of a node / token and their descendants

Source

pub fn walk_token_values<'f>( &self, tokens: &[u32], options: WalkerValueOptions<'f>, reverse: bool, ) -> ValueWalker<'f>

walk tokens and their descendants

Source

pub fn walk_ids(&self, node: u32) -> Vec<u32>

node ids of a subtree in document order (collected)

Source

pub fn visit_values(&self, tokens: &[u32], visitor: impl FnMut(&Self, u32))

call the visitor for the tokens and their children, in the order of Arena::walk_values (the children of a token are read after the visitor returns)

Source

pub fn get_value_parent(&self, id: u32) -> u32

parent of a value token: the node / token that owns the token list, or the structured token that owns the field (the field list token is skipped). 0 when the token is detached

Source

pub fn get_value_owner(&self, id: u32) -> u32

node that owns a value token: the declaration, the rule or the at-rule. 0 when the token is detached

Source

pub fn find( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>

first match of a subtree, in document order

Source

pub fn find_all( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Vec<u32>

all the matches of a subtree, in document order

Source

pub fn find_last( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>

last match of a subtree in document order

Source

pub fn find_by_value( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<(u32, u32)>

first value token match of a subtree: the node that owns the token and the token

Source

pub fn find_value( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>

first match of a token and its descendants, or of the value tokens of a node

Source

pub fn sub_tokens(&self, id: u32) -> Vec<u32>

direct value tokens of a node or a token: the token list, then the tokens of the structured fields and the child tokens

Source

pub fn find_child( &self, id: u32, predicate: impl FnMut(u32) -> bool, ) -> Option<u32>

Source

pub fn find_token( &self, id: u32, predicate: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>

find a token in the token list

Source

pub fn siblings( &self, id: u32, filter: Option<&mut dyn FnMut(&Self, u32) -> bool>, ) -> Vec<u32>

the children of the parent of a node, the node included

Source

pub fn children(&self, id: u32) -> Vec<u32>

child node ids

Source

pub fn children_filtered( &self, id: u32, filter: impl FnMut(&Self, u32) -> bool, ) -> Vec<u32>

child node ids accepted by a filter

Source

pub fn tokens(&self, id: u32) -> Vec<u32>

token ids of the token list

Source

pub fn tokens_filtered( &self, id: u32, filter: impl FnMut(&Self, u32) -> bool, ) -> Vec<u32>

token ids of the token list accepted by a filter

Source

pub fn token_iter(&self, id: u32) -> impl Iterator<Item = u32> + '_

iterate the token list without allocation. the next token is read before the current one is returned

Source

pub fn child_iter(&self, id: u32) -> impl Iterator<Item = u32> + '_

iterate the children without allocation. the next child is read before the current one is returned

Source

pub fn token_count(&self, id: u32) -> u32

number of tokens in the token list

Source

pub fn set_children(&self, id: u32, children: &[u32])

Source

pub fn set_child_list(&self, id: u32, children: &[u32])

replace the children

Source

pub fn set_tokens(&self, id: u32, tokens: &[u32])

Source

pub fn set_token_list(&self, id: u32, tokens: &[u32])

replace the token list

Source

pub fn move_children(&self, from: u32, to: u32, before: u32)

move the children of a node into another node, in order

Source

pub fn append_children(&self, id: u32, children: &[u32])

Source

pub fn append_child_list(&self, id: u32, children: &[u32])

append children

Source

pub fn append_tokens(&self, id: u32, tokens: &[u32])

Source

pub fn append_token_list(&self, id: u32, tokens: &[u32])

append tokens

Source

pub fn append_child(&self, id: u32, child: u32)

append a single child node

Source

pub fn append_token(&self, id: u32, token: u32)

append a single token

Source

pub fn prepend_tokens(&self, id: u32, tokens: &[u32])

prepend tokens to the token list

Source

pub fn equals_ignore_case(&self, node: u32, key: &str, string: &str) -> bool

key is “nam” or “val”

Source

pub fn starts_with(&self, node: u32, key: &str, string: &str) -> bool

key is “nam” or “val”

Source

pub fn resize(&self, new_size: usize)

grow the columns

Source

pub fn compact_strings(&self)

drop the interned strings that are no longer referenced by any node

Source

pub fn compact(&self, errors: Option<&mut [ErrorDescription]>) -> Vec<u32>

drop the nodes that are not reachable from the root and renumber the remaining nodes in document order. the unused strings are dropped too. returns the mapping of the old ids to the new ids (0 when dropped)

Source

pub fn import_columns(&self, t: ArenaTransferables)

replace the columns

Source

pub fn mark(&self, ids: &[u32])

mark nodes to be released or trimmed later

Source

pub fn is_marked(&self, id: u32) -> bool

Source

pub fn release(&self, id: Option<u32>, deep: bool)

release nodes: detach them and drop their data. None releases the marked nodes

Source

pub fn trim(&self, errors: Option<&mut [ErrorDescription]>) -> Vec<u32>

reclaim the memory: release the marked nodes, then compact

Source

pub fn set_errors(&self, id: u32, errors: Option<Vec<ErrorDescription>>)

Source

pub fn get_errors(&self, id: u32) -> Option<Vec<ErrorDescription>>

Source

pub fn child_count(&self, id: u32) -> u32

number of children

Source

pub fn export_properties(&self) -> ArenaProperties

properties of the arena

Source

pub fn export_transferables(&self) -> ArenaTransferables

copy of the columns

Source

pub fn take_transferables(&self) -> ArenaTransferables

move the columns out (transfer): the arena can no longer be used

Source

pub fn import_properties( &self, properties: &ArenaProperties, transferables: ArenaTransferables, )

initialize the arena from exported properties and column buffers

Trait Implementations§

Source§

impl Default for Arena

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Arena

§

impl !RefUnwindSafe for Arena

§

impl !Send for Arena

§

impl !Sync for Arena

§

impl !UnwindSafe for Arena

§

impl Forget for Arena

§

impl Move for Arena

§

impl Unpin for Arena

§

impl UnsafeUnpin for Arena

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.