From ae849cf134f3ff21261e175d95ba994e86ffe81a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 03:21:17 +0300 Subject: Owned&Borrowed nodes HOLLY COW, UNLIKE C++, WE CAN BE GENERIC WRT OWNERSHIP/BORROWING, SO WE CAN BOTH MAKE SYNTAX NODES OWNED (WHICH IS CONVENIENT) AND BORROWED (WHICH IS CONVENIENT FOR LOCAL PROCESSING, BC YOU DON'T NEED TO BUMP REFCOUNTS). --- src/yellow/syntax.rs | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'src/yellow/syntax.rs') diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index cc6122689..0fa24fb47 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs @@ -10,17 +10,28 @@ use { yellow::{RedNode, GreenNode}, }; -#[derive(Clone)] -pub struct SyntaxNode { - pub(crate) root: SyntaxRoot, +#[derive(Clone, Copy)] +pub struct SyntaxNode + Clone = Arc> { + pub(crate) root: ROOT, // guaranteed to be alive bc SyntaxRoot holds a strong ref red: ptr::NonNull, } -#[derive(Clone)] +pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; + +#[derive(Debug)] pub struct SyntaxRoot { - red: Arc, - pub(crate) errors: Arc>, + red: RedNode, + pub(crate) errors: Vec, +} + +impl SyntaxRoot { + pub(crate) fn new(green: GreenNode, errors: Vec) -> SyntaxRoot { + SyntaxRoot { + red: RedNode::new_root(green), + errors, + } + } } #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] @@ -29,13 +40,21 @@ pub(crate) struct SyntaxError { pub(crate) offset: TextUnit, } -impl SyntaxNode { - pub(crate) fn new(root: GreenNode, errors: Vec) -> SyntaxNode { - let red = Arc::new(RedNode::new_root(root)); - let red_weak: ptr::NonNull = (&*red).into(); - let root = SyntaxRoot { red, errors: Arc::new(errors) }; +impl SyntaxNode> { + pub(crate) fn new_owned(root: SyntaxRoot) -> Self { + let root = Arc::new(root); + let red_weak = ptr::NonNull::from(&root.red); SyntaxNode { root, red: red_weak } } +} + +impl + Clone> SyntaxNode { + pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { + SyntaxNode { + root: &*self.root, + red: ptr::NonNull::clone(&self.red), + } + } pub fn kind(&self) -> SyntaxKind { self.red().green().kind() @@ -53,7 +72,7 @@ impl SyntaxNode { self.red().green().text() } - pub fn children(&self) -> Vec { + pub fn children(&self) -> Vec> { let red = self.red(); let n_children = red.n_children(); let mut res = Vec::with_capacity(n_children); @@ -71,7 +90,7 @@ impl SyntaxNode { } } -impl fmt::Debug for SyntaxNode { +impl + Clone> fmt::Debug for SyntaxNode { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; if has_short_text(self.kind()) { -- cgit v1.2.3