Skip to content

Commit

Permalink
Implement borrowed display traits
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Jul 10, 2023
1 parent d85172b commit d8b1358
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::ops::Deref;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{error, fmt};

use raw_window_handle::{HandleError, HasRawDisplayHandle, RawDisplayHandle};
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasRawDisplayHandle, RawDisplayHandle,
};
#[cfg(not(wasm_platform))]
use std::time::{Duration, Instant};
#[cfg(wasm_platform)]
Expand Down Expand Up @@ -323,6 +325,14 @@ unsafe impl<T> HasRawDisplayHandle for EventLoop<T> {
}
}

impl<T> HasDisplayHandle for EventLoop<T> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
// SAFETY: The returned display handle is always valid for this lifetime.
self.raw_display_handle()
.map(|handle| unsafe { DisplayHandle::borrow_raw(handle) })
}
}

unsafe impl<T> raw_window_handle_05::HasRawDisplayHandle for EventLoop<T> {
fn raw_display_handle(&self) -> raw_window_handle_05::RawDisplayHandle {
let result = HasRawDisplayHandle::raw_display_handle(self);
Expand Down
17 changes: 16 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use std::fmt;

use raw_window_handle::{
HandleError, HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
DisplayHandle, HandleError, HasDisplayHandle, HasRawDisplayHandle, HasRawWindowHandle,
HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle,
};

use crate::{
Expand Down Expand Up @@ -1376,6 +1377,13 @@ unsafe impl HasRawWindowHandle for Window {
}
}

impl HasWindowHandle for Window {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
self.raw_window_handle()
.map(|window| unsafe { WindowHandle::borrow_raw(window) })
}
}

unsafe impl raw_window_handle_05::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle_05::RawWindowHandle {
let result = HasRawWindowHandle::raw_window_handle(self);
Expand All @@ -1397,6 +1405,13 @@ unsafe impl HasRawDisplayHandle for Window {
}
}

impl HasDisplayHandle for Window {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
self.raw_display_handle()
.map(|display| unsafe { DisplayHandle::borrow_raw(display) })
}
}

unsafe impl raw_window_handle_05::HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> raw_window_handle_05::RawDisplayHandle {
let result = HasRawDisplayHandle::raw_display_handle(self);
Expand Down

0 comments on commit d8b1358

Please sign in to comment.