minor: replace panics with types

This commit is contained in:
Aleksey Kladov 2022-01-02 19:05:37 +03:00
parent 174c439c56
commit 3836b195dd
2 changed files with 15 additions and 21 deletions

View file

@ -280,7 +280,7 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
parent.subtree.token_trees.extend(entry.subtree.token_trees); 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 { if let [tt::TokenTree::Subtree(first)] = &*subtree.token_trees {
first.clone() first.clone()
} else { } else {

View file

@ -1,45 +1,39 @@
//! A [`Vec`] that is guaranteed to at least contain one element. //! See [`NonEmptyVec`].
pub struct NonEmptyVec<T>(Vec<T>); /// A [`Vec`] that is guaranteed to at least contain one element.
pub struct NonEmptyVec<T> {
first: T,
rest: Vec<T>,
}
impl<T> NonEmptyVec<T> { impl<T> NonEmptyVec<T> {
#[inline] #[inline]
pub fn new(initial: T) -> Self { pub fn new(first: T) -> Self {
NonEmptyVec(vec![initial]) NonEmptyVec { first, rest: Vec::new() }
} }
#[inline] #[inline]
pub fn last_mut(&mut self) -> &mut T { pub fn last_mut(&mut self) -> &mut T {
match self.0.last_mut() { self.rest.last_mut().unwrap_or(&mut self.first)
Some(it) => it,
None => unreachable!(),
}
} }
#[inline] #[inline]
pub fn pop(&mut self) -> Option<T> { pub fn pop(&mut self) -> Option<T> {
if self.0.len() <= 1 { self.rest.pop()
None
} else {
self.0.pop()
}
} }
#[inline] #[inline]
pub fn push(&mut self, value: T) { pub fn push(&mut self, value: T) {
self.0.push(value) self.rest.push(value)
} }
#[inline] #[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.len() 1 + self.rest.len()
} }
#[inline] #[inline]
pub fn into_first(mut self) -> T { pub fn into_last(mut self) -> T {
match self.0.pop() { self.rest.pop().unwrap_or(self.first)
Some(it) => it,
None => unreachable!(),
}
} }
} }