summaryrefslogtreecommitdiffstats
path: root/third_party/rust/d3d12/src/com.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/d3d12/src/com.rs
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/d3d12/src/com.rs')
-rw-r--r--third_party/rust/d3d12/src/com.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/third_party/rust/d3d12/src/com.rs b/third_party/rust/d3d12/src/com.rs
new file mode 100644
index 0000000000..f68349e6c5
--- /dev/null
+++ b/third_party/rust/d3d12/src/com.rs
@@ -0,0 +1,102 @@
+use std::fmt;
+use std::hash::{Hash, Hasher};
+use std::ops::Deref;
+use std::ptr;
+use winapi::ctypes::c_void;
+use winapi::um::unknwnbase::IUnknown;
+use winapi::Interface;
+use D3DResult;
+
+#[repr(transparent)]
+pub struct WeakPtr<T>(*mut T);
+
+impl<T> WeakPtr<T> {
+ pub fn null() -> Self {
+ WeakPtr(ptr::null_mut())
+ }
+
+ pub unsafe fn from_raw(raw: *mut T) -> Self {
+ WeakPtr(raw)
+ }
+
+ pub fn is_null(&self) -> bool {
+ self.0.is_null()
+ }
+
+ pub fn as_ptr(&self) -> *const T {
+ self.0
+ }
+
+ pub fn as_mut_ptr(&self) -> *mut T {
+ self.0
+ }
+
+ pub unsafe fn mut_void(&mut self) -> *mut *mut c_void {
+ &mut self.0 as *mut *mut _ as *mut *mut _
+ }
+}
+
+impl<T: Interface> WeakPtr<T> {
+ pub unsafe fn as_unknown(&self) -> &IUnknown {
+ debug_assert!(!self.is_null());
+ &*(self.0 as *mut IUnknown)
+ }
+
+ // Cast creates a new WeakPtr requiring explicit destroy call.
+ pub unsafe fn cast<U>(&self) -> D3DResult<WeakPtr<U>>
+ where
+ U: Interface,
+ {
+ let mut obj = WeakPtr::<U>::null();
+ let hr = self
+ .as_unknown()
+ .QueryInterface(&U::uuidof(), obj.mut_void());
+ (obj, hr)
+ }
+
+ // Destroying one instance of the WeakPtr will invalidate all
+ // copies and clones.
+ pub unsafe fn destroy(&self) {
+ self.as_unknown().Release();
+ }
+}
+
+impl<T> Clone for WeakPtr<T> {
+ fn clone(&self) -> Self {
+ WeakPtr(self.0)
+ }
+}
+
+impl<T> Copy for WeakPtr<T> {}
+
+impl<T> Deref for WeakPtr<T> {
+ type Target = T;
+ fn deref(&self) -> &T {
+ debug_assert!(!self.is_null());
+ unsafe { &*self.0 }
+ }
+}
+
+impl<T> fmt::Debug for WeakPtr<T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "WeakPtr( ptr: {:?} )", self.0)
+ }
+}
+
+impl<T> PartialEq<*mut T> for WeakPtr<T> {
+ fn eq(&self, other: &*mut T) -> bool {
+ self.0 == *other
+ }
+}
+
+impl<T> PartialEq for WeakPtr<T> {
+ fn eq(&self, other: &Self) -> bool {
+ self.0 == other.0
+ }
+}
+
+impl<T> Hash for WeakPtr<T> {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.0.hash(state);
+ }
+}