win_wrap/
ext.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2024. The RigelA open source project team and
 * its contributors reserve all rights.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

use crate::common::{FARPROC, HOOKPROC, LPARAM};
use std::{
    ffi::{c_char, CStr},
    intrinsics::transmute,
};
use windows::core::PCWSTR;

//noinspection SpellCheckingInspection
/**
对FARPROC类型的扩展操作。
*/
pub trait FarProcExt {
    fn to_hook_proc(self) -> HOOKPROC;
}

impl FarProcExt for FARPROC {
    //noinspection SpellCheckingInspection
    /**
    转换到HOOKPROC类型。
    */
    fn to_hook_proc(self) -> HOOKPROC {
        unsafe { transmute(self) }
    }
}

//noinspection SpellCheckingInspection
/**
对LPARAM类型的扩展操作。
*/
pub trait LParamExt {
    /**
    转换到T的引用类型。
    */
    fn to<T>(&self) -> &T;
}

impl LParamExt for LPARAM {
    fn to<T>(&self) -> &T {
        let ptr = self.0 as *const T;
        unsafe { &*ptr }
    }
}

/**
把引用类型转换到bytes。
*/
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;

    /**
    转换到utf16字符串。
    */
    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])
    }
}

/// Vec的扩展操作。
pub trait VecExt<T> {
    fn to_vec(self) -> Vec<T>;
}