Skip to main content

WindowHandle

Trait WindowHandle 

Source
pub trait WindowHandle:
    HasWindowHandle
    + HasDisplayHandle
    + Send
    + Sync { }
Expand description

A window/display handle pair, as one trait.

Presenters receive raw-window-handle types, not a concrete winit::window::Window: softbuffer, wgpu, and glutin all accept these handles directly, so any windowing library that produces them can drive the same presenter, and only this crate depends on winit itself.

raw-window-handle has no combined trait, and surface libraries need to own the handle (softbuffer stores it for the surface’s lifetime), so presenters receive Arc<dyn WindowHandle>: rwh implements the handle traits for Arc<H: ?Sized>, so the trait object passes straight into softbuffer::Surface::new / wgpu::Instance::create_surface.

Send + Sync is part of the trait rather than left to each implementation because a trait object erases auto traits its trait doesn’t name, and wgpu::Instance::create_surface requires them: its safe entry point takes ownership of a Send + Sync handle, and the alternative that doesn’t is unsafe. Declaring them here is what makes Arc<dyn WindowHandle> usable with it. Every windowing library that produces raw-window-handle types satisfies this already (winit::window::Window does on every platform).

§Examples

Blanket-implemented for any type implementing both raw-window-handle traits; there is nothing to implement directly on WindowHandle itself.

use raw_window_handle::{
    DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle,
    WindowHandle as RawWindowHandle,
};
use retroglyph_window::WindowHandle;

struct NoWindow;

impl HasWindowHandle for NoWindow {
    fn window_handle(&self) -> Result<RawWindowHandle<'_>, HandleError> {
        Err(HandleError::NotSupported)
    }
}

impl HasDisplayHandle for NoWindow {
    fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
        Err(HandleError::NotSupported)
    }
}

fn assert_is_window_handle<T: WindowHandle>(_handle: &T) {}
assert_is_window_handle(&NoWindow);

Implementors§

Source§

impl<T: HasWindowHandle + HasDisplayHandle + Send + Sync + ?Sized> WindowHandle for T