Debian Codegraph @

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

raw
1 use super::*;
2 use core::ffi::c_void;
3 use core::ptr::null_mut;
4
5 /// A WinRT object that may be used as a polymorphic stand-in for any WinRT class, interface, or boxed value.
6 /// [`IInspectable`] represents the
7 /// [IInspectable](https://docs.microsoft.com/en-us/windows/win32/api/inspectable/nn-inspectable-iinspectable)
8 /// interface.
9 #[repr(transparent)]
10 #[derive(Clone, PartialEq, Eq, Debug)]
11 pub struct IInspectable(pub IUnknown);
12
13 interface_hierarchy!(IInspectable, IUnknown);
14
15 impl IInspectable {
16 /// Returns the canonical type name for the underlying object.
17 #[cfg(windows)]
18 pub fn GetRuntimeClassName(&self) -> Result<HSTRING> {
19 unsafe {
20 let mut abi = null_mut();
21 (self.vtable().GetRuntimeClassName)(core::mem::transmute_copy(self), &mut abi).ok()?;
22 Ok(core::mem::transmute::<*mut c_void, HSTRING>(abi))
23 }
24 }
25
26 /// Gets the trust level of the current object.
27 pub fn GetTrustLevel(&self) -> Result<i32> {
28 unsafe {
29 let mut value = 0;
30 (self.vtable().GetTrustLevel)(core::mem::transmute_copy(self), &mut value).ok()?;
31 Ok(value)
32 }
33 }
34 }
35
36 #[doc(hidden)]
37 #[repr(C)]
38 pub struct IInspectable_Vtbl {
39 pub base: IUnknown_Vtbl,
40 pub GetIids: unsafe extern "system" fn(
41 this: *mut c_void,
42 count: *mut u32,
43 values: *mut *mut GUID,
44 ) -> HRESULT,
45 pub GetRuntimeClassName:
46 unsafe extern "system" fn(this: *mut c_void, value: *mut *mut c_void) -> HRESULT,
47 pub GetTrustLevel: unsafe extern "system" fn(this: *mut c_void, value: *mut i32) -> HRESULT,
48 }
49
50 unsafe impl Interface for IInspectable {
51 type Vtable = IInspectable_Vtbl;
52 const IID: GUID = GUID::from_u128(0xaf86e2e0_b12d_4c6a_9c5a_d7aa65101e90);
53 }
54
55 impl RuntimeType for IInspectable {
56 const SIGNATURE: imp::ConstBuffer = imp::ConstBuffer::from_slice(b"cinterface(IInspectable)");
57 }
58
59 impl RuntimeName for IInspectable {}
60
61 impl IInspectable_Vtbl {
62 pub const fn new<Identity: IUnknownImpl, Name: RuntimeName, const OFFSET: isize>() -> Self {
63 unsafe extern "system" fn GetIids(
64 _: *mut c_void,
65 count: *mut u32,
66 values: *mut *mut GUID,
67 ) -> HRESULT {
68 if count.is_null() || values.is_null() {
69 return imp::E_POINTER;
70 }
71 // Note: even if we end up implementing this in future, it still doesn't need a this pointer
72 // since the data to be returned is type- not instance-specific so can be shared for all
73 // interfaces.
74 *count = 0;
75 *values = null_mut();
76 HRESULT(0)
77 }
78 unsafe extern "system" fn GetRuntimeClassName<T: RuntimeName>(
79 _: *mut c_void,
80 value: *mut *mut c_void,
81 ) -> HRESULT {
82 if value.is_null() {
83 return imp::E_POINTER;
84 }
85
86 #[cfg(windows)]
87 {
88 *value = core::mem::transmute::<HSTRING, *mut c_void>(T::NAME.into());
89 }
90
91 #[cfg(not(windows))]
92 {
93 *value = core::ptr::null_mut();
94 }
95
96 HRESULT(0)
97 }
98 unsafe extern "system" fn GetTrustLevel<T: IUnknownImpl, const OFFSET: isize>(
99 this: *mut c_void,
100 value: *mut i32,
101 ) -> HRESULT {
102 if value.is_null() {
103 return imp::E_POINTER;
104 }
105 let this = (this as *mut *mut c_void).offset(OFFSET) as *mut T;
106 (*this).GetTrustLevel(value)
107 }
108 Self {
109 base: IUnknown_Vtbl::new::<Identity, OFFSET>(),
110 GetIids,
111 GetRuntimeClassName: GetRuntimeClassName::<Name>,
112 GetTrustLevel: GetTrustLevel::<Identity, OFFSET>,
113 }
114 }
115 }