Skip to content

Commit ab03721

Browse files
authored
Make new windows-strings crate Windows-only (#3143)
1 parent f6c49f4 commit ab03721

File tree

10 files changed

+90
-98
lines changed

10 files changed

+90
-98
lines changed

crates/libs/core/src/inspectable.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22
use core::ffi::c_void;
3-
use core::mem::{transmute, transmute_copy};
43
use core::ptr::null_mut;
54

65
/// A WinRT object that may be used as a polymorphic stand-in for any WinRT class, interface, or boxed value.
@@ -15,19 +14,20 @@ interface_hierarchy!(IInspectable, IUnknown);
1514

1615
impl IInspectable {
1716
/// Returns the canonical type name for the underlying object.
17+
#[cfg(windows)]
1818
pub fn GetRuntimeClassName(&self) -> Result<HSTRING> {
1919
unsafe {
2020
let mut abi = null_mut();
21-
(self.vtable().GetRuntimeClassName)(transmute_copy(self), &mut abi).ok()?;
22-
Ok(transmute::<*mut c_void, HSTRING>(abi))
21+
(self.vtable().GetRuntimeClassName)(core::mem::transmute_copy(self), &mut abi).ok()?;
22+
Ok(core::mem::transmute::<*mut c_void, HSTRING>(abi))
2323
}
2424
}
2525

2626
/// Gets the trust level of the current object.
2727
pub fn GetTrustLevel(&self) -> Result<i32> {
2828
unsafe {
2929
let mut value = 0;
30-
(self.vtable().GetTrustLevel)(transmute_copy(self), &mut value).ok()?;
30+
(self.vtable().GetTrustLevel)(core::mem::transmute_copy(self), &mut value).ok()?;
3131
Ok(value)
3232
}
3333
}
@@ -82,8 +82,17 @@ impl IInspectable_Vtbl {
8282
if value.is_null() {
8383
return imp::E_POINTER;
8484
}
85-
let h: HSTRING = T::NAME.into(); // TODO: should be try_into
86-
*value = transmute::<HSTRING, *mut c_void>(h);
85+
86+
#[cfg(windows)]
87+
{
88+
*value = core::mem::transmute::<HSTRING, *mut c_void>(T::NAME.into());
89+
}
90+
91+
#[cfg(not(windows))]
92+
{
93+
*value = core::ptr::null_mut();
94+
}
95+
8796
HRESULT(0)
8897
}
8998
unsafe extern "system" fn GetTrustLevel<T: IUnknownImpl, const OFFSET: isize>(

crates/libs/core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ pub use weak::*;
5858
pub use windows_implement::implement;
5959
pub use windows_interface::interface;
6060
pub use windows_result::*;
61-
pub use windows_strings::*;

crates/libs/core/src/param.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,3 @@ where
6161
ParamValue::Owned(transmute_copy(&self))
6262
}
6363
}
64-
65-
impl Param<PCWSTR> for &BSTR {
66-
unsafe fn param(self) -> ParamValue<PCWSTR> {
67-
ParamValue::Owned(PCWSTR(self.as_ptr()))
68-
}
69-
}
70-
71-
impl Param<PCWSTR> for &HSTRING {
72-
unsafe fn param(self) -> ParamValue<PCWSTR> {
73-
ParamValue::Owned(PCWSTR(self.as_ptr()))
74-
}
75-
}
76-
77-
impl Param<PCWSTR> for PWSTR {
78-
unsafe fn param(self) -> ParamValue<PCWSTR> {
79-
ParamValue::Owned(PCWSTR(self.0))
80-
}
81-
}
82-
83-
impl Param<PCSTR> for PSTR {
84-
unsafe fn param(self) -> ParamValue<PCSTR> {
85-
ParamValue::Owned(PCSTR(self.0))
86-
}
87-
}

crates/libs/core/src/runtime_type.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,3 @@ primitives! {
2828
(f32, b"f4"),
2929
(f64, b"f8")
3030
}
31-
32-
impl RuntimeType for HSTRING {
33-
const SIGNATURE: imp::ConstBuffer = imp::ConstBuffer::from_slice(b"string");
34-
}

crates/libs/core/src/type.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,3 @@ primitives!(bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
9898

9999
#[doc(hidden)]
100100
pub type AbiType<T> = <T as Type<T>>::Abi;
101-
102-
impl TypeKind for PWSTR {
103-
type TypeKind = CopyType;
104-
}
105-
106-
impl TypeKind for PSTR {
107-
type TypeKind = CopyType;
108-
}
109-
110-
impl TypeKind for PCWSTR {
111-
type TypeKind = CopyType;
112-
}
113-
114-
impl TypeKind for PCSTR {
115-
type TypeKind = CopyType;
116-
}
117-
118-
impl TypeKind for HSTRING {
119-
type TypeKind = CloneType;
120-
}
121-
122-
impl TypeKind for BSTR {
123-
type TypeKind = CloneType;
124-
}

crates/libs/core/src/windows.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,62 @@ pub use handles::*;
1515
mod variant;
1616
pub use variant::*;
1717

18+
pub use windows_strings::*;
19+
1820
/// Attempts to load the factory object for the given WinRT class.
1921
/// This can be used to access COM interfaces implemented on a Windows Runtime class factory.
2022
pub fn factory<C: RuntimeName, I: Interface>() -> Result<I> {
2123
imp::factory::<C, I>()
2224
}
25+
26+
impl Param<PCWSTR> for &BSTR {
27+
unsafe fn param(self) -> ParamValue<PCWSTR> {
28+
ParamValue::Owned(PCWSTR(self.as_ptr()))
29+
}
30+
}
31+
32+
impl Param<PCWSTR> for &HSTRING {
33+
unsafe fn param(self) -> ParamValue<PCWSTR> {
34+
ParamValue::Owned(PCWSTR(self.as_ptr()))
35+
}
36+
}
37+
38+
impl Param<PCWSTR> for PWSTR {
39+
unsafe fn param(self) -> ParamValue<PCWSTR> {
40+
ParamValue::Owned(PCWSTR(self.0))
41+
}
42+
}
43+
44+
impl Param<PCSTR> for PSTR {
45+
unsafe fn param(self) -> ParamValue<PCSTR> {
46+
ParamValue::Owned(PCSTR(self.0))
47+
}
48+
}
49+
50+
impl RuntimeType for HSTRING {
51+
const SIGNATURE: imp::ConstBuffer = imp::ConstBuffer::from_slice(b"string");
52+
}
53+
54+
impl TypeKind for PWSTR {
55+
type TypeKind = CopyType;
56+
}
57+
58+
impl TypeKind for PSTR {
59+
type TypeKind = CopyType;
60+
}
61+
62+
impl TypeKind for PCWSTR {
63+
type TypeKind = CopyType;
64+
}
65+
66+
impl TypeKind for PCSTR {
67+
type TypeKind = CopyType;
68+
}
69+
70+
impl TypeKind for HSTRING {
71+
type TypeKind = CloneType;
72+
}
73+
74+
impl TypeKind for BSTR {
75+
type TypeKind = CloneType;
76+
}

crates/libs/strings/src/hstring.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl HSTRING {
5454
}
5555

5656
/// Get the contents of this `HSTRING` as a OsString.
57-
#[cfg(all(feature = "std", windows))]
57+
#[cfg(feature = "std")]
5858
pub fn to_os_string(&self) -> std::ffi::OsString {
5959
std::os::windows::ffi::OsStringExt::from_wide(self.as_wide())
6060
}
@@ -154,14 +154,14 @@ impl From<&String> for HSTRING {
154154
}
155155
}
156156

157-
#[cfg(all(feature = "std", windows))]
157+
#[cfg(feature = "std")]
158158
impl From<&std::path::Path> for HSTRING {
159159
fn from(value: &std::path::Path) -> Self {
160160
value.as_os_str().into()
161161
}
162162
}
163163

164-
#[cfg(all(feature = "std", windows))]
164+
#[cfg(feature = "std")]
165165
impl From<&std::ffi::OsStr> for HSTRING {
166166
fn from(value: &std::ffi::OsStr) -> Self {
167167
unsafe {
@@ -174,14 +174,14 @@ impl From<&std::ffi::OsStr> for HSTRING {
174174
}
175175
}
176176

177-
#[cfg(all(feature = "std", windows))]
177+
#[cfg(feature = "std")]
178178
impl From<std::ffi::OsString> for HSTRING {
179179
fn from(value: std::ffi::OsString) -> Self {
180180
value.as_os_str().into()
181181
}
182182
}
183183

184-
#[cfg(all(feature = "std", windows))]
184+
#[cfg(feature = "std")]
185185
impl From<&std::ffi::OsString> for HSTRING {
186186
fn from(value: &std::ffi::OsString) -> Self {
187187
value.as_os_str().into()
@@ -286,28 +286,28 @@ impl PartialEq<&HSTRING> for String {
286286
}
287287
}
288288

289-
#[cfg(all(feature = "std", windows))]
289+
#[cfg(feature = "std")]
290290
impl PartialEq<std::ffi::OsString> for HSTRING {
291291
fn eq(&self, other: &std::ffi::OsString) -> bool {
292292
*self == **other
293293
}
294294
}
295295

296-
#[cfg(all(feature = "std", windows))]
296+
#[cfg(feature = "std")]
297297
impl PartialEq<std::ffi::OsString> for &HSTRING {
298298
fn eq(&self, other: &std::ffi::OsString) -> bool {
299299
**self == **other
300300
}
301301
}
302302

303-
#[cfg(all(feature = "std", windows))]
303+
#[cfg(feature = "std")]
304304
impl PartialEq<&std::ffi::OsString> for HSTRING {
305305
fn eq(&self, other: &&std::ffi::OsString) -> bool {
306306
*self == ***other
307307
}
308308
}
309309

310-
#[cfg(all(feature = "std", windows))]
310+
#[cfg(feature = "std")]
311311
impl PartialEq<std::ffi::OsStr> for HSTRING {
312312
fn eq(&self, other: &std::ffi::OsStr) -> bool {
313313
self.as_wide()
@@ -317,56 +317,56 @@ impl PartialEq<std::ffi::OsStr> for HSTRING {
317317
}
318318
}
319319

320-
#[cfg(all(feature = "std", windows))]
320+
#[cfg(feature = "std")]
321321
impl PartialEq<std::ffi::OsStr> for &HSTRING {
322322
fn eq(&self, other: &std::ffi::OsStr) -> bool {
323323
**self == *other
324324
}
325325
}
326326

327-
#[cfg(all(feature = "std", windows))]
327+
#[cfg(feature = "std")]
328328
impl PartialEq<&std::ffi::OsStr> for HSTRING {
329329
fn eq(&self, other: &&std::ffi::OsStr) -> bool {
330330
*self == **other
331331
}
332332
}
333333

334-
#[cfg(all(feature = "std", windows))]
334+
#[cfg(feature = "std")]
335335
impl PartialEq<HSTRING> for std::ffi::OsStr {
336336
fn eq(&self, other: &HSTRING) -> bool {
337337
*other == *self
338338
}
339339
}
340340

341-
#[cfg(all(feature = "std", windows))]
341+
#[cfg(feature = "std")]
342342
impl PartialEq<HSTRING> for &std::ffi::OsStr {
343343
fn eq(&self, other: &HSTRING) -> bool {
344344
*other == **self
345345
}
346346
}
347347

348-
#[cfg(all(feature = "std", windows))]
348+
#[cfg(feature = "std")]
349349
impl PartialEq<&HSTRING> for std::ffi::OsStr {
350350
fn eq(&self, other: &&HSTRING) -> bool {
351351
**other == *self
352352
}
353353
}
354354

355-
#[cfg(all(feature = "std", windows))]
355+
#[cfg(feature = "std")]
356356
impl PartialEq<HSTRING> for std::ffi::OsString {
357357
fn eq(&self, other: &HSTRING) -> bool {
358358
*other == **self
359359
}
360360
}
361361

362-
#[cfg(all(feature = "std", windows))]
362+
#[cfg(feature = "std")]
363363
impl PartialEq<HSTRING> for &std::ffi::OsString {
364364
fn eq(&self, other: &HSTRING) -> bool {
365365
*other == ***self
366366
}
367367
}
368368

369-
#[cfg(all(feature = "std", windows))]
369+
#[cfg(feature = "std")]
370370
impl PartialEq<&HSTRING> for std::ffi::OsString {
371371
fn eq(&self, other: &&HSTRING) -> bool {
372372
**other == **self
@@ -389,14 +389,14 @@ impl TryFrom<HSTRING> for String {
389389
}
390390
}
391391

392-
#[cfg(all(feature = "std", windows))]
392+
#[cfg(feature = "std")]
393393
impl<'a> From<&'a HSTRING> for std::ffi::OsString {
394394
fn from(hstring: &HSTRING) -> Self {
395395
hstring.to_os_string()
396396
}
397397
}
398398

399-
#[cfg(all(feature = "std", windows))]
399+
#[cfg(feature = "std")]
400400
impl From<HSTRING> for std::ffi::OsString {
401401
fn from(hstring: HSTRING) -> Self {
402402
Self::from(&hstring)

crates/libs/strings/src/hstring_header.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,9 @@ impl HStringHeader {
2323
// The space for the terminating null character is already accounted for inside of `HStringHeader`.
2424
let bytes = core::mem::size_of::<Self>() + 2 * len as usize;
2525

26-
#[cfg(windows)]
2726
let header =
2827
unsafe { bindings::HeapAlloc(bindings::GetProcessHeap(), 0, bytes) } as *mut Self;
2928

30-
#[cfg(not(windows))]
31-
let header = unsafe {
32-
extern "C" {
33-
fn malloc(bytes: usize) -> *mut core::ffi::c_void;
34-
}
35-
36-
malloc(bytes) as *mut Self
37-
};
38-
3929
if header.is_null() {
4030
return Err(Error::from_hresult(HRESULT(bindings::E_OUTOFMEMORY)));
4131
}
@@ -56,17 +46,7 @@ impl HStringHeader {
5646
return;
5747
}
5848

59-
#[cfg(windows)]
6049
bindings::HeapFree(bindings::GetProcessHeap(), 0, header as *mut _);
61-
62-
#[cfg(not(windows))]
63-
{
64-
extern "C" {
65-
fn free(ptr: *mut core::ffi::c_void);
66-
}
67-
68-
free(header as *mut _);
69-
}
7050
}
7151

7252
pub fn duplicate(&self) -> Result<*mut Self> {

crates/libs/strings/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
33
*/
44

5+
#![cfg(windows)]
56
#![allow(non_snake_case)]
67
#![cfg_attr(
78
windows_debugger_visualizer,

crates/libs/windows/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs
66
[Feature search](https://microsoft.github.io/windows-rs/features/#/0.57.0)
77
*/
88

9+
#![cfg(windows)]
910
#![doc(html_no_source)]
1011
#![allow(non_snake_case, clashing_extern_declarations, non_upper_case_globals, non_camel_case_types, missing_docs, clippy::all)]
1112
#![cfg_attr(not(feature = "docs"), doc(hidden))]

0 commit comments

Comments
 (0)