win_wrap/
ext.rs

1/*
2 * Copyright (c) 2024. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14use crate::common::{FARPROC, HOOKPROC, LPARAM};
15use std::{
16    ffi::{c_char, CStr},
17    intrinsics::transmute,
18};
19use windows::core::PCWSTR;
20
21//noinspection SpellCheckingInspection
22/**
23对FARPROC类型的扩展操作。
24*/
25pub trait FarProcExt {
26    fn to_hook_proc(self) -> HOOKPROC;
27}
28
29impl FarProcExt for FARPROC {
30    //noinspection SpellCheckingInspection
31    /**
32    转换到HOOKPROC类型。
33    */
34    fn to_hook_proc(self) -> HOOKPROC {
35        unsafe { transmute(self) }
36    }
37}
38
39//noinspection SpellCheckingInspection
40/**
41对LPARAM类型的扩展操作。
42*/
43pub trait LParamExt {
44    /**
45    转换到T的引用类型。
46    */
47    fn to<T>(&self) -> &T;
48}
49
50impl LParamExt for LPARAM {
51    fn to<T>(&self) -> &T {
52        let ptr = self.0 as *const T;
53        unsafe { &*ptr }
54    }
55}
56
57/**
58把引用类型转换到bytes。
59*/
60pub trait ToBytesExt {
61    type Input;
62    fn to_bytes(&self) -> &[u8] {
63        unsafe {
64            let ptr = self as *const Self as *const u8;
65            std::slice::from_raw_parts(ptr, size_of::<Self::Input>())
66        }
67    }
68}
69
70/**
71对字符串的扩展操作。
72*/
73pub trait StringExt {
74    /**
75    转换到普通字符串。
76    */
77    fn to_string(self) -> String;
78
79    /**
80    转换到utf16字符串。
81    */
82    fn to_string_utf16(self) -> String;
83}
84
85impl StringExt for *const u8 {
86    fn to_string(self) -> String {
87        unsafe {
88            CStr::from_ptr(self as *const c_char)
89                .to_str()
90                .unwrap_or("")
91                .to_string()
92        }
93    }
94
95    fn to_string_utf16(self) -> String {
96        (self as *const u16).to_string_utf16()
97    }
98}
99
100impl StringExt for *const u16 {
101    fn to_string(self) -> String {
102        self.to_string_utf16()
103    }
104
105    fn to_string_utf16(self) -> String {
106        unsafe { PCWSTR(self).to_hstring().to_string_lossy() }
107    }
108}
109
110impl StringExt for &[u16] {
111    fn to_string(self) -> String {
112        self.to_string_utf16()
113    }
114
115    fn to_string_utf16(self) -> String {
116        let Some(p) = self.iter().position(|x| x == &0) else {
117            return String::new();
118        };
119        String::from_utf16_lossy(&self[..p])
120    }
121}
122
123/// Vec的扩展操作。
124pub trait VecExt<T> {
125    fn to_vec(self) -> Vec<T>;
126}