diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/wpf-gpu-raster/src/nullable_ref.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/wpf-gpu-raster/src/nullable_ref.rs')
-rw-r--r-- | third_party/rust/wpf-gpu-raster/src/nullable_ref.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/third_party/rust/wpf-gpu-raster/src/nullable_ref.rs b/third_party/rust/wpf-gpu-raster/src/nullable_ref.rs new file mode 100644 index 0000000000..1e8389e5b7 --- /dev/null +++ b/third_party/rust/wpf-gpu-raster/src/nullable_ref.rs @@ -0,0 +1,53 @@ +use std::{marker::PhantomData, ops::Deref}; + +pub struct Ref<'a, T> { + ptr: *const T, + _phantom: PhantomData<&'a T> +} + +impl<'a, T> Copy for Ref<'a, T> { } + +impl<'a, T> Clone for Ref<'a, T> { + fn clone(&self) -> Self { + *self + } +} + +impl<'a, T> Ref<'a, T> { + pub fn new(p: &'a T) -> Self { + Ref { ptr: p as *const T, _phantom: PhantomData} + } + pub unsafe fn null() -> Self { + Ref { ptr: std::ptr::null(), _phantom: PhantomData} + } + pub fn is_null(&self) -> bool { + self.ptr.is_null() + } + pub fn get_ref(self) -> &'a T { + unsafe { &*self.ptr } + } +} + +impl<'a, T> PartialEq for Ref<'a, T> { + fn eq(&self, other: &Self) -> bool { + self.ptr == other.ptr && self._phantom == other._phantom + } +} + +impl<'a, T> PartialOrd for Ref<'a, T> { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + match self.ptr.partial_cmp(&other.ptr) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + self._phantom.partial_cmp(&other._phantom) + } +} + +impl<'a, T> Deref for Ref<'a, T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + unsafe { &*self.ptr } + } +}
\ No newline at end of file |