Skip to content

Commit 3836b19

Browse files
committed
minor: replace panics with types
1 parent 174c439 commit 3836b19

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

crates/mbe/src/syntax_bridge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
280280
parent.subtree.token_trees.extend(entry.subtree.token_trees);
281281
}
282282

283-
let subtree = stack.into_first().subtree;
283+
let subtree = stack.into_last().subtree;
284284
if let [tt::TokenTree::Subtree(first)] = &*subtree.token_trees {
285285
first.clone()
286286
} else {

crates/stdx/src/non_empty_vec.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
//! A [`Vec`] that is guaranteed to at least contain one element.
1+
//! See [`NonEmptyVec`].
22
3-
pub struct NonEmptyVec<T>(Vec<T>);
3+
/// A [`Vec`] that is guaranteed to at least contain one element.
4+
pub struct NonEmptyVec<T> {
5+
first: T,
6+
rest: Vec<T>,
7+
}
48

59
impl<T> NonEmptyVec<T> {
610
#[inline]
7-
pub fn new(initial: T) -> Self {
8-
NonEmptyVec(vec![initial])
11+
pub fn new(first: T) -> Self {
12+
NonEmptyVec { first, rest: Vec::new() }
913
}
1014

1115
#[inline]
1216
pub fn last_mut(&mut self) -> &mut T {
13-
match self.0.last_mut() {
14-
Some(it) => it,
15-
None => unreachable!(),
16-
}
17+
self.rest.last_mut().unwrap_or(&mut self.first)
1718
}
1819

1920
#[inline]
2021
pub fn pop(&mut self) -> Option<T> {
21-
if self.0.len() <= 1 {
22-
None
23-
} else {
24-
self.0.pop()
25-
}
22+
self.rest.pop()
2623
}
2724

2825
#[inline]
2926
pub fn push(&mut self, value: T) {
30-
self.0.push(value)
27+
self.rest.push(value)
3128
}
3229

3330
#[inline]
3431
pub fn len(&self) -> usize {
35-
self.0.len()
32+
1 + self.rest.len()
3633
}
3734

3835
#[inline]
39-
pub fn into_first(mut self) -> T {
40-
match self.0.pop() {
41-
Some(it) => it,
42-
None => unreachable!(),
43-
}
36+
pub fn into_last(mut self) -> T {
37+
self.rest.pop().unwrap_or(self.first)
4438
}
4539
}

0 commit comments

Comments
 (0)