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: u32root 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
impl Arena
pub fn new(size: usize) -> Self
pub fn set_current(&self, current: u32)
Sourcepub fn generation(&self) -> u32
pub fn generation(&self) -> u32
incremented when the columns are reallocated
Sourcepub fn generation_ptr(&self) -> *const u32
pub fn generation_ptr(&self) -> *const u32
address of the generation counter: the javascript views read it to detect a growth of the columns
Sourcepub fn column_raw(&self, index: u32) -> (*const u8, usize)
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
Sourcepub fn transferred(&self) -> bool
pub fn transferred(&self) -> bool
true when the column buffers have been transferred: the arena can no longer be used
pub fn allocate(&self) -> u32
Sourcepub fn recycle(&self, id: u32)
pub fn recycle(&self, id: u32)
recycle a detached leaf node that is no longer referenced: its id is reused by the next allocation
Sourcepub fn intern_string(&self, str: &str) -> u32
pub fn intern_string(&self, str: &str) -> u32
intern a string, returns its id
intern a shared string: a shared Rc<str> is not copied
Sourcepub fn intern_range(
&self,
codes: &CodeUnits,
start: usize,
end: usize,
source: i32,
) -> u32
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)
Sourcepub fn intern_units(&self, codes: &[u16]) -> u32
pub fn intern_units(&self, codes: &[u16]) -> u32
intern utf-16 code units
Sourcepub fn intern_canonical(&self, start: u32, end: u32) -> u32
pub fn intern_canonical(&self, start: u32, end: u32) -> u32
intern a range of the canonical text
Sourcepub fn set_source_text(&self, source: usize, codes: CodeUnits)
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
pub fn release_source_codes(&self, source: usize)
Sourcepub fn source_text(&self, source: usize) -> Option<CodeUnits>
pub fn source_text(&self, source: usize) -> Option<CodeUnits>
code units of a source
Sourcepub fn string_id(&self, str: &str) -> i32
pub fn string_id(&self, str: &str) -> i32
id of an interned string, -1 when the string is not interned
pub fn string_length(&self, id: u32) -> usize
pub fn string_code_at(&self, id: u32, index: usize) -> u32
pub fn string_index_of(&self, id: u32, code: u32) -> isize
pub fn string_equals(&self, id: u32, str: &str) -> bool
pub fn string_equals_ignore_case(&self, id: u32, str: &str) -> bool
pub fn string_starts_with(&self, id: u32, str: &str) -> bool
Sourcepub fn lower_string_id(&self, id: u32) -> u32
pub fn lower_string_id(&self, id: u32) -> u32
id of the lowercase version of an interned string
Sourcepub fn copy_string_ascii(&self, id: u32, target: &mut Vec<u8>) -> isize
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
Sourcepub fn string_flag(
&self,
id: u32,
flag: StringFlag,
compute: impl FnOnce(&str) -> bool,
) -> bool
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
Sourcepub fn string_flag_id(
&self,
id: u32,
flag: StringFlag,
compute: impl FnOnce(u32) -> bool,
) -> bool
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
Sourcepub fn string_derived<T: Clone + 'static>(
&self,
id: u32,
slot: StringDerived,
compute: impl FnOnce(u32) -> T,
) -> T
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
Sourcepub fn copy_string(&self, id: u32, target: &mut Vec<u16>) -> usize
pub fn copy_string(&self, id: u32, target: &mut Vec<u16>) -> usize
append the code units of an interned string to a buffer
Sourcepub fn append_string<'b>(
&self,
builder: &'b mut StringBuilder,
id: u32,
) -> &'b mut StringBuilder
pub fn append_string<'b>( &self, builder: &'b mut StringBuilder, id: u32, ) -> &'b mut StringBuilder
append an interned string to a builder
Sourcepub fn intern_builder(&self, builder: &StringBuilder) -> u32
pub fn intern_builder(&self, builder: &StringBuilder) -> u32
intern the string built by a builder
Sourcepub fn string_epoch(&self) -> u64
pub fn string_epoch(&self) -> u64
identity of the string ids: changed when the strings are renumbered
Sourcepub fn with_string<R>(&self, id: u32, f: impl FnOnce(&str) -> R) -> R
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
Sourcepub fn resolve_string(&self, id: u32) -> SharedStr
pub fn resolve_string(&self, id: u32) -> SharedStr
the string of an id (created and cached the first time it is read)
pub fn get_type(&self, id: u32) -> u8
Sourcepub fn type_of(&self, id: Option<u32>) -> Option<u8>
pub fn type_of(&self, id: Option<u32>) -> Option<u8>
type of a node or a token. None when the id is None
pub fn set_type(&self, id: u32, typ: u8)
pub fn set_name(&self, id: u32, name_id: u32)
pub fn get_name(&self, id: u32) -> SharedStr
pub fn set_value(&self, id: u32, value_id: u32)
pub fn get_value(&self, id: u32) -> SharedStr
pub fn set_numeric_value(&self, id: u32, value: f64)
pub fn set_unit(&self, id: u32, unit: u32)
pub fn get_unit(&self, id: u32) -> SharedStr
pub fn get_numeric_value(&self, id: u32) -> f64
pub fn set_parent(&self, id: u32, parent: u32)
pub fn get_first_child(&self, id: u32) -> u32
pub fn get_next_sibling(&self, id: u32) -> u32
pub fn get_previous_sibling(&self, id: u32) -> u32
pub fn get_first_token(&self, id: u32) -> u32
pub fn get_last_token(&self, id: u32) -> u32
Sourcepub fn get_capacity(&self) -> usize
pub fn get_capacity(&self) -> usize
number of nodes the columns can hold
pub fn get_last_id(&self) -> u32
Sourcepub fn memory_size(&self) -> usize
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
pub fn get_string_count(&self) -> u32
Sourcepub fn set_first_child(&self, id: u32, first_child_id: u32)
pub fn set_first_child(&self, id: u32, first_child_id: u32)
replace the first child
pub fn get_last_child(&self, id: u32) -> u32
Sourcepub fn set_last_child(&self, id: u32, last_child_id: u32)
pub fn set_last_child(&self, id: u32, last_child_id: u32)
replace the last child
Sourcepub fn set_next_sibling(&self, id: u32, next_sibling_id: u32)
pub fn set_next_sibling(&self, id: u32, next_sibling_id: u32)
insert a node after another one
Sourcepub fn set_previous_sibling(&self, id: u32, previous_sibling_id: u32)
pub fn set_previous_sibling(&self, id: u32, previous_sibling_id: u32)
insert a node before another one
Sourcepub fn set_first_token(&self, id: u32, first_token_id: u32)
pub fn set_first_token(&self, id: u32, first_token_id: u32)
replace the first token
pub fn set_color_type(&self, id: u32, color_type: u8)
pub fn get_color_type(&self, id: u32) -> u8
pub fn set_flags(&self, id: u32, flags: u32)
pub fn get_flags(&self, id: u32) -> u32
pub fn has_flags(&self, id: u32, flag: u32) -> bool
Sourcepub fn get_token_at(&self, id: u32, index: u32) -> Option<u32>
pub fn get_token_at(&self, id: u32, index: u32) -> Option<u32>
token at an index of the token list
Sourcepub fn set_child_at(&self, id: u32, index: u32, child: u32)
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
Sourcepub fn field_list(&self, id: u32, field: u32) -> u32
pub fn field_list(&self, id: u32, field: u32) -> u32
list node holding the tokens of a structured token field. created when missing
Sourcepub fn set_field(&self, id: u32, field: u32, tokens: &[u32])
pub fn set_field(&self, id: u32, field: u32, tokens: &[u32])
set the tokens of a structured token field
Sourcepub fn append_field(&self, id: u32, field: u32, tokens: &[u32])
pub fn append_field(&self, id: u32, field: u32, tokens: &[u32])
append tokens to a structured token field
pub fn set_left(&self, id: u32, children: &[u32])
pub fn get_left(&self, id: u32) -> Option<Vec<u32>>
pub fn set_right(&self, id: u32, right: &[u32])
pub fn get_right(&self, id: u32) -> Option<Vec<u32>>
pub fn set_op(&self, id: u32, op: u32)
pub fn get_op(&self, id: u32) -> u32
pub fn set_op2(&self, id: u32, op2: u32)
pub fn get_op2(&self, id: u32) -> u32
pub fn set_span(&self, id: u32, source_id: u32, start: u32, end: u32)
pub fn get_source_id(&self, id: u32) -> u32
pub fn get_span_start(&self, id: u32) -> u32
Sourcepub fn set_span_start(&self, id: u32, start: u32)
pub fn set_span_start(&self, id: u32, start: u32)
(sic: the typescript name is setSpandStart)
pub fn get_span_end(&self, id: u32) -> u32
pub fn set_span_end(&self, id: u32, end: u32)
pub fn get_parent(&self, id: u32) -> u32
Sourcepub fn walk<'f>(
&self,
node: u32,
filter: Option<WalkerFilter<'f>>,
reverse: bool,
) -> NodeWalker<'f>
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
Sourcepub fn walk_with<'f>(
&self,
node: u32,
options: WalkerOptions<'f>,
) -> NodeWalker<'f>
pub fn walk_with<'f>( &self, node: u32, options: WalkerOptions<'f>, ) -> NodeWalker<'f>
walk the ast nodes with options
Sourcepub fn walk_values<'f>(
&self,
node: u32,
options: WalkerValueOptions<'f>,
reverse: bool,
) -> ValueWalker<'f>
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
Sourcepub fn walk_token_values<'f>(
&self,
tokens: &[u32],
options: WalkerValueOptions<'f>,
reverse: bool,
) -> ValueWalker<'f>
pub fn walk_token_values<'f>( &self, tokens: &[u32], options: WalkerValueOptions<'f>, reverse: bool, ) -> ValueWalker<'f>
walk tokens and their descendants
Sourcepub fn walk_ids(&self, node: u32) -> Vec<u32>
pub fn walk_ids(&self, node: u32) -> Vec<u32>
node ids of a subtree in document order (collected)
Sourcepub fn visit_values(&self, tokens: &[u32], visitor: impl FnMut(&Self, u32))
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)
Sourcepub fn get_value_parent(&self, id: u32) -> u32
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
Sourcepub fn get_value_owner(&self, id: u32) -> u32
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
Sourcepub fn find(
&self,
node: u32,
matcher: impl FnMut(&Self, u32) -> bool,
) -> Option<u32>
pub fn find( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>
first match of a subtree, in document order
Sourcepub fn find_all(
&self,
node: u32,
matcher: impl FnMut(&Self, u32) -> bool,
) -> Vec<u32>
pub fn find_all( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Vec<u32>
all the matches of a subtree, in document order
Sourcepub fn find_last(
&self,
node: u32,
matcher: impl FnMut(&Self, u32) -> bool,
) -> Option<u32>
pub fn find_last( &self, node: u32, matcher: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>
last match of a subtree in document order
Sourcepub fn find_by_value(
&self,
node: u32,
matcher: impl FnMut(&Self, u32) -> bool,
) -> Option<(u32, u32)>
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
Sourcepub fn find_value(
&self,
node: u32,
matcher: impl FnMut(&Self, u32) -> bool,
) -> Option<u32>
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
Sourcepub fn sub_tokens(&self, id: u32) -> Vec<u32>
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
pub fn find_child( &self, id: u32, predicate: impl FnMut(u32) -> bool, ) -> Option<u32>
Sourcepub fn find_token(
&self,
id: u32,
predicate: impl FnMut(&Self, u32) -> bool,
) -> Option<u32>
pub fn find_token( &self, id: u32, predicate: impl FnMut(&Self, u32) -> bool, ) -> Option<u32>
find a token in the token list
Sourcepub fn siblings(
&self,
id: u32,
filter: Option<&mut dyn FnMut(&Self, u32) -> bool>,
) -> Vec<u32>
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
Sourcepub fn children_filtered(
&self,
id: u32,
filter: impl FnMut(&Self, u32) -> bool,
) -> Vec<u32>
pub fn children_filtered( &self, id: u32, filter: impl FnMut(&Self, u32) -> bool, ) -> Vec<u32>
child node ids accepted by a filter
Sourcepub fn tokens_filtered(
&self,
id: u32,
filter: impl FnMut(&Self, u32) -> bool,
) -> Vec<u32>
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
Sourcepub fn token_iter(&self, id: u32) -> impl Iterator<Item = u32> + '_
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
Sourcepub fn child_iter(&self, id: u32) -> impl Iterator<Item = u32> + '_
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
Sourcepub fn token_count(&self, id: u32) -> u32
pub fn token_count(&self, id: u32) -> u32
number of tokens in the token list
pub fn set_children(&self, id: u32, children: &[u32])
Sourcepub fn set_child_list(&self, id: u32, children: &[u32])
pub fn set_child_list(&self, id: u32, children: &[u32])
replace the children
pub fn set_tokens(&self, id: u32, tokens: &[u32])
Sourcepub fn set_token_list(&self, id: u32, tokens: &[u32])
pub fn set_token_list(&self, id: u32, tokens: &[u32])
replace the token list
Sourcepub fn move_children(&self, from: u32, to: u32, before: u32)
pub fn move_children(&self, from: u32, to: u32, before: u32)
move the children of a node into another node, in order
pub fn append_children(&self, id: u32, children: &[u32])
Sourcepub fn append_child_list(&self, id: u32, children: &[u32])
pub fn append_child_list(&self, id: u32, children: &[u32])
append children
pub fn append_tokens(&self, id: u32, tokens: &[u32])
Sourcepub fn append_token_list(&self, id: u32, tokens: &[u32])
pub fn append_token_list(&self, id: u32, tokens: &[u32])
append tokens
Sourcepub fn append_child(&self, id: u32, child: u32)
pub fn append_child(&self, id: u32, child: u32)
append a single child node
Sourcepub fn append_token(&self, id: u32, token: u32)
pub fn append_token(&self, id: u32, token: u32)
append a single token
Sourcepub fn prepend_tokens(&self, id: u32, tokens: &[u32])
pub fn prepend_tokens(&self, id: u32, tokens: &[u32])
prepend tokens to the token list
Sourcepub fn equals_ignore_case(&self, node: u32, key: &str, string: &str) -> bool
pub fn equals_ignore_case(&self, node: u32, key: &str, string: &str) -> bool
key is “nam” or “val”
Sourcepub fn compact_strings(&self)
pub fn compact_strings(&self)
drop the interned strings that are no longer referenced by any node
Sourcepub fn compact(&self, errors: Option<&mut [ErrorDescription]>) -> Vec<u32>
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)
Sourcepub fn import_columns(&self, t: ArenaTransferables)
pub fn import_columns(&self, t: ArenaTransferables)
replace the columns
pub fn is_marked(&self, id: u32) -> bool
Sourcepub fn release(&self, id: Option<u32>, deep: bool)
pub fn release(&self, id: Option<u32>, deep: bool)
release nodes: detach them and drop their data. None releases the marked nodes
Sourcepub fn trim(&self, errors: Option<&mut [ErrorDescription]>) -> Vec<u32>
pub fn trim(&self, errors: Option<&mut [ErrorDescription]>) -> Vec<u32>
reclaim the memory: release the marked nodes, then compact
pub fn set_errors(&self, id: u32, errors: Option<Vec<ErrorDescription>>)
pub fn get_errors(&self, id: u32) -> Option<Vec<ErrorDescription>>
Sourcepub fn child_count(&self, id: u32) -> u32
pub fn child_count(&self, id: u32) -> u32
number of children
Sourcepub fn export_properties(&self) -> ArenaProperties
pub fn export_properties(&self) -> ArenaProperties
properties of the arena
Sourcepub fn export_transferables(&self) -> ArenaTransferables
pub fn export_transferables(&self) -> ArenaTransferables
copy of the columns
Sourcepub fn take_transferables(&self) -> ArenaTransferables
pub fn take_transferables(&self) -> ArenaTransferables
move the columns out (transfer): the arena can no longer be used
Sourcepub fn import_properties(
&self,
properties: &ArenaProperties,
transferables: ArenaTransferables,
)
pub fn import_properties( &self, properties: &ArenaProperties, transferables: ArenaTransferables, )
initialize the arena from exported properties and column buffers