-
Notifications
You must be signed in to change notification settings - Fork 921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error handling on ApplicationHandler
#3692
Comments
You could always just panic
Hmm, how exactly do you envision this would work on iOS? Once we've started running the application in |
You can forward the error yourself via the Having return |
@kchibisov what does this mean? what |
You're passing the See https://github.com/alacritty/alacritty/blob/master/alacritty/src/event.rs#L76 |
We can return |
@kchibisov ah, that's exactly what I'm already doing |
I think a better alternative is to change a slight variant to this idea is to name it differently, e.g. EDIT:after some thinking, I found a major problem with my idea: it infests the in theory, we can erase the status type ( due to this problem, now I don't think my suggestion is a good fit anymore. after all, this kind of ergonomic inconvenience is very subjective, maybe it can be better solved from the application side instead of the platform side, e.g., by automating (or "hiding") some of the error state management. the advantage for this approach is that it can be implemented as a library on top of winit, no need to change the winit core abstraction. below is a sketch for one possible implementation of such "adapter/wrapper" library: /// this is the trait application should implement instead of the winit one
/// it can use whatever protocol for return status propogation
/// for example, associated type as return type for event callbacks
trait ApplicationHandlerWithStatus<T: 'static = ()> {
type Status;
/// use `Result` may be convenient if the status is meant for error cases
/// because it allows you touse question mark operator
/// can also return other types, e.g. `Option<Self::Status>`
fn foo_bar_event(
&mut self,
event_loop: &ActiveEventLoop,
event: FooBarEvent<T>,
) -> Result<(), Self::Status> {
None
}
}
/// add extension api to `EventLoop`
impl<T> EventLoopExt for EventLoop<T> {
fn run_app_with_status(self, app: App) -> Result<App::Status, EventLoopError>
where
App: ApplicationHandlerWithStatus<T>,
{
let mut appx = AppWithStatus { app, status: None };
let result = self.run_app_on_demand(&mut app);
result.map(|()| {
appx.status
.take()
.expect("app didn't exit with status properly")
})
}
}
// -----------------------------------------------------------------------------
// glue logic
// -----------------------------------------------------------------------------
struct AppWithStatus<A, S> {
app: A,
status: Result<S, EventLoopError>,
}
impl<A, E, S> ApplicationHandler<E> for AppWithStatus<A, S>
where
E: 'static,
A: ApplicationHandlerWithStatus<E, Status = S>,
{
fn foo_bar_event(&mut self, event_loop: &ActiveEventLoop, event: FooBarEvent<E>) {
// call app callback, and store the returned error status
// and if it is `Err`, exit the event loop, the error will be extracted after
self.status = self.app.foo_bar_event_with_status(event_loop, event).err();
if self.status.is_some() {
event_loop.exit();
}
}
} |
Description
Introduction
Many of winit's functions return
Result
s, includingActiveEventLoop::create_window
. Despite this, allApplicationHandler
functions have no return type. It would be nice to be able to specify anError
type, and have the functions returnResult<(), Self::Error>
instead. If these functions were to fail,run_app
would forward this error, potentially with a mechanism for resuming the loop after handling the error.Current alternatives
The way I currently implement error handling is by having a
result
field on my handler, and setting it on every operation that returns aResult
; then, if this is anErr
, I exit the handler, and read the value outside of the handler. This is not very ergonomic to use.Relevant platforms
Windows, macOS, Wayland, X11, Web, iOS, Android
The text was updated successfully, but these errors were encountered: