Debian Codegraph @

third_party/rust/wpf-gpu-raster/src/nullable_ref.rs

raw
1 use std::{marker::PhantomData, ops::Deref};
2
3 pub struct Ref<'a, T> {
4 ptr: *const T,
5 _phantom: PhantomData<&'a T>
6 }
7
8 impl<'a, T> Copy for Ref<'a, T> { }
9
10 impl<'a, T> Clone for Ref<'a, T> {
11 fn clone(&self) -> Self {
12 *self
13 }
14 }
15
16 impl<'a, T> Ref<'a, T> {
17 pub fn new(p: &'a T) -> Self {
18 Ref { ptr: p as *const T, _phantom: PhantomData}
19 }
20 pub unsafe fn null() -> Self {
21 Ref { ptr: std::ptr::null(), _phantom: PhantomData}
22 }
23 pub fn is_null(&self) -> bool {
24 self.ptr.is_null()
25 }
26 pub fn get_ref(self) -> &'a T {
27 unsafe { &*self.ptr }
28 }
29 }
30
31 impl<'a, T> PartialEq for Ref<'a, T> {
32 fn eq(&self, other: &Self) -> bool {
33 self.ptr == other.ptr && self._phantom == other._phantom
34 }
35 }
36
37 impl<'a, T> PartialOrd for Ref<'a, T> {
38 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
39 match self.ptr.partial_cmp(&other.ptr) {
40 Some(core::cmp::Ordering::Equal) => {}
41 ord => return ord,
42 }
43 self._phantom.partial_cmp(&other._phantom)
44 }
45 }
46
47 impl<'a, T> Deref for Ref<'a, T> {
48 type Target = T;
49
50 fn deref(&self) -> &Self::Target {
51 unsafe { &*self.ptr }
52 }
53 }