avoid unwrap for infallible string formatting#12349
avoid unwrap for infallible string formatting#12349xtqqczze wants to merge 2 commits intofish-shell:masterfrom
Conversation
| 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(); |
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.
String formatting is an infallible operation