sharkdp/bat

`Controller::run_with_error_handler` does not allow the handler closure to modify variables outside

rhysd opened this issue · 0 comments

I'm using bat::controller::Controller and I'd like to handle error(s) while printing outputs in my application side. I tried to capture the last error with FnMut closure:

use bat::controller::Controller;

let mut controller = Controller::new(...);

// ...

let inputs = ...;

let mut last_error: Option<MyError> = None;
controller.run_with_error_handler(inputs, |err, _| {
   last_error = Some(err.into()); 
})?;

But the handler type is bounded by Fn so it could not modify the variable outside:

error[E0594]: cannot assign to `last_error`, as it is a captured variable in a `Fn` closure
   --> src/bat.rs:169:13
    |
110 |     pub fn print(&self, file: File) -> Result<()> {
    |            -----                       ---------- change this to return `FnMut` instead of `Fn`
...
168 |         if controller.run_with_error_handler(vec![input], None, |err, _| {
    |                                                                 -------- in this closure
169 |             last_error = Some(err);
    |             ^^^^^^^^^^^^^^^^^^^^^^ cannot assign

I'd like to request to modify the Fn bound to FnMut to make error handling more flexible.