Debian Codegraph @

third_party/rust/windows-core/src/param.rs

raw
1 use super::*;
2 use core::mem::transmute_copy;
3 use core::mem::zeroed;
4
5 /// Provides automatic parameter conversion in cases where the Windows API expects implicit conversion support.
6 ///
7 /// There is no need to implement this trait. Blanket implementations are provided for all applicable Windows types.
8 pub trait Param<T: TypeKind, C = <T as TypeKind>::TypeKind>: Sized
9 where
10 T: Type<T>,
11 {
12 #[doc(hidden)]
13 unsafe fn param(self) -> ParamValue<T>;
14 }
15
16 impl<T> Param<T> for Option<&T>
17 where
18 T: Type<T>,
19 {
20 unsafe fn param(self) -> ParamValue<T> {
21 ParamValue::Borrowed(match self {
22 Some(item) => transmute_copy(item),
23 None => zeroed(),
24 })
25 }
26 }
27
28 impl<T, U> Param<T, InterfaceType> for &U
29 where
30 T: TypeKind<TypeKind = InterfaceType> + Clone,
31 T: Interface,
32 U: Interface,
33 U: imp::CanInto<T>,
34 {
35 unsafe fn param(self) -> ParamValue<T> {
36 if U::QUERY {
37 self.cast()
38 .map_or(ParamValue::Borrowed(zeroed()), |ok| ParamValue::Owned(ok))
39 } else {
40 ParamValue::Borrowed(transmute_copy(self))
41 }
42 }
43 }
44
45 impl<T> Param<T, CloneType> for &T
46 where
47 T: TypeKind<TypeKind = CloneType> + Clone,
48 {
49 unsafe fn param(self) -> ParamValue<T> {
50 ParamValue::Borrowed(transmute_copy(self))
51 }
52 }
53
54 impl<T, U> Param<T, CopyType> for U
55 where
56 T: TypeKind<TypeKind = CopyType> + Clone,
57 U: TypeKind<TypeKind = CopyType> + Clone,
58 U: imp::CanInto<T>,
59 {
60 unsafe fn param(self) -> ParamValue<T> {
61 ParamValue::Owned(transmute_copy(&self))
62 }
63 }