windows_ext/minwindef/ext.rs
1//! This module provides extension traits that expose the freestanding functions
2//! from [minwindef][crate::minwindef] directly on the respective types.
3//!
4//! These traits can also be used with [new-types](https://doc.rust-lang.org/rust-by-example/generics/new_types.html).
5
6/// This trait provides the freestanding functions from [minwindef][crate::minwindef]
7/// directly on WORDs ([u16]).
8///
9/// When using [new-types](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) that wrap a WORD,
10/// you can implement this trait by implementing the [value][WordExt::value] method.
11///
12/// This trait is included in the convenience wrapper [`windows_ext::ext`][crate::ext].
13///
14/// ```
15/// use windows_ext::ext::WordExt; // or: windows_ext::minwindef::ext::DWordExt;
16///
17/// assert_eq!(0x12_34u16.hibyte(), 0x12);
18/// assert_eq!(0x12_34u16.lobyte(), 0x34);
19/// ```
20pub trait WordExt: Copy {
21 /// Provide the value of the WORD
22 fn value(self) -> u16;
23
24 /// Get the low order word as [u16]
25 ///
26 /// ```
27 /// # use windows_ext::ext::WordExt;
28 /// assert_eq!(0x12_34u16.lobyte(), 0x34);
29 /// ```
30 #[inline(always)]
31 fn lobyte(self) -> u8 {
32 super::lobyte(self.value())
33 }
34
35 /// Get the high order word as [u16]
36 ///
37 /// ```
38 /// # use windows_ext::ext::WordExt;
39 /// assert_eq!(0x12_34u16.hibyte(), 0x12);
40 /// ```
41 #[inline(always)]
42 fn hibyte(self) -> u8 {
43 super::hibyte(self.value())
44 }
45
46 /// Split a single dword into both of its words (lo, hi)
47 ///
48 /// ```
49 /// # use windows_ext::ext::WordExt;
50 /// assert_eq!(0x12_34u16.split(), (0x34, 0x12));
51 /// ```
52 #[inline(always)]
53 fn split(self) -> (u8, u8) {
54 super::splitword(self.value())
55 }
56}
57
58impl WordExt for u16 {
59 #[inline(always)]
60 fn value(self) -> u16 {
61 self as u16
62 }
63}
64
65/// This trait provides the freestanding functions from [minwindef][crate::minwindef]
66/// directly on DWORDs ([u32]).
67///
68/// When using [new-types](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) that wrap a DWORD,
69/// you can implement this trait by implementing the [value][DWordExt::value] method.
70///
71/// This trait is included in the convenience wrapper [`windows_ext::ext`][crate::ext].
72///
73/// ```
74/// use windows_ext::ext::DWordExt; // or: windows_ext::minwindef::ext::DWordExt;
75///
76/// assert_eq!(0x1234_5678u32.hiword(), 0x1234);
77/// assert_eq!(0x1234_5678u32.loword(), 0x5678);
78/// ```
79pub trait DWordExt: Copy {
80 /// Gets the value of the DWORD.
81 fn value(self) -> u32;
82
83 /// Get the low order word as [u16]
84 ///
85 /// ```
86 /// # use windows_ext::ext::DWordExt;
87 /// assert_eq!(0x1234_5678u32.loword(), 0x5678);
88 /// ```
89 #[inline(always)]
90 fn loword(self) -> u16 {
91 super::loword(self.value())
92 }
93
94 /// Get the high order word as [u16]
95 ///
96 /// ```
97 /// # use windows_ext::ext::DWordExt;
98 /// assert_eq!(0x1234_5678u32.hiword(), 0x1234);
99 /// ```
100 #[inline(always)]
101 fn hiword(self) -> u16 {
102 super::hiword(self.value())
103 }
104
105 /// Split a single dword into both of its words (lo, hi)
106 ///
107 /// ```
108 /// # use windows_ext::ext::DWordExt;
109 /// assert_eq!(0x1234_5678u32.split(), (0x5678, 0x1234));
110 /// ```
111 #[inline(always)]
112 fn split(self) -> (u16, u16) {
113 super::splitdword(self.value())
114 }
115}
116
117impl DWordExt for u32 {
118 #[inline(always)]
119 fn value(self) -> u32 {
120 self
121 }
122}
123
124impl DWordExt for usize {
125 /// DWord value of a usize. On 64-bit this truncates to the lower 32.
126 #[inline(always)]
127 fn value(self) -> u32 {
128 self as u32
129 }
130}
131
132/// This trait provides the freestanding functions from [minwindef][crate::minwindef]
133/// directly on QWORDs ([u64]).
134///
135/// When using [new-types](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) that wrap a QWORD,
136/// you can implement this trait by implementing the [value][QWordExt::value] method.
137///
138/// This trait is included in the convenience wrapper [`windows_ext::ext`][crate::ext].
139///
140/// ```
141/// use windows_ext::ext::QWordExt; // or: windows_ext::minwindef::ext::QWordExt;
142///
143/// assert_eq!(0x1234_5678_9abc_def0u64.hidword(), 0x1234_5678);
144/// assert_eq!(0x1234_5678_9abc_def0u64.lodword(), 0x9abc_def0);
145/// ```
146pub trait QWordExt: Copy {
147 /// Gets the value of the QWORD.
148 fn value(self) -> u64;
149
150 /// Get the low order double word as [u32]
151 ///
152 /// ```
153 /// # use windows_ext::ext::QWordExt;
154 /// assert_eq!(0x1234_5678_9abc_def0u64.lodword(), 0x9abc_def0);
155 /// ```
156 #[inline(always)]
157 fn lodword(self) -> u32 {
158 super::lodword(self.value())
159 }
160
161 /// Get the high order double word as [u32]
162 ///
163 /// ```
164 /// # use windows_ext::ext::QWordExt;
165 /// assert_eq!(0x1234_5678_9abc_def0u64.hidword(), 0x1234_5678);
166 /// ```
167 #[inline(always)]
168 fn hidword(self) -> u32 {
169 super::hidword(self.value())
170 }
171
172 /// Split a single quad word into both of its double words (lo, hi)
173 ///
174 /// ```
175 /// # use windows_ext::ext::QWordExt;
176 /// assert_eq!(0x1234_5678_9abc_def0u64.split(), (0x9abc_def0, 0x1234_5678));
177 /// ```
178 #[inline(always)]
179 fn split(self) -> (u32, u32) {
180 super::splitqword(self.value())
181 }
182}
183
184impl QWordExt for u64 {
185 fn value(self) -> u64 {
186 self
187 }
188}
189
190impl QWordExt for usize {
191 /// QWord for usize, on 32-bit this has normal padding semantics.
192 fn value(self) -> u64 {
193 self as u64
194 }
195}