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

Make the output order deterministic #87

Merged
merged 1 commit into from
Jun 13, 2022

Conversation

tavianator
Copy link
Contributor

Fixes #63.

Comment on lines +193 to +198
let mut mapping = filetypes
.mapping
.iter()
.map(|(filetype, category)| (filetype, theme.get_style(category)))
.map(|(filetype, style)| style.map(|style| (filetype, style)))
.collect::<Result<Vec<_>>>()?;
Copy link
Owner

Choose a reason for hiding this comment

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

Minor: Maybe add a type annotation instead of collect::<T>() (just like below)

Suggested change
let mut mapping = filetypes
.mapping
.iter()
.map(|(filetype, category)| (filetype, theme.get_style(category)))
.map(|(filetype, style)| style.map(|style| (filetype, style)))
.collect::<Result<Vec<_>>>()?;
let mut mapping: Result<Vec<_>> = filetypes
.mapping
.iter()
.map(|(filetype, category)| (filetype, theme.get_style(category)))
.map(|(filetype, style)| style.map(|style| (filetype, style)))
.collect()?;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be mapping: Vec<_> = ...collect()?;. But rustc's type inference doesn't actually handle that. I could do

let mut mapping: Vec<_> = ...collect::<Result<_>>()?;

but I figured I might as well put everything in one place

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you! If I understand correctly, the problem was that entries were previously just sorted by a single key? Now we sort by FileType in addition, which makes the sort order completely deterministic?

… and the rest is just a refactoring.

@tavianator
Copy link
Contributor Author

Yeah the functional change is just

-filetypes_list.sort_unstable_by_key(|entry| entry.len());
+filetypes_list.sort_unstable_by_key(|entry| (entry.len(), entry));

Everything else is just refactoring.

@sharkdp sharkdp merged commit 499f813 into sharkdp:master Jun 13, 2022
@tavianator tavianator deleted the determinism branch June 13, 2022 17:40
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.

Output is not deterministic
2 participants