mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
minor: replace panics with types
This commit is contained in:
parent
174c439c56
commit
3836b195dd
2 changed files with 15 additions and 21 deletions
|
@ -280,7 +280,7 @@ fn convert_tokens<C: TokenConvertor>(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 {
|
||||
|
|
|
@ -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> {
|
||||
#[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<T> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue