mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-09 03:50:47 +00:00
introduced better typed AstPtr
This commit is contained in:
parent
e22b6edae5
commit
d4ed25d86f
3 changed files with 40 additions and 9 deletions
|
@ -1,3 +1,5 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
|
||||
algo::generate,
|
||||
|
@ -37,6 +39,38 @@ impl SyntaxNodePtr {
|
|||
}
|
||||
}
|
||||
|
||||
/// Like `SyntaxNodePtr`, but remembers the type of node
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct AstPtr<N: AstNode> {
|
||||
ptr: SyntaxNodePtr,
|
||||
_ty: PhantomData<N>,
|
||||
}
|
||||
|
||||
impl<N: AstNode> Copy for AstPtr<N> {}
|
||||
impl<N: AstNode> Clone for AstPtr<N> {
|
||||
fn clone(&self) -> AstPtr<N> {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: AstNode> AstPtr<N> {
|
||||
pub fn new(node: &N) -> AstPtr<N> {
|
||||
AstPtr {
|
||||
ptr: SyntaxNodePtr::new(node.syntax()),
|
||||
_ty: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_node(self, source_file: &SourceFile) -> &N {
|
||||
let syntax_node = self.ptr.to_node(source_file);
|
||||
N::cast(syntax_node).unwrap()
|
||||
}
|
||||
|
||||
pub fn syntax_node_ptr(self) -> SyntaxNodePtr {
|
||||
self.ptr
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_local_syntax_ptr() {
|
||||
use crate::{ast, AstNode};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue