Skip to content

avoid unwrap for infallible string formatting#12349

Open
xtqqczze wants to merge 2 commits intofish-shell:masterfrom
xtqqczze:write_infallible
Open

avoid unwrap for infallible string formatting#12349
xtqqczze wants to merge 2 commits intofish-shell:masterfrom
xtqqczze:write_infallible

Conversation

@xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jan 18, 2026

@xtqqczze xtqqczze marked this pull request as ready for review January 18, 2026 18:10
@krobelus krobelus added this to the fish 4.4 milestone Jan 21, 2026
pager_selected_description,
}

impl From<HighlightRole> for &'static str {
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah From makes sense

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();
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

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