diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 172ef26724..5d70558241 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -280,7 +280,7 @@ fn convert_tokens(conv: &mut C) -> tt::Subtree { parent.subtree.token_trees.extend(entry.subtree.token_trees); } - let subtree = stack.into_first().subtree; + let subtree = stack.into_last().subtree; if let [tt::TokenTree::Subtree(first)] = &*subtree.token_trees { first.clone() } else { diff --git a/crates/stdx/src/non_empty_vec.rs b/crates/stdx/src/non_empty_vec.rs index 199ee18b94..342194c783 100644 --- a/crates/stdx/src/non_empty_vec.rs +++ b/crates/stdx/src/non_empty_vec.rs @@ -1,45 +1,39 @@ -//! A [`Vec`] that is guaranteed to at least contain one element. +//! See [`NonEmptyVec`]. -pub struct NonEmptyVec(Vec); +/// A [`Vec`] that is guaranteed to at least contain one element. +pub struct NonEmptyVec { + first: T, + rest: Vec, +} impl NonEmptyVec { #[inline] - pub fn new(initial: T) -> Self { - NonEmptyVec(vec![initial]) + pub fn new(first: T) -> Self { + NonEmptyVec { first, rest: Vec::new() } } #[inline] pub fn last_mut(&mut self) -> &mut T { - match self.0.last_mut() { - Some(it) => it, - None => unreachable!(), - } + self.rest.last_mut().unwrap_or(&mut self.first) } #[inline] pub fn pop(&mut self) -> Option { - if self.0.len() <= 1 { - None - } else { - self.0.pop() - } + self.rest.pop() } #[inline] pub fn push(&mut self, value: T) { - self.0.push(value) + self.rest.push(value) } #[inline] pub fn len(&self) -> usize { - self.0.len() + 1 + self.rest.len() } #[inline] - pub fn into_first(mut self) -> T { - match self.0.pop() { - Some(it) => it, - None => unreachable!(), - } + pub fn into_last(mut self) -> T { + self.rest.pop().unwrap_or(self.first) } }