Skip to content

Commit a782687

Browse files
authoredMar 5, 2021
Merge pull request #970 from osa1/helpful_asserts
Replace assertions in Puctuated with more helpful panic messages
2 parents 0bd6d57 + 8a75e45 commit a782687

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed
 

‎src/punctuated.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ impl<T, P> Punctuated<T, P> {
162162
/// Panics if the sequence does not already have a trailing punctuation when
163163
/// this method is called.
164164
pub fn push_value(&mut self, value: T) {
165-
assert!(self.empty_or_trailing());
165+
assert!(
166+
self.empty_or_trailing(),
167+
"Punctuated::push_value: Punctuated is not empty or \
168+
does not have a trailing punctuation"
169+
);
170+
166171
self.last = Some(Box::new(value));
167172
}
168173

@@ -174,7 +179,10 @@ impl<T, P> Punctuated<T, P> {
174179
///
175180
/// Panics if the sequence is empty or already has a trailing punctuation.
176181
pub fn push_punct(&mut self, punctuation: P) {
177-
assert!(self.last.is_some());
182+
assert!(
183+
self.last.is_some(),
184+
"Punctuated::push_punct: Punctuated doesn't have any items"
185+
);
178186
let last = self.last.take().unwrap();
179187
self.inner.push((*last, punctuation));
180188
}
@@ -228,7 +236,10 @@ impl<T, P> Punctuated<T, P> {
228236
where
229237
P: Default,
230238
{
231-
assert!(index <= self.len());
239+
assert!(
240+
index <= self.len(),
241+
"Punctuated::insert: index out of range"
242+
);
232243

233244
if index == self.len() {
234245
self.push(value);
@@ -454,7 +465,12 @@ impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> {
454465

455466
impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
456467
fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
457-
assert!(self.empty_or_trailing());
468+
assert!(
469+
self.empty_or_trailing(),
470+
"Punctuated::extend: Punctuated is not empty or \
471+
does not have a trailing punctuation"
472+
);
473+
458474
let mut nomore = false;
459475
for pair in i {
460476
if nomore {

0 commit comments

Comments
 (0)
Please sign in to comment.