Skip to main content

ty_python_core/
node_key.rs

1use ruff_python_ast::{HasNodeIndex, NodeIndex};
2
3use crate::ast_node_ref::AstNodeRef;
4
5/// Compact key for a node for use in a hash map.
6#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, get_size2::GetSize)]
7pub struct NodeKey(NodeIndex);
8
9impl NodeKey {
10    /// Returns the index of the AST node.
11    pub fn index(self) -> NodeIndex {
12        self.0
13    }
14
15    pub fn from_node<N>(node: N) -> Self
16    where
17        N: HasNodeIndex,
18    {
19        NodeKey(node.node_index().load())
20    }
21
22    pub fn from_node_ref<T>(node_ref: &AstNodeRef<T>) -> Self {
23        NodeKey(node_ref.index())
24    }
25}