aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ptr.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-23 15:26:02 +0000
committerAleksey Kladov <[email protected]>2019-01-23 15:26:02 +0000
commitd4ed25d86fdec0ce47199c262af62213b62e4863 (patch)
tree8b6f174292aee253fa6db15184a2ed7b440ca919 /crates/ra_syntax/src/ptr.rs
parente22b6edae5787039dc0b85112702bb8d16a2aa60 (diff)
introduced better typed AstPtr
Diffstat (limited to 'crates/ra_syntax/src/ptr.rs')
-rw-r--r--crates/ra_syntax/src/ptr.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index e8c40e5d3..b50cd8a52 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -1,3 +1,5 @@
1use std::marker::PhantomData;
2
1use crate::{ 3use crate::{
2 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, 4 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
3 algo::generate, 5 algo::generate,
@@ -37,6 +39,38 @@ impl SyntaxNodePtr {
37 } 39 }
38} 40}
39 41
42/// Like `SyntaxNodePtr`, but remembers the type of node
43#[derive(Debug, PartialEq, Eq, Hash)]
44pub struct AstPtr<N: AstNode> {
45 ptr: SyntaxNodePtr,
46 _ty: PhantomData<N>,
47}
48
49impl<N: AstNode> Copy for AstPtr<N> {}
50impl<N: AstNode> Clone for AstPtr<N> {
51 fn clone(&self) -> AstPtr<N> {
52 *self
53 }
54}
55
56impl<N: AstNode> AstPtr<N> {
57 pub fn new(node: &N) -> AstPtr<N> {
58 AstPtr {
59 ptr: SyntaxNodePtr::new(node.syntax()),
60 _ty: PhantomData,
61 }
62 }
63
64 pub fn to_node(self, source_file: &SourceFile) -> &N {
65 let syntax_node = self.ptr.to_node(source_file);
66 N::cast(syntax_node).unwrap()
67 }
68
69 pub fn syntax_node_ptr(self) -> SyntaxNodePtr {
70 self.ptr
71 }
72}
73
40#[test] 74#[test]
41fn test_local_syntax_ptr() { 75fn test_local_syntax_ptr() {
42 use crate::{ast, AstNode}; 76 use crate::{ast, AstNode};