-
Notifications
You must be signed in to change notification settings - Fork 946
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
Make types generic over a Backend
trait
#908
Comments
This introduces a significant amount of complexity to the cross-platform API for benefits that are really only felt on one platform. Even then, I'm not convinced this provides much in the way of benefits on the platforms it affects - you'd need to do some serious type system contortions to use the statically-checked backend while maintaining cross-platform and cross-backend compatibility (I can elaborate on what those contortions would be if you'd like, but I don't feel that's necessary right now). If you want to separate types for the X11 and Wayland backends, you can do that without touching the public cross-platform API: pub trait WindowExtUnix {
fn get_window_ext(&self) -> WindowExtUnixEnum<'_>;
}
pub enum WindowExtUnixEnum<'a> {
Wayland(WindowExtWayland<'a>),
X11(WindowExtX11<'a>),
}
pub struct WindowExtWayland<'_> {
/* functions */
}
pub struct WindowExtX11<'_> {
/* functions */
} Why is a generics-based solution better than that?
Why is this a benefit? You could do that, but:
|
Because some platforms are capable of using multiple different backends, it may be worthwhile to create a
Backend
trait that's implemented on concrete types for each backend. The types would only be exposed on the platforms that support them.For example, Unix (excluding macOS) supports
X11
andWayland
.The following are the proposed types that would go in a
backend
module:X11
,Wayland
Windows
(orWin32
?)AppKit
UiKit
Android
Other platforms will inevitably have more than one backend in the future. For example:
UiKit
backend type can then be used on iOS or macOS.Implementation
The following presents a basic idea of how this would be implemented:
AnyBackend
The following type would be available for all supported platforms:
This is somewhat similar to how
platform_impl::linux::EventLoop
is implemented.There are 3 cases:
n
backends available. TheBackend::new
implementation would pick the preferred option available on the current system.Pros
Type safety. Functionality specific to Wayland vs X11 doesn't get mixed into the same type.
Enforces error handling.
std::convert::Infallible
can be used as the error type when operations will always succeed. This would eliminate any branches associated withResult::Err
.May allow for getting rid of
platform_impl
.Cons
You'd need to create a window via:
The user may not want to be concerned about about the actual backend being used.
Using
winit
becomes slightly more verbose.There's a lot of boilerplate when implementing
Backend
functionality on the concrete types. However, this can perhaps be mitigated with macros although I'm not certain.The text was updated successfully, but these errors were encountered: