winresult_types/
wait.rs

1use bytemuck::*;
2use core::fmt::{self, Debug, Formatter};
3
4
5
6/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitformultipleobjectsex#return-value)\]
7/// WAIT_\* values returned by various WaitFor\* and other win32 functions.
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Zeroable)] #[repr(transparent)] pub struct WaitCode(pub(crate) u32);
9
10impl WaitCode {
11    #[doc(hidden)] pub const fn from_constant(value: u32) -> Self { Self(value) }
12
13    pub const fn to_u32(self) -> u32 { self.0 }
14
15    /// ### Returns
16    /// *   `Some(0)`   for `WAIT::OBJECT_0`
17    /// *   `Some(63)`  for `WAIT::OBJECT_0 + 63`
18    /// *   `None`      for `WAIT::OBJECT_0 + 64` (>= `MAXIMUM_WAIT_OBJECTS`)
19    pub fn to_object<T: From<u8>>(self) -> Option<T> {
20        if (0 .. 64).contains(&self.0) {
21            Some((self.0 as u8).into())
22        } else {
23            None
24        }
25    }
26
27    /// ### Returns
28    /// *   `Some(0)`   for `WAIT::ABANDONED_0`
29    /// *   `Some(63)`  for `WAIT::ABANDONED_0 + 63`
30    /// *   `None`      for `WAIT::ABANDONED_0 + 64` (>= `MAXIMUM_WAIT_OBJECTS`)
31    pub fn to_abandoned<T: From<u8>>(self) -> Option<T> {
32        if (0x80 .. 0xC0).contains(&self.0) {
33            Some(((self.0 - 0x80) as u8).into())
34        } else {
35            None
36        }
37    }
38
39    pub fn to_object_u32        (self) -> Option<u32  > { self.to_object() }
40    pub fn to_object_usize      (self) -> Option<usize> { self.to_object() }
41    pub fn to_abandoned_u32     (self) -> Option<u32  > { self.to_abandoned() }
42    pub fn to_abandoned_usize   (self) -> Option<usize> { self.to_abandoned() }
43}
44
45impl Debug for WaitCode {
46    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
47        if      let Some(n) = self.to_object_u32()      { write!(fmt, "WAIT::OBJECT_{n}") }
48        else if let Some(n) = self.to_abandoned_u32()   { write!(fmt, "WAIT::ABANDONED_{n}") }
49        else if self.0 == 0xC0                          { write!(fmt, "WAIT::IO_COMPLETION") }
50        else if self.0 == 0x102                         { write!(fmt, "WAIT::TIMEOUT") }
51        else if self.0 == 0xFFFFFFFF                    { write!(fmt, "WAIT::FAILED") }
52        else                                            { write!(fmt, "WaitCode(0x{:08X})", self.0) }
53    }
54}