introduced better typed AstPtr

This commit is contained in:
Aleksey Kladov 2019-01-23 18:26:02 +03:00
parent e22b6edae5
commit d4ed25d86f
3 changed files with 40 additions and 9 deletions

View file

@ -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};