datafusion_expr/
ptr_eq.rs1use std::fmt::Debug;
6use std::hash::{Hash, Hasher};
7use std::ops::Deref;
8use std::sync::Arc;
9
10pub fn arc_ptr_eq<T: ?Sized>(a: &Arc<T>, b: &Arc<T>) -> bool {
14 std::ptr::eq(Arc::as_ptr(a), Arc::as_ptr(b))
15}
16
17pub fn arc_ptr_hash<T: ?Sized>(a: &Arc<T>, hasher: &mut impl Hasher) {
21 std::ptr::hash(Arc::as_ptr(a), hasher)
22}
23
24#[derive(Clone)]
29#[expect(private_bounds)] pub struct PtrEq<Ptr: PointerType>(Ptr);
31
32impl<T> PartialEq for PtrEq<Arc<T>>
33where
34 T: ?Sized,
35{
36 fn eq(&self, other: &Self) -> bool {
37 arc_ptr_eq(&self.0, &other.0)
38 }
39}
40impl<T> Eq for PtrEq<Arc<T>> where T: ?Sized {}
41
42impl<T> Hash for PtrEq<Arc<T>>
43where
44 T: ?Sized,
45{
46 fn hash<H: Hasher>(&self, state: &mut H) {
47 arc_ptr_hash(&self.0, state);
48 }
49}
50
51impl<Ptr> From<Ptr> for PtrEq<Ptr>
52where
53 Ptr: PointerType,
54{
55 fn from(ptr: Ptr) -> Self {
56 PtrEq(ptr)
57 }
58}
59
60impl<T> From<PtrEq<Arc<T>>> for Arc<T>
61where
62 T: ?Sized,
63{
64 fn from(wrapper: PtrEq<Arc<T>>) -> Self {
65 wrapper.0
66 }
67}
68
69impl<Ptr> Debug for PtrEq<Ptr>
70where
71 Ptr: PointerType + Debug,
72{
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 self.0.fmt(f)
75 }
76}
77
78impl<Ptr> Deref for PtrEq<Ptr>
79where
80 Ptr: PointerType,
81{
82 type Target = Ptr;
83
84 fn deref(&self) -> &Self::Target {
85 &self.0
86 }
87}
88
89trait PointerType {}
90impl<T> PointerType for Arc<T> where T: ?Sized {}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95 use std::hash::DefaultHasher;
96
97 #[test]
98 pub fn test_ptr_eq_wrapper() {
99 let a = Arc::new("Hello".to_string());
100 let b = Arc::new(a.deref().clone());
101 let c = Arc::new("world".to_string());
102
103 let wrapper = PtrEq(Arc::clone(&a));
104 assert_eq!(wrapper, wrapper);
105
106 assert_eq!(PtrEq(Arc::clone(&a)), PtrEq(Arc::clone(&a)));
108 assert_eq!(hash(PtrEq(Arc::clone(&a))), hash(PtrEq(Arc::clone(&a))));
109
110 assert_ne!(PtrEq(Arc::clone(&a)), PtrEq(Arc::clone(&b)));
112
113 assert_ne!(PtrEq(Arc::clone(&a)), PtrEq(Arc::clone(&c)));
115 }
116
117 fn hash<T: Hash>(value: T) -> u64 {
118 let hasher = &mut DefaultHasher::new();
119 value.hash(hasher);
120 hasher.finish()
121 }
122}