mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-21 04:03:11 +00:00
make ast object safe
This commit is contained in:
parent
e055cfacdf
commit
bbcca4f735
@ -19,7 +19,10 @@ pub struct AstEditor<N: AstNode> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<N: AstNode> AstEditor<N> {
|
impl<N: AstNode> AstEditor<N> {
|
||||||
pub fn new(node: N) -> AstEditor<N> {
|
pub fn new(node: N) -> AstEditor<N>
|
||||||
|
where
|
||||||
|
N: Clone,
|
||||||
|
{
|
||||||
AstEditor { original_ast: node.clone(), ast: node }
|
AstEditor { original_ast: node.clone(), ast: node }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,7 +382,7 @@ impl AstBuilder<ast::MatchArmList> {
|
|||||||
|
|
||||||
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
||||||
let parse = SourceFile::parse(text);
|
let parse = SourceFile::parse(text);
|
||||||
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned();
|
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,15 +25,23 @@ pub use self::{
|
|||||||
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
|
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
|
||||||
/// the same representation: a pointer to the tree root and a pointer to the
|
/// the same representation: a pointer to the tree root and a pointer to the
|
||||||
/// node itself.
|
/// node itself.
|
||||||
pub trait AstNode: Clone {
|
pub trait AstNode {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool;
|
fn can_cast(kind: SyntaxKind) -> bool
|
||||||
|
where
|
||||||
|
Self: Sized;
|
||||||
|
|
||||||
fn cast(syntax: SyntaxNode) -> Option<Self>
|
fn cast(syntax: SyntaxNode) -> Option<Self>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
fn syntax(&self) -> &SyntaxNode;
|
fn syntax(&self) -> &SyntaxNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn assert_ast_is_object_safe() {
|
||||||
|
fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
|
||||||
|
}
|
||||||
|
|
||||||
/// Like `AstNode`, but wraps tokens rather than interior nodes.
|
/// Like `AstNode`, but wraps tokens rather than interior nodes.
|
||||||
pub trait AstToken {
|
pub trait AstToken {
|
||||||
fn cast(token: SyntaxToken) -> Option<Self>
|
fn cast(token: SyntaxToken) -> Option<Self>
|
||||||
|
Loading…
Reference in New Issue
Block a user