rust-windowing / glutin

A low-level library for OpenGL context creation
Apache License 2.0
2k stars 477 forks source link

Return type of config picker for glutin_winit::DisplayBuilder::build #1685

Closed kozel-sd closed 4 months ago

kozel-sd commented 4 months ago

Is there a significant reason why config picker must return Config with no room for a failure? If I understand Rust correctly (I'm not very experienced in it), if no suitable config has been found, the only option now is to panic and eventually terminate at the point where build is called. What if someone wants to handle that as a recoverable error?

I see the following options:

  1. Let the picker return Option<Config> and return a specific error defined by glutin if the picker returns None
  2. Let the picker return Result<Config, Whatever> and propagate the error returned by it
  3. Add some other functionality like try_build
  4. Leave it as is :smiley: (it's OK if my imagination is too good and it actually isn't worth to complicate the interface just to handle such edge cases)

Some context: I'm developing an app for myself, and everything works. But unwrap where a failure is theoretically possible still looks a bit dirty. I've created this issue right now because I think it's better to improve the interface while it's unstable. If I'm not the first one with a suggestion like this (it looks obvious, I suppose) - well, I tried searching.

kchibisov commented 4 months ago

The only thing that makes sense to return is Option<Config> in case you just don't want anything you've asked for. If no suitable config was found it'll fail earlier, before even calling the picker.

So for me it sounds like your issue is theoretical and not practical.

kchibisov commented 4 months ago

I'd say that the current situation is ok though, because you're guaranteed to have a Config that matches your criteria.

kozel-sd commented 4 months ago

I've requested an impossible config with ConfigTemplateBuilder in my app and got "argument does not name a valid config". If this is how it's meant to work if there is no suitable config (even if requested configs are theoretically possible, not anything like "71 alpha bits"), I get the idea. In this case, we can close this issue.

At some point, I forgot why ConfigTemplateBuilder exists and thought about filtering configs in the picker...

kchibisov commented 4 months ago

When you ask for config there could be multiple that suits what you've asked for. When you don't specify what you want, the default config values are used, but they still could result in a lot of config matching the criteria.

When you ask something that doesn't exist at all, it'll error as it does for you now.

kozel-sd commented 4 months ago

Choosing the option #4 and closing