mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
internal: Use basic NonEmptyVec in mbe::syntax_bridge
This commit is contained in:
parent
a0e0e4575b
commit
65a1538dd1
3 changed files with 98 additions and 51 deletions
45
crates/stdx/src/non_empty_vec.rs
Normal file
45
crates/stdx/src/non_empty_vec.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
//! A [`Vec`] that is guaranteed to at least contain one element.
|
||||
|
||||
pub struct NonEmptyVec<T>(Vec<T>);
|
||||
|
||||
impl<T> NonEmptyVec<T> {
|
||||
#[inline]
|
||||
pub fn new(initial: T) -> Self {
|
||||
NonEmptyVec(vec![initial])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn last_mut(&mut self) -> &mut T {
|
||||
match self.0.last_mut() {
|
||||
Some(it) => it,
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
if self.0.len() <= 1 {
|
||||
None
|
||||
} else {
|
||||
self.0.pop()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn push(&mut self, value: T) {
|
||||
self.0.push(value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_first(mut self) -> T {
|
||||
match self.0.pop() {
|
||||
Some(it) => it,
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue