Skip to content

Commit e1ab47c

Browse files
authored
Simplify examples (#705)
1 parent 363667d commit e1ab47c

File tree

32 files changed

+63
-112
lines changed

32 files changed

+63
-112
lines changed

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn main() {
6565
We're not done! This generates the bindings, but it just puts them into the target folder of our crate where they'll go unused. We want to actually *use* this generated code not just generate and forget about it. In order for the generated code to be exported from the `bindings` crate, we need to include it in the crate itself. Change the lib.rs file of the bindings crate to the following:
6666

6767
```rust
68-
::windows::include_bindings!();
68+
windows::include_bindings!();
6969
```
7070

7171
This effectively copy/pastes the generated code into the `lib.rs` file. The `bindings` crate now exports all the bindings we've generated. Next, we need to make sure our `spellchecker` crate depends on the `bindings` crate. In the `spellchecker` crate's Cargo.toml file, add the `bindings` crate as a dependency. We'll also add the `windows` crate as a dependency since we'll be using that as well in our `spellchecker` crate.

examples/clock/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/clock/src/main.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use bindings::{
22
Windows::Foundation::Numerics::*, Windows::Win32::Direct2D::*, Windows::Win32::Direct3D11::*,
3-
Windows::Win32::Dxgi::*, Windows::Win32::Gdi::*, Windows::Win32::MenusAndResources::*,
4-
Windows::Win32::SystemServices::*, Windows::Win32::UIAnimation::*,
5-
Windows::Win32::WindowsAndMessaging::*, Windows::Win32::WindowsProgramming::*,
3+
Windows::Win32::Dxgi::*, Windows::Win32::Gdi::*, Windows::Win32::SystemServices::*,
4+
Windows::Win32::UIAnimation::*, Windows::Win32::WindowsAndMessaging::*,
5+
Windows::Win32::WindowsProgramming::*,
66
};
77

88
use windows::*;
@@ -131,11 +131,7 @@ impl Window {
131131
if error == DXGI_STATUS_OCCLUDED {
132132
unsafe {
133133
self.dxfactory
134-
.RegisterOcclusionStatusWindow(
135-
self.handle,
136-
WM_USER as u32,
137-
&mut self.occlusion,
138-
)
134+
.RegisterOcclusionStatusWindow(self.handle, WM_USER, &mut self.occlusion)
139135
.ok()?;
140136
}
141137
self.visible = false;
@@ -397,11 +393,11 @@ impl Window {
397393

398394
fn run(&mut self) -> Result<()> {
399395
unsafe {
400-
let instance = HINSTANCE(GetModuleHandleA(PSTR::NULL));
396+
let instance = HINSTANCE(GetModuleHandleA(None));
401397
debug_assert!(instance.0 != 0);
402398

403399
let wc = WNDCLASSA {
404-
hCursor: LoadCursorW(HINSTANCE(0), IDC_HAND),
400+
hCursor: LoadCursorW(None, IDC_HAND),
405401
hInstance: instance,
406402
lpszClassName: PSTR(b"window\0".as_ptr() as _),
407403

@@ -422,8 +418,8 @@ impl Window {
422418
CW_USEDEFAULT,
423419
CW_USEDEFAULT,
424420
CW_USEDEFAULT,
425-
HWND(0),
426-
HMENU(0),
421+
None,
422+
None,
427423
instance,
428424
self as *mut _ as _,
429425
);
@@ -436,26 +432,27 @@ impl Window {
436432
if self.visible {
437433
self.render()?;
438434

439-
// TODO: https://github.com/microsoft/win32metadata/issues/331
440435
while PeekMessageA(
441436
&mut message,
442-
HWND(0),
437+
None,
443438
0,
444439
0,
445440
PEEK_MESSAGE_REMOVE_TYPE::PM_REMOVE,
446441
)
447442
.into()
448443
{
449-
if message.message == WM_QUIT as u32 {
444+
if message.message == WM_QUIT {
450445
return Ok(());
451446
}
452447
DispatchMessageA(&message);
453448
}
454449
} else {
455-
GetMessageA(&mut message, HWND(0), 0, 0);
456-
if message.message == WM_QUIT as u32 {
450+
GetMessageA(&mut message, None, 0, 0);
451+
452+
if message.message == WM_QUIT {
457453
return Ok(());
458454
}
455+
459456
DispatchMessageA(&message);
460457
}
461458
}
@@ -469,7 +466,7 @@ impl Window {
469466
lparam: LPARAM,
470467
) -> LRESULT {
471468
unsafe {
472-
if message == WM_NCCREATE as u32 {
469+
if message == WM_NCCREATE {
473470
let cs = lparam.0 as *const CREATESTRUCTA;
474471
let this = (*cs).lpCreateParams as *mut Self;
475472
(*this).handle = window;
@@ -602,7 +599,7 @@ fn create_device_with_type(drive_type: D3D_DRIVER_TYPE) -> Result<ID3D11Device>
602599
flags,
603600
std::ptr::null(),
604601
0,
605-
D3D11_SDK_VERSION as u32,
602+
D3D11_SDK_VERSION,
606603
&mut device,
607604
std::ptr::null_mut(),
608605
&mut None,

examples/com_uri/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/event/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/event/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use bindings::{
22
Windows::Win32::SystemServices::{
3-
CreateEventW, SetEvent, WaitForSingleObject, PWSTR, WAIT_RETURN_CAUSE,
3+
CreateEventW, SetEvent, WaitForSingleObject, WAIT_RETURN_CAUSE,
44
},
55
Windows::Win32::WindowsProgramming::CloseHandle,
66
};
77

88
fn main() -> windows::Result<()> {
99
unsafe {
10-
let event = CreateEventW(std::ptr::null_mut(), true, false, PWSTR::NULL);
10+
let event = CreateEventW(std::ptr::null_mut(), true, false, None);
1111

1212
assert!(event.0 != 0);
1313

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/message_box/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use bindings::Windows::Win32::WindowsAndMessaging::{MessageBoxA, HWND, MESSAGEBOX_STYLE};
1+
use bindings::Windows::Win32::WindowsAndMessaging::{MessageBoxA, MESSAGEBOX_STYLE};
22

33
fn main() {
44
unsafe {
5-
MessageBoxA(HWND(0), "Hello", "World", MESSAGEBOX_STYLE::MB_OK);
5+
MessageBoxA(None, "Hello", "World", MESSAGEBOX_STYLE::MB_OK);
66
}
77
}

examples/ocr/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/overlapped/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() -> windows::Result<()> {
1515
std::ptr::null_mut(),
1616
FILE_CREATION_DISPOSITION::OPEN_EXISTING,
1717
FILE_FLAGS_AND_ATTRIBUTES::FILE_FLAG_OVERLAPPED,
18-
HANDLE::NULL,
18+
None,
1919
);
2020

2121
if file.is_invalid() {
@@ -29,14 +29,14 @@ fn main() -> windows::Result<()> {
2929
OffsetHigh: 0,
3030
},
3131
},
32-
hEvent: CreateEventA(std::ptr::null_mut(), true, false, PSTR::NULL),
32+
hEvent: CreateEventA(std::ptr::null_mut(), true, false, None),
3333
Internal: 0,
3434
InternalHigh: 0,
3535
};
3636

3737
assert!(overlapped.hEvent.0 != 0);
38-
3938
let mut buffer: [u8; 12] = Default::default();
39+
4040
let read_ok = ReadFile(
4141
file,
4242
buffer.as_mut_ptr() as _,

examples/rss/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/simple/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod bindings {
2-
::windows::include_bindings!();
2+
windows::include_bindings!();
33
}
44

55
fn main() -> windows::Result<()> {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/webview2/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/webview2/src/main.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use bindings::{
1414
Gdi,
1515
HiDpi::{self, PROCESS_DPI_AWARENESS},
1616
KeyboardAndMouseInput,
17-
MenusAndResources::HMENU,
1817
SystemServices::{self, HINSTANCE, LRESULT, PSTR},
1918
WindowsAndMessaging::{
20-
self, HWND, LPARAM, MSG, SET_WINDOW_POS_FLAGS, SHOW_WINDOW_CMD, WINDOW_EX_STYLE,
19+
self, HWND, LPARAM, MSG, SET_WINDOW_POS_FLAGS, SHOW_WINDOW_CMD,
2120
WINDOW_LONG_PTR_INDEX, WINDOW_STYLE, WNDCLASSA, WPARAM,
2221
},
2322
},
@@ -141,17 +140,17 @@ impl FrameWindow {
141140
WindowsAndMessaging::RegisterClassA(&window_class);
142141

143142
WindowsAndMessaging::CreateWindowExA(
144-
WINDOW_EX_STYLE(0),
143+
Default::default(),
145144
class_name,
146145
class_name,
147146
WINDOW_STYLE::WS_OVERLAPPEDWINDOW,
148147
WindowsAndMessaging::CW_USEDEFAULT,
149148
WindowsAndMessaging::CW_USEDEFAULT,
150-
640,
151-
480,
152-
HWND(0),
153-
HMENU(0),
154-
HINSTANCE(SystemServices::GetModuleHandleA(PSTR(0 as *mut _))),
149+
WindowsAndMessaging::CW_USEDEFAULT,
150+
WindowsAndMessaging::CW_USEDEFAULT,
151+
None,
152+
None,
153+
HINSTANCE(SystemServices::GetModuleHandleA(None)),
155154
0 as *mut _,
156155
)
157156
}
@@ -404,7 +403,7 @@ impl WebView {
404403
unsafe {
405404
WindowsAndMessaging::SetWindowPos(
406405
*frame.window,
407-
HWND(0),
406+
None,
408407
0,
409408
0,
410409
width,

examples/win2d/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

examples/xml/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Finally, make use of any Windows APIs as needed.
4343

4444
```rust
4545
mod bindings {
46-
::windows::include_bindings!();
46+
windows::include_bindings!();
4747
}
4848

4949
use bindings::{
5050
Windows::Data::Xml::Dom::*,
51-
Windows::Win32::SystemServices::{CreateEventW, SetEvent, WaitForSingleObject, PWSTR},
52-
Windows::Win32::WindowsAndMessaging::{MessageBoxA, HWND, MESSAGEBOX_STYLE},
51+
Windows::Win32::SystemServices::{CreateEventW, SetEvent, WaitForSingleObject},
52+
Windows::Win32::WindowsAndMessaging::{MessageBoxA, MESSAGEBOX_STYLE},
5353
Windows::Win32::WindowsProgramming::CloseHandle,
5454
};
5555

@@ -62,12 +62,12 @@ fn main() -> windows::Result<()> {
6262
assert!(root.InnerText()? == "hello world");
6363

6464
unsafe {
65-
let event = CreateEventW(std::ptr::null_mut(), true, false, PWSTR::NULL);
65+
let event = CreateEventW(std::ptr::null_mut(), true, false, None);
6666
SetEvent(event).ok()?;
6767
WaitForSingleObject(event, 0);
6868
CloseHandle(event).ok()?;
6969

70-
MessageBoxA(HWND(0), "Text", "Caption", MESSAGEBOX_STYLE::MB_OK);
70+
MessageBoxA(None, "Text", "Caption", MESSAGEBOX_STYLE::MB_OK);
7171
}
7272

7373
Ok(())

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Includes the generating bindings into the current context.
1+
/// Includes the generated bindings into the current context.
22
#[macro_export]
33
macro_rules! include_bindings {
44
() => {

src/runtime/param.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::*;
22

3-
// TODO: define IntoParam<T> trait rather than Into<Param<T>> so that other modules can define specializations
4-
// TODO: that way, types that must be specialized don't have to live in the Windows crate and have their own ElementType
5-
63
// A WinRT method parameter used to accept either a reference or value. `Param` is used by the
74
// generated bindings and should not generally be used directly.
85
#[doc(hidden)]
@@ -31,45 +28,3 @@ impl<'a, T: Abi> Drop for Param<'a, T> {
3128
T::drop_param(self);
3229
}
3330
}
34-
35-
// impl<'a, T: Abi> From<T> for Param<'a, T> {
36-
// fn from(value: T) -> Self {
37-
// Param::Owned(value)
38-
// }
39-
// }
40-
41-
// impl<'a, T: Abi> From<&'a T> for Param<'a, T> {
42-
// fn from(value: &'a T) -> Self {
43-
// Param::Borrowed(value)
44-
// }
45-
// }
46-
47-
// impl<'a, T: Interface> From<Option<T>> for Param<'a, T> {
48-
// fn from(value: Option<T>) -> Self {
49-
// match value {
50-
// Some(value) => Param::Owned(value),
51-
// None => Param::None,
52-
// }
53-
// }
54-
// }
55-
56-
// impl<'a, T: Interface> From<&'a Option<T>> for Param<'a, T> {
57-
// fn from(value: &'a Option<T>) -> Self {
58-
// match value {
59-
// Some(value) => Param::Borrowed(value),
60-
// None => Param::None,
61-
// }
62-
// }
63-
// }
64-
65-
// impl<'a> From<&'a str> for Param<'a, HString> {
66-
// fn from(value: &'a str) -> Self {
67-
// Param::Owned(value.into())
68-
// }
69-
// }
70-
71-
// impl<'a> From<String> for Param<'a, HString> {
72-
// fn from(value: String) -> Self {
73-
// Param::Owned(value.into())
74-
// }
75-
// }

src/traits/into_param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'a, T: Abi> IntoParam<'a, T> for &'a T {
1616
}
1717
}
1818

19-
impl<'a, T: Interface> IntoParam<'a, T> for Option<T> {
19+
impl<'a, T: Abi> IntoParam<'a, T> for Option<T> {
2020
fn into_param(self) -> Param<'a, T> {
2121
match self {
2222
Some(value) => Param::Owned(value),
@@ -25,7 +25,7 @@ impl<'a, T: Interface> IntoParam<'a, T> for Option<T> {
2525
}
2626
}
2727

28-
impl<'a, T: Interface> IntoParam<'a, T> for &'a Option<T> {
28+
impl<'a, T: Abi> IntoParam<'a, T> for &'a Option<T> {
2929
fn into_param(self) -> Param<'a, T> {
3030
match self {
3131
Some(value) => Param::Borrowed(value),

tests/convertible/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/deprecated/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/handles/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/unions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/wildcard/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/win32/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/win32/tests/win32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ fn onecore_imports() -> windows::Result<()> {
272272
assert!(port == 80);
273273

274274
let result = MiniDumpWriteDump(
275-
HANDLE(0),
275+
None,
276276
0,
277-
HANDLE(0),
277+
None,
278278
MINIDUMP_TYPE::MiniDumpNormal,
279279
std::ptr::null_mut(),
280280
std::ptr::null_mut(),

tests/win32_arrays/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

tests/winrt/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
::windows::include_bindings!();
1+
windows::include_bindings!();

0 commit comments

Comments
 (0)