Debian Codegraph @

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

raw
1 use super::*;
2 use core::ffi::c_void;
3 use core::marker::PhantomData;
4 use core::mem::transmute;
5
6 /// A borrowed type with the same memory layout as the type itself that can be used to construct ABI-compatible function signatures.
7 #[repr(transparent)]
8 pub struct Ref<'a, T: Type<T>>(T::Abi, PhantomData<&'a T>);
9
10 impl<'a, T: Type<T, Default = Option<T>, Abi = *mut c_void>> Ref<'a, T> {
11 /// Converts the argument to a [Result<&T>] reference.
12 pub fn ok(&self) -> Result<&T> {
13 if self.0.is_null() {
14 Err(Error::from_hresult(imp::E_POINTER))
15 } else {
16 unsafe { Ok(transmute::<&*mut c_void, &T>(&self.0)) }
17 }
18 }
19 }
20
21 impl<'a, T: Type<T>> core::ops::Deref for Ref<'a, T> {
22 type Target = T::Default;
23 fn deref(&self) -> &Self::Target {
24 unsafe { transmute(&self.0) }
25 }
26 }