use crate::common::{FARPROC, HOOKPROC, LPARAM};
use std::{
ffi::{c_char, CStr},
intrinsics::transmute,
};
use windows::core::PCWSTR;
pub trait FarProcExt {
fn to_hook_proc(self) -> HOOKPROC;
}
impl FarProcExt for FARPROC {
fn to_hook_proc(self) -> HOOKPROC {
unsafe { transmute(self) }
}
}
pub trait LParamExt {
fn to<T>(&self) -> &T;
}
impl LParamExt for LPARAM {
fn to<T>(&self) -> &T {
let ptr = self.0 as *const T;
unsafe { &*ptr }
}
}
pub trait ToBytesExt {
type Input;
fn to_bytes(&self) -> &[u8] {
unsafe {
let ptr = self as *const Self as *const u8;
std::slice::from_raw_parts(ptr, size_of::<Self::Input>())
}
}
}
pub trait StringExt {
fn to_string(self) -> String;
fn to_string_utf16(self) -> String;
}
impl StringExt for *const u8 {
fn to_string(self) -> String {
unsafe {
CStr::from_ptr(self as *const c_char)
.to_str()
.unwrap_or("")
.to_string()
}
}
fn to_string_utf16(self) -> String {
(self as *const u16).to_string_utf16()
}
}
impl StringExt for *const u16 {
fn to_string(self) -> String {
self.to_string_utf16()
}
fn to_string_utf16(self) -> String {
unsafe { PCWSTR(self).to_hstring().to_string_lossy() }
}
}
impl StringExt for &[u16] {
fn to_string(self) -> String {
self.to_string_utf16()
}
fn to_string_utf16(self) -> String {
let Some(p) = self.iter().position(|x| x == &0) else {
return String::new();
};
String::from_utf16_lossy(&self[..p])
}
}
pub trait VecExt<T> {
fn to_vec(self) -> Vec<T>;
}