// A map from AST node to `ValueT`, to associate extra data to AST node. #[derive(Debug)] pubstruct AssociatedData<ValueT> {
items: Vec<Item<ValueT>>,
map: HashMap<Key, ItemIndex>,
}
// Insert an item for `node`. // Returns the index of the inserted item. pubfn insert<NodeT>(&mutself, node: &NodeT, value: ValueT) -> AssociatedDataItemIndex where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{ let index = self.items.len(); let key = Key::new(node); self.items.push(Item::new(key.clone(), value)); self.map.insert(key, index);
AssociatedDataItemIndex::new(index)
}
// Get the immutable reference of the item for the index. // The index is the return value of `insert` or `to_index`. pubfn get_by_index(&self, index: AssociatedDataItemIndex) -> &ValueT { // NOTE: This can panic if `index` is created by another instance of // `AssociatedData`.
&self.items[index.index].value
}
// Get the mutable reference of the item for the index. // The index is the return value of `insert` or `to_index`. pubfn get_mut_by_index(&mutself, index: AssociatedDataItemIndex) -> &mut ValueT { // NOTE: This can panic if `index` is created by another instance of // `AssociatedData`.
&mutself.items[index.index].value
}
// Get the immutable reference of the item for `node`. // `None` if there's no item inserted for `node`. pubfn get<NodeT>(&self, node: &NodeT) -> Option<&ValueT> where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{ self.to_index(node)
.and_then(|index| Some(self.get_by_index(index)))
}
// Get the mutable reference of the item for `node`. // `None` if there's no item inserted for `node`. pubfn get_mut<NodeT>(&mutself, node: &NodeT) -> Option<&mut ValueT> where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{ self.to_index(node)
.and_then(move |index| Some(self.get_mut_by_index(index)))
}
// Returns the index for the item for `node`. // `None` if there's no item inserted for `node`. pubfn to_index<NodeT>(&self, node: &NodeT) -> Option<AssociatedDataItemIndex> where
NodeT: SourceLocationAccessor + NodeTypeIdAccessor,
{ let key = Key::new(node); matchself.map.get(&key) {
Some(index) => Some(AssociatedDataItemIndex::new(*index)),
None => None,
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-06-20)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.