avoid unwrap for infallible string formatting#12349
Open
xtqqczze wants to merge 2 commits intofish-shell:masterfrom
Open
avoid unwrap for infallible string formatting#12349xtqqczze wants to merge 2 commits intofish-shell:masterfrom
xtqqczze wants to merge 2 commits intofish-shell:masterfrom
Conversation
krobelus
reviewed
Jan 21, 2026
| pager_selected_description, | ||
| } | ||
|
|
||
| impl From<HighlightRole> for &'static str { |
| write!(output, "\nExecuted in {:6.2} {}", wall_time, wall_unit.long_name()).unwrap(); | ||
| write!(output, "\n usr time {:6.2} {}", usr_time, cpu_unit.long_name()).unwrap(); | ||
| write!(output, "\n sys time {:6.2} {}", sys_time, cpu_unit.long_name()).unwrap(); | ||
| write!(output, "\n_______________________________").ok(); |
Contributor
There was a problem hiding this comment.
this unwrap() we want to keep because writing to a buffer can't fail; we we want to assert that.
If we want' to get rid of the repeated unwrap(), we can use a trait.
We already do something similar when outputting to the terminal:
```rust
pub(crate) trait Output {
fn write_bytes(&mut self, buf: &[u8]);
}
macro_rules! write_to_output {
($out:expr, $($arg:tt)*) => {{
$crate::common::do_write_to_output($out, format_args!($($arg)*));
}};
}
```
I think it would be okay-ish to reuse Output here for now.
We already have a impl Output for Vec<u8> we could use here.
In future, we should probably extract a parent trait of Output
that doesn't have the terminal escape sequences
(and give it a better name InfallibleOutput?)
We might want to use the trait instead of the .unwrap() call added in 7fa9e9b (History: use Rust's buffered writing instead of our own, 2025-12-30), not sure yet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
String formatting is an infallible operation