From 8cdfa445d6629ffef4cb84967ff7017654045bc2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 2 Jul 2025 18:36:06 -0600 Subject: chore: add vendor directory --- vendor/windows-core/src/ref.rs | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 vendor/windows-core/src/ref.rs (limited to 'vendor/windows-core/src/ref.rs') diff --git a/vendor/windows-core/src/ref.rs b/vendor/windows-core/src/ref.rs new file mode 100644 index 00000000..fa70852c --- /dev/null +++ b/vendor/windows-core/src/ref.rs @@ -0,0 +1,60 @@ +use super::*; +use core::mem::transmute; + +/// A borrowed type with the same memory layout as the type itself that can be used to construct ABI-compatible function signatures. +#[repr(transparent)] +pub struct Ref<'a, T: Type>(T::Abi, core::marker::PhantomData<&'a T>); + +impl> Ref<'_, T> { + /// Returns `true` if the argument is null. + pub fn is_null(&self) -> bool { + T::is_null(&self.0) + } + + /// Converts the argument to a [`Result<&T>`] reference. + pub fn ok(&self) -> Result<&T> { + self.as_ref() + .ok_or_else(|| Error::from_hresult(imp::E_POINTER)) + } + + /// Converts the argument to a [`Option<&T>`] reference. + pub fn as_ref(&self) -> Option<&T> { + if self.is_null() { + None + } else { + unsafe { Some(self.assume_init_ref()) } + } + } + + /// Converts the argument to a `&T` reference. + /// + /// # Panics + /// + /// Panics if the argument is null. + #[track_caller] + pub fn unwrap(&self) -> &T { + self.as_ref().expect("called `Ref::unwrap` on a null value") + } + + /// Converts the argument to an [`Option`] by cloning the reference. + pub fn cloned(&self) -> Option { + self.as_ref().cloned() + } + + unsafe fn assume_init_ref(&self) -> &T { + unsafe { T::assume_init_ref(&self.0) } + } +} + +impl> core::ops::Deref for Ref<'_, T> { + type Target = T::Default; + fn deref(&self) -> &Self::Target { + unsafe { transmute(&self.0) } + } +} + +impl<'a, T: Type> From<&'a T::Default> for Ref<'a, T> { + fn from(from: &'a T::Default) -> Self { + unsafe { core::mem::transmute_copy(from) } + } +} -- cgit v1.2.3