blob: f68349e6c5344d2518714828a6dde20c91064189 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
}
}
|