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

Fix panic with custom Debug impl returning an empty string #26

Closed
wants to merge 1 commit into from
Closed

Fix panic with custom Debug impl returning an empty string #26

wants to merge 1 commit into from

Conversation

nickolay
Copy link
Contributor

I tried to use the idea posted in #24 by @idubrov.

When running:

assert_eq!(PrettyString(""), PrettyString("foo"));

I got a panic 'attempt to subtract with overflow'. This PR fixes it.

The panic was 'attempt to subtract with overflow' because
the first difference is addition.

PrettyString is from idubrov:
#24 (comment)
Copy link
Collaborator

@colin-kiegel colin-kiegel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks alot for your contribution. 👍

I'll merge it manually with the minor adjustment I mentioned and I'll also handle the potential overflow in line 55. Or if you want to, you can fix it and I'll merge it via Github. ;-)

@@ -36,7 +36,7 @@ pub fn format_changeset(f: &mut fmt::Formatter, changeset: &Changeset) -> fmt::R
}
}
Difference::Add(ref added) => {
match diffs.get(i - 1) {
match diffs.get(i.wrapping_sub(1)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to rely on the last index to be None, but instead handle i=0 explicitly, e.g.

let prev = i.checked_sub(1).and_then(|x| diffs.get(x));
match prev {

@nickolay
Copy link
Contributor Author

Thanks for taking a look! Please feel free to incorporate the suggested adjustment yourself (I don't care about attribution in the commit history, if that's why you asked, and can't make the change easily).

@colin-kiegel
Copy link
Collaborator

merged manually :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants