Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assertions in Puctuated with more helpful panic messages #970

Merged
merged 1 commit into from Mar 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/punctuated.rs
Expand Up @@ -162,7 +162,12 @@ impl<T, P> Punctuated<T, P> {
/// Panics if the sequence does not already have a trailing punctuation when
/// this method is called.
pub fn push_value(&mut self, value: T) {
assert!(self.empty_or_trailing());
assert!(
self.empty_or_trailing(),
"Punctuated::push_value: Punctuated is not empty or \
does not have a trailing punctuation"
);

self.last = Some(Box::new(value));
}

Expand All @@ -174,7 +179,10 @@ impl<T, P> Punctuated<T, P> {
///
/// Panics if the sequence is empty or already has a trailing punctuation.
pub fn push_punct(&mut self, punctuation: P) {
assert!(self.last.is_some());
assert!(
self.last.is_some(),
"Punctuated::push_punct: Punctuated doesn't have any items"
);
let last = self.last.take().unwrap();
self.inner.push((*last, punctuation));
}
Expand Down Expand Up @@ -228,7 +236,10 @@ impl<T, P> Punctuated<T, P> {
where
P: Default,
{
assert!(index <= self.len());
assert!(
index <= self.len(),
"Punctuated::insert: index out of range"
);

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

impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
assert!(self.empty_or_trailing());
assert!(
self.empty_or_trailing(),
"Punctuated::extend: Punctuated is not empty or \
does not have a trailing punctuation"
);

let mut nomore = false;
for pair in i {
if nomore {
Expand Down