Skip to content

Commit

Permalink
Merge pull request #161 from mattfbacon/main
Browse files Browse the repository at this point in the history
Use niching for Checkpoint
  • Loading branch information
Veykril authored Jan 24, 2024
2 parents e732108 + bfd10e4 commit 2e75f11
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/green/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::NonZeroUsize;

use crate::{
cow_mut::CowMut,
green::{node_cache::NodeCache, GreenElement, GreenNode, SyntaxKind},
Expand All @@ -6,7 +8,17 @@ use crate::{

/// A checkpoint for maybe wrapping a node. See `GreenNodeBuilder::checkpoint` for details.
#[derive(Clone, Copy, Debug)]
pub struct Checkpoint(usize);
pub struct Checkpoint(NonZeroUsize);

impl Checkpoint {
fn new(inner: usize) -> Self {
Self(NonZeroUsize::new(inner + 1).unwrap())
}

fn into_inner(self) -> usize {
self.0.get() - 1
}
}

/// A builder for a green tree.
#[derive(Default, Debug)]
Expand Down Expand Up @@ -82,14 +94,14 @@ impl GreenNodeBuilder<'_> {
/// ```
#[inline]
pub fn checkpoint(&self) -> Checkpoint {
Checkpoint(self.children.len())
Checkpoint::new(self.children.len())
}

/// Wrap the previous branch marked by `checkpoint` in a new branch and
/// make it current.
#[inline]
pub fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind) {
let Checkpoint(checkpoint) = checkpoint;
let checkpoint = checkpoint.into_inner();
assert!(
checkpoint <= self.children.len(),
"checkpoint no longer valid, was finish_node called early?"
Expand Down

0 comments on commit 2e75f11

Please sign in to comment.