The Azul GUI Framework would really need this for ergonomics, for two reasons: one, to convert from None to Update::DoNothing automatically on a failed downcast and two, to convert to FFI types (it uses a repr(C) for every OptionFoo in the C ABI).
Here is a callback from a file manager (current API):
struct CopyRef {
app: RefAny, // handle to the whole app's state
drive_id: String, // which drive to copy
}
// Opens the folder picker and returns immediately with DoNothing
// to not block UI thread. Window stays responsive while the
// dialog is up
extern "C"
fn button_clicked(mut data: RefAny, _: CallbackInfo) -> Update {
let Some(copy) = data.downcast_ref::<CopyRef>() else {
return Update::DoNothing;
};
let _ = FileDialog::open_directory(
"Copy everything to…".into(), // title
OptionString::None, // default folder
RefAny::new(CopyRef { // handed back to us later
app: copy.app.clone(),
drive_id: copy.drive_id.clone(),
}),
on_copy_folder, // called when the user has picked a file
);
Update::DoNothing // no UI change yet, only opens dialog and returns
}
// Later: user did something with the file dialog: framework calls back
// on_copy_folder with the CopyRef from the above and the result
// from the file picker
extern "C"
fn on_copy_folder(mut data: RefAny, mut info: CallbackInfo, result: RefAny) -> Update {
// our own context back
let Some(copy) = data.downcast_ref::<CopyRef>() else {
return Update::DoNothing;
};
let (mut app, drive_id) = (copy.app.clone(), copy.drive_id.clone());
drop(copy);
// the dialog's result (an FFI OptionFileOpenResult, so first into a core Option)
let Some(picked) = FileOpenResult::downcast(result).into_option() else {
return Update::DoNothing;
};
// the chosen folder (an FFI OptionFilePath): None means the user cancelled
let Some(path) = picked.path.into_option() else {
return Update::DoNothing;
};
let folder = PathBuf::from(path.inner.as_str());
// the app's state
let app_handle = app.clone();
let Some(mut state) = app.downcast_mut::<DriveState>() else {
return Update::DoNothing;
};
// now do the thing
copy_everything(&mut info, &app_handle, &mut *state, &drive_id, folder);
// UI should refresh now
Update::RefreshDom
}
With the try trait (and please also FromResidual), I can implement:
impl Try for OptionFoo so that OptionFoo with repr(C) has the same semantics as Option<Foo> and ? works on it without .into_option()
impl FromResidual<Option<Infallible>> for Update so that I can auto-convert from a failed downcast to an Update::DoNothing return enum with ?:
extern "C"
fn on_copy_folder(mut data: RefAny, mut info: CallbackInfo, result: RefAny) -> Update {
let copy = data.downcast_ref::<CopyRef>()?;
let (mut app, drive_id) = (copy.app.clone(), copy.drive_id.clone());
drop(copy);
let picked = FileOpenResult::downcast(result)?;
let folder = PathBuf::from(picked.path?.inner.as_str());
let app_handle = app.clone();
let mut state = app.downcast_mut::<DriveState>()?;
copy_everything(&mut info, &app_handle, &mut *state, &drive_id, folder);
Update::RefreshDom
}
It's just a lot more readable: both the OptionFileResult can convert directly into an Update without any ".into_option() else { return Update::DoNothing }" and the failed downcast Option<T> can also convert directly.
I cannot use Option<T> in an "extern C" callback because Rusts native Option doesn't have a stable ABI layout. Yes, the file picker should offload the actual copying to a separate thread, etc. etc. - but this is just an example, this is simply how you'd do "async" continuation-style callbacks with a C FFI, which is where the Try trait is very very useful at saving lines of code.