diff options
Diffstat (limited to 'crates/ra_syntax/src/ptr.rs')
-rw-r--r-- | crates/ra_syntax/src/ptr.rs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index 8665c8976..992034ef0 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs | |||
@@ -31,6 +31,13 @@ impl SyntaxNodePtr { | |||
31 | pub fn kind(self) -> SyntaxKind { | 31 | pub fn kind(self) -> SyntaxKind { |
32 | self.kind | 32 | self.kind |
33 | } | 33 | } |
34 | |||
35 | pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> { | ||
36 | if !N::can_cast(self.kind()) { | ||
37 | return None; | ||
38 | } | ||
39 | Some(AstPtr { raw: self, _ty: PhantomData }) | ||
40 | } | ||
34 | } | 41 | } |
35 | 42 | ||
36 | /// Like `SyntaxNodePtr`, but remembers the type of node | 43 | /// Like `SyntaxNodePtr`, but remembers the type of node |
@@ -80,7 +87,7 @@ fn test_local_syntax_ptr() { | |||
80 | use crate::{ast, AstNode, SourceFile}; | 87 | use crate::{ast, AstNode, SourceFile}; |
81 | 88 | ||
82 | let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap(); | 89 | let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap(); |
83 | let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); | 90 | let field = file.syntax().descendants().find_map(ast::RecordFieldDef::cast).unwrap(); |
84 | let ptr = SyntaxNodePtr::new(field.syntax()); | 91 | let ptr = SyntaxNodePtr::new(field.syntax()); |
85 | let field_syntax = ptr.to_node(file.syntax()); | 92 | let field_syntax = ptr.to_node(file.syntax()); |
86 | assert_eq!(field.syntax(), &field_syntax); | 93 | assert_eq!(field.syntax(), &field_syntax); |