1
2#![warn(missing_docs)]
3#![doc = include_str!("../README.md")]
4
5use std::ptr::{self, NonNull};
6use std::rc::{Rc, Weak as WeakRc};
7use std::sync::{Arc, Weak as WeakArc};
8
9pub trait Nullable {
13
14 const NULL: Self;
24
25 fn is_null(&self) -> bool;
31
32}
33
34pub const fn null<P: Nullable>() -> P {
38 P::NULL
39}
40
41pub fn is_null<P: Nullable>(ptr: &P) -> bool {
45 ptr.is_null()
46}
47
48impl<T> Nullable for *const T {
49
50 const NULL: Self = ptr::null();
51
52 fn is_null(&self) -> bool {
53 <*const T>::is_null(*self)
54 }
55
56}
57
58impl<T> Nullable for *mut T {
59
60 const NULL: Self = ptr::null_mut();
61
62 fn is_null(&self) -> bool {
63 <*mut T>::is_null(*self)
64 }
65
66}
67
68impl<T: NoneIsNull> Nullable for Option<T> {
69
70 const NULL: Self = None;
71
72 fn is_null(&self) -> bool {
73 self.is_none()
74 }
75
76}
77
78pub trait NoneIsNull: Sized {}
87
88impl<T: ?Sized> NoneIsNull for NonNull<T> {}
89
90impl<T: ?Sized> NoneIsNull for Box<T> {}
91
92impl<T: ?Sized> NoneIsNull for Rc<T> {}
93
94impl<T: ?Sized> NoneIsNull for WeakRc<T> {}
95
96impl<T: ?Sized> NoneIsNull for Arc<T> {}
97
98impl<T: ?Sized> NoneIsNull for WeakArc<T> {}
99
100impl<'a, T: ?Sized> NoneIsNull for &'a T {}
101
102impl<'a, T: ?Sized> NoneIsNull for &'a mut T {}