/
bstr.rs
149 lines (139 loc) · 5.19 KB
/
bstr.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use super::*;
pub fn gen() -> TokenStream {
quote! {
#[repr(transparent)]
pub struct BSTR(*const u16);
impl BSTR {
pub fn new() -> Self {
Self(core::ptr::null_mut())
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
if self.0.is_null() {
0
} else {
unsafe { SysStringLen(self) as usize }
}
}
pub fn from_wide(value: &[u16]) -> Self {
if value.is_empty() {
return Self(::core::ptr::null_mut());
}
unsafe {
SysAllocStringLen(
PWSTR(value.as_ptr()),
value.len() as u32,
)
}
}
pub fn as_wide(&self) -> &[u16] {
if self.0.is_null() {
return &[];
}
unsafe { ::core::slice::from_raw_parts(self.0, self.len()) }
}
}
impl ::core::clone::Clone for BSTR {
fn clone(&self) -> Self {
Self::from_wide(self.as_wide())
}
}
impl ::core::convert::From<&str> for BSTR {
fn from(value: &str) -> Self {
let value: ::windows::core::alloc::vec::Vec<u16> = value.encode_utf16().collect();
Self::from_wide(&value)
}
}
impl ::core::convert::From<::windows::core::alloc::string::String> for BSTR {
fn from(value: ::windows::core::alloc::string::String) -> Self {
value.as_str().into()
}
}
impl ::core::convert::From<&::windows::core::alloc::string::String> for BSTR {
fn from(value: &::windows::core::alloc::string::String) -> Self {
value.as_str().into()
}
}
impl<'a> ::core::convert::TryFrom<&'a BSTR> for ::windows::core::alloc::string::String {
type Error = ::windows::core::alloc::string::FromUtf16Error;
fn try_from(value: &BSTR) -> ::core::result::Result<Self, Self::Error> {
::windows::core::alloc::string::String::from_utf16(value.as_wide())
}
}
impl ::core::convert::TryFrom<BSTR> for ::windows::core::alloc::string::String {
type Error = ::windows::core::alloc::string::FromUtf16Error;
fn try_from(value: BSTR) -> ::core::result::Result<Self, Self::Error> {
::windows::core::alloc::string::String::try_from(&value)
}
}
impl ::core::default::Default for BSTR {
fn default() -> Self {
Self(::core::ptr::null_mut())
}
}
impl ::core::fmt::Display for BSTR {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
use ::core::fmt::Write;
for c in ::core::char::decode_utf16(self.as_wide().iter().cloned()) {
f.write_char(c.map_err(|_| ::core::fmt::Error)?)?
}
Ok(())
}
}
impl ::core::fmt::Debug for BSTR {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::write!(f, "{}", self)
}
}
impl ::core::cmp::PartialEq for BSTR {
fn eq(&self, other: &Self) -> bool {
self.as_wide() == other.as_wide()
}
}
impl ::core::cmp::Eq for BSTR {}
impl ::core::cmp::PartialEq<::windows::core::alloc::string::String> for BSTR {
fn eq(&self, other: &::windows::core::alloc::string::String) -> bool {
self == other.as_str()
}
}
impl ::core::cmp::PartialEq<str> for BSTR {
fn eq(&self, other: &str) -> bool {
self == other
}
}
impl ::core::cmp::PartialEq<&str> for BSTR {
fn eq(&self, other: &&str) -> bool {
self.as_wide().iter().copied().eq(other.encode_utf16())
}
}
impl ::core::cmp::PartialEq<BSTR> for &str {
fn eq(&self, other: &BSTR) -> bool {
other == self
}
}
impl ::core::ops::Drop for BSTR {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { SysFreeString(self as &Self) }
}
}
}
unsafe impl ::windows::core::Abi for BSTR {
type Abi = ::core::mem::ManuallyDrop<Self>;
}
#[cfg(feature = "alloc")]
impl<'a> ::windows::core::IntoParam<'a, BSTR> for &str {
fn into_param(self) -> ::windows::core::Param<'a, BSTR> {
::windows::core::Param::Owned(self.into())
}
}
#[cfg(feature = "alloc")]
impl<'a> ::windows::core::IntoParam<'a, BSTR> for ::windows::core::alloc::string::String {
fn into_param(self) -> ::windows::core::Param<'a, BSTR> {
::windows::core::Param::Owned(self.into())
}
}
}
}