Debian Codegraph @

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

raw
1 use super::*;
2 use core::mem::{take, transmute_copy, zeroed};
3
4 /// Provides automatic parameter conversion in cases where the Windows API expects implicit conversion support.
5 ///
6 /// This is a mutable version of [Param] meant to support out parameters.
7 /// There is no need to implement this trait. Blanket implementations are provided for all applicable Windows types.
8 pub trait OutParam<T: TypeKind, C = <T as TypeKind>::TypeKind>: Sized
9 where
10 T: Type<T>,
11 {
12 #[doc(hidden)]
13 unsafe fn borrow_mut(&self) -> OutRef<'_, T>;
14 }
15
16 impl<T> OutParam<T, CloneType> for &mut T
17 where
18 T: TypeKind<TypeKind = CloneType> + Clone + Default,
19 {
20 unsafe fn borrow_mut(&self) -> OutRef<'_, T> {
21 let this: &mut T = transmute_copy(self);
22 take(this);
23 transmute_copy(self)
24 }
25 }
26
27 impl<T> OutParam<T, CopyType> for &mut T
28 where
29 T: TypeKind<TypeKind = CopyType> + Clone + Default,
30 {
31 unsafe fn borrow_mut(&self) -> OutRef<'_, T> {
32 transmute_copy(self)
33 }
34 }
35
36 impl<T> OutParam<T, InterfaceType> for &mut Option<T>
37 where
38 T: TypeKind<TypeKind = InterfaceType> + Clone,
39 {
40 unsafe fn borrow_mut(&self) -> OutRef<'_, T> {
41 let this: &mut Option<T> = transmute_copy(self);
42 take(this);
43 transmute_copy(self)
44 }
45 }
46
47 impl<T> OutParam<T> for Option<&mut T>
48 where
49 T: Type<T>,
50 {
51 unsafe fn borrow_mut(&self) -> OutRef<'_, T> {
52 match self {
53 Some(this) => transmute_copy(this),
54 None => zeroed(),
55 }
56 }
57 }