use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
pub mod builtins {
use std::{ffi::c_void, hash::Hash};
use windows::Win32::{
Foundation::{HMODULE, HWND},
UI::Accessibility::HWINEVENTHOOK,
};
use super::PlatformHandle;
pub type OsHandle = HWINEVENTHOOK;
impl PlatformHandle for OsHandle {
type Primitive = *mut c_void;
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let ptr = self.0 as *const c_void as usize;
ptr.hash(state);
}
}
pub type ModuleHandle = HMODULE;
impl PlatformHandle for ModuleHandle {
type Primitive = *mut c_void;
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let ptr = self.0 as *const c_void as usize;
ptr.hash(state);
}
}
pub type WindowHandle = HWND;
impl PlatformHandle for WindowHandle {
type Primitive = *mut c_void;
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let ptr = self.0 as *const c_void as usize;
ptr.hash(state);
}
}
}
pub type OsHandle = OpaqueHandle<builtins::OsHandle>;
pub type ModuleHandle = OpaqueHandle<builtins::ModuleHandle>;
pub type WindowHandle = OpaqueHandle<builtins::WindowHandle>;
pub trait PlatformHandle: Debug + Clone + PartialEq + Eq {
type Primitive: Debug + Clone + PartialEq + Eq;
fn hash<H: Hasher>(&self, state: &mut H);
}
pub trait Handle: Debug + Clone + PartialEq + Eq + Hash {
type PlatformHandle: PlatformHandle;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpaqueHandle<T: PlatformHandle>(T);
impl<T> Handle for OpaqueHandle<T>
where
T: PlatformHandle,
{
type PlatformHandle = T;
}
unsafe impl<T> Sync for OpaqueHandle<T> where T: PlatformHandle {}
unsafe impl<T> Send for OpaqueHandle<T> where T: PlatformHandle {}
impl<T> From<T> for OpaqueHandle<T>
where
T: PlatformHandle,
{
fn from(value: T) -> Self {
Self(value)
}
}
impl<T> Deref for OpaqueHandle<T>
where
T: PlatformHandle,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for OpaqueHandle<T>
where
T: PlatformHandle,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Hash for OpaqueHandle<T>
where
T: PlatformHandle,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl<T> Default for OpaqueHandle<T>
where
T: PlatformHandle + Default,
{
fn default() -> Self {
Self(T::default())
}
}