mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Trait for tree root
This commit is contained in:
parent
ae849cf134
commit
423298dddd
@ -1,7 +1,8 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
ptr
|
ptr,
|
||||||
|
ops::Deref,
|
||||||
};
|
};
|
||||||
|
|
||||||
use {
|
use {
|
||||||
@ -10,8 +11,12 @@ use {
|
|||||||
yellow::{RedNode, GreenNode},
|
yellow::{RedNode, GreenNode},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone {}
|
||||||
|
impl TreeRoot for Arc<SyntaxRoot> {}
|
||||||
|
impl<'a> TreeRoot for &'a SyntaxRoot {}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct SyntaxNode<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone = Arc<SyntaxRoot>> {
|
pub struct SyntaxNode<ROOT: TreeRoot = Arc<SyntaxRoot>> {
|
||||||
pub(crate) root: ROOT,
|
pub(crate) root: ROOT,
|
||||||
// guaranteed to be alive bc SyntaxRoot holds a strong ref
|
// guaranteed to be alive bc SyntaxRoot holds a strong ref
|
||||||
red: ptr::NonNull<RedNode>,
|
red: ptr::NonNull<RedNode>,
|
||||||
@ -48,7 +53,7 @@ impl SyntaxNode<Arc<SyntaxRoot>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone> SyntaxNode<ROOT> {
|
impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
|
||||||
pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> {
|
pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> {
|
||||||
SyntaxNode {
|
SyntaxNode {
|
||||||
root: &*self.root,
|
root: &*self.root,
|
||||||
@ -90,7 +95,7 @@ impl<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone> SyntaxNode<ROOT> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone> fmt::Debug for SyntaxNode<ROOT> {
|
impl<ROOT: TreeRoot> fmt::Debug for SyntaxNode<ROOT> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
|
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
|
||||||
if has_short_text(self.kind()) {
|
if has_short_text(self.kind()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user