Skip to content

fmt: remove crash! macro#5589

Merged
tertsdiepraam merged 13 commits intouutils:mainfrom
Arp-1:fmt-remove-crash-macro
Dec 15, 2023
Merged

fmt: remove crash! macro#5589
tertsdiepraam merged 13 commits intouutils:mainfrom
Arp-1:fmt-remove-crash-macro

Conversation

@Arp-1
Copy link
Copy Markdown
Contributor

@Arp-1 Arp-1 commented Nov 27, 2023

Related to issue: #5487
This removes crash! usage from fmt.

@uutils uutils deleted a comment from github-actions bot Nov 28, 2023
@uutils uutils deleted a comment from github-actions bot Nov 28, 2023
None => {
return Err(USimpleError::new(
1,
"Failed to find a k-p linebreak solution. This should never happen.",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this text is right actually, this can, as far as I can tell, never happen. This is because the min_by_key call only returns None if the slice is empty, but the active slice is initialized with at least one value. Maybe an unreachable! call is more appropriate here. Although, I would also love to see the code refactored so that this case is eliminated. For example, we could also choose to just unwrap_or(0) on this min_by_key call, because active is initialized with 0. I'll leave that decision up to you :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This makes sense. Will mark the arm unreachable! as unwrap_or can give the impression that the default value can possibly be picked.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refactored to remove it completely.

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/inotify-dir-recreate. tests/tail/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@Arp-1
Copy link
Copy Markdown
Contributor Author

Arp-1 commented Nov 30, 2023

@tertsdiepraam I've made the requested changes. I see another related PR on this issue, please let me know if these changes are now redundant. Also, on another note, I see that some unrelated tests are failing, ....is this supposed to happen?

@tertsdiepraam
Copy link
Copy Markdown
Collaborator

Ah I missed that there were two PRs. I don't think we should discard these changes necessarily. The other PR simply changes crash! to unreachable! which is correct, but it would be even better if we can get rid of that case altogether. We can always merge that PR and merge this later as well. Also, yeah, sometimes we have unrelated tests that fail 😕 It's an ongoing effort to fix those

@Arp-1
Copy link
Copy Markdown
Contributor Author

Arp-1 commented Nov 30, 2023

Thank you for your response @tertsdiepraam !
I've removed the usage of crash! fully in this PR by using flat_map and collect. I'm very new to rust and honestly don't have a good understanding of rust's memory model - but unless this new code discards and reallocates the vector data on heap, I don't see any other issues in the code immediately.

Copy link
Copy Markdown
Collaborator

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

I like it! Great work! I think this is the right way to go about it. I just think we should avoid the iter method to simplify a bit.

Comment on lines +368 to +369
.iter()
.flat_map(|&&(mut best_idx)| {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.iter()
.flat_map(|&&(mut best_idx)| {
.map(|&(mut best_idx)| {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

}
}
})
.collect()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.collect()
.unwrap_or_default()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/inotify-dir-recreate is no longer failing!

Copy link
Copy Markdown
Collaborator

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

Great! Many thanks!

@tertsdiepraam tertsdiepraam merged commit 3a7a3bf into uutils:main Dec 15, 2023
@Arp-1 Arp-1 deleted the fmt-remove-crash-macro branch December 15, 2023 15:37
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.

2 participants