luaur_analysis/records/
not_null.rs1use core::ops::{Deref, DerefMut};
4use core::ptr::NonNull;
5
6#[repr(transparent)]
7#[derive(Debug)]
8pub struct NotNull<T: ?Sized> {
9 ptr: NonNull<T>,
10}
11
12impl<T: ?Sized> Copy for NotNull<T> {}
13
14impl<T: ?Sized> Clone for NotNull<T> {
15 fn clone(&self) -> Self {
16 *self
17 }
18}
19
20impl<T: ?Sized> NotNull<T> {
21 pub fn new(ptr: *mut T) -> Self {
22 Self {
23 ptr: NonNull::new(ptr).expect("NotNull constructed from null pointer"),
24 }
25 }
26
27 pub fn from_ref(value: &mut T) -> Self {
28 Self::new(value as *mut T)
29 }
30
31 pub fn get(self) -> *mut T {
32 self.ptr.as_ptr()
33 }
34}
35
36impl<T: ?Sized> Deref for NotNull<T> {
37 type Target = T;
38
39 fn deref(&self) -> &Self::Target {
40 unsafe { self.ptr.as_ref() }
41 }
42}
43
44impl<T: ?Sized> DerefMut for NotNull<T> {
45 fn deref_mut(&mut self) -> &mut Self::Target {
46 unsafe { self.ptr.as_mut() }
47 }
48}
49
50impl<T: ?Sized, U: ?Sized> PartialEq<NotNull<U>> for NotNull<T> {
51 fn eq(&self, other: &NotNull<U>) -> bool {
52 self.ptr.as_ptr().cast::<()>() == other.ptr.as_ptr().cast::<()>()
53 }
54}
55
56impl<T: ?Sized> Eq for NotNull<T> {}
57
58impl<T: ?Sized> core::hash::Hash for NotNull<T> {
59 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
60 self.ptr.as_ptr().cast::<()>().hash(state);
61 }
62}