On a lot of platforms, the window handle is reference-counted:
- UIKit:
UIView is reference-counted.
- AppKit:
NSView is reference-counted. If you close or destroy the original window, the view can still be used (it just won't show anything).
- OpenHarmony OS NDK: Same as Android?
- WinRT:
CoreWindow, seems to be garbage collected / managed by the C# runtime.
- Web:
HtmlCanvasElement / OffscreenCanvas (JsValue is garbage-collected IIUC).
- Android NDK:
ANativeWindow can be acquired, and remains valid until it's released.
- Orbital: Windows are file descriptors IIUC.
On other platforms, the window is just an integer, and if the window is destroyed you'll either get an error, or attempt to do something with the wrong window:
- Xlib: Window =
XID, if you destroy the window, you might just get a BadWindow error in later calls.
- Xcb:
xcb_window_t, probably same as above (?)
- Wayland:
wl_surface, can be destroyed in safe code, probably also just causes protocol errors later on, but not unsoundness (?)
- Win32:
HWND, pretty sure this is "safe" to use after being destroyed, they're indexes into a thread-local table.
(Note that I'm (even more) unsure about the DRM, GBM and Haiku handles).
This difference means that it's impossible for us to provide a function that converts a WindowHandle<'_> to an OwnedWindowHandle (which would be possible if all handles used reference-counting), because we cannot prevent the handle from being destroyed, only the original creator of it can.
But maybe we don't need to? As long as we can create some Rusty wrapper around the WindowHandle<'_> while it is guaranteed to be alive (i.e. bump the reference count on the handles that need that), and that the wrapper can still be soundly used afterwards (even if it ends up erroring)? My desire is to remove the generics from Softbuffer, CC rust-windowing/softbuffer#298.
In any case, I think our current documentation around why the design is as it is this is clearly not good enough, because my mind keeps coming back to this issue.
On a lot of platforms, the window handle is reference-counted:
UIViewis reference-counted.NSViewis reference-counted. If you close or destroy the original window, the view can still be used (it just won't show anything).CoreWindow, seems to be garbage collected / managed by the C# runtime.HtmlCanvasElement/OffscreenCanvas(JsValueis garbage-collected IIUC).ANativeWindowcan be acquired, and remains valid until it's released.On other platforms, the window is just an integer, and if the window is destroyed you'll either get an error, or attempt to do something with the wrong window:
XID, if you destroy the window, you might just get aBadWindowerror in later calls.xcb_window_t, probably same as above (?)wl_surface, can be destroyed in safe code, probably also just causes protocol errors later on, but not unsoundness (?)HWND, pretty sure this is "safe" to use after being destroyed, they're indexes into a thread-local table.(Note that I'm (even more) unsure about the DRM, GBM and Haiku handles).
This difference means that it's impossible for us to provide a function that converts a
WindowHandle<'_>to anOwnedWindowHandle(which would be possible if all handles used reference-counting), because we cannot prevent the handle from being destroyed, only the original creator of it can.But maybe we don't need to? As long as we can create some Rusty wrapper around the
WindowHandle<'_>while it is guaranteed to be alive (i.e. bump the reference count on the handles that need that), and that the wrapper can still be soundly used afterwards (even if it ends up erroring)? My desire is to remove the generics from Softbuffer, CC rust-windowing/softbuffer#298.In any case, I think our current documentation around why the design is as it is this is clearly not good enough, because my mind keeps coming back to this issue.