Skip to main content

datafusion_expr/
ptr_eq.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::fmt::Debug;
6use std::hash::{Hash, Hasher};
7use std::ops::Deref;
8use std::sync::Arc;
9
10/// Compares two `Arc` pointers for equality based on their underlying pointers values.
11/// This is not equivalent to [`Arc::ptr_eq`] for fat pointers, see that method
12/// for more information.
13pub 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
17/// Hashes an `Arc` pointer based on its underlying pointer value.
18/// The general contract for this function is that if [`arc_ptr_eq`] returns `true`
19/// for two `Arc`s, then this function should return the same hash value for both.
20pub 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/// A wrapper around a pointer that implements `Eq` and `Hash` comparing
25/// the underlying pointer address.
26///
27/// If you have pointers to a `dyn UDF impl` consider using [`super::udf_eq::UdfEq`].
28#[derive(Clone)]
29#[expect(private_bounds)] // This is so that PtrEq can only be used with allowed pointer types (e.g. Arc), without allowing misuse.
30pub 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        // same address (equal)
107        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        // different address, same content (not equal)
111        assert_ne!(PtrEq(Arc::clone(&a)), PtrEq(Arc::clone(&b)));
112
113        // different address, different content (not equal)
114        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}