So I have an existing method that accepts FnOnce(Foo)
, and I'd like to change it so that it also accepts FnOnce(Foo) -> Result<()>
. I can add a new Callback
enum param that can store either of those. E.g:
pub enum Callback {
FnOnce(Box<dyn FnOnce(&mut Foo)>),
FnOnceResult(Box<dyn FnOnce(&mut Foo) -> Result<()>>),
}
And I can change the method to accept T: Into<Callback>
, but I'm struggling to work out how to make create the into methods as this gives me a conflicting implementation error:
impl<T: FnOnce(&mut Foo) + 'static> From<T> for Callback {
fn from(f: T) -> Self {
Callback::FnOnce(Box::new(f))
}
}
impl<T: FnOnce(&mut Foo) -> Result<()> + 'static> From<T> for Callback {
fn from(f: T) -> Self {
Callback::FnOnceResult(Box::new(f))
}
}
Is there any way that would make this work easily.
Playground: Rust Playground
(The context for this is Ratatui's Terminal::draw method, which would be nice to accept a method that returns a result instead of having to panic. I can of course introduce a try_draw method, but i was curious about whether it's possible to make the existing method work for both method types)