datafusion_expr_common/
dyn_eq.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::any::Any;
19use std::hash::{Hash, Hasher};
20
21/// A dyn-compatible version of [`Eq`] trait.
22/// The implementation constraints for this trait are the same as for [`Eq`]:
23/// the implementation must be reflexive, symmetric, and transitive.
24/// Additionally, if two values can be compared with [`DynEq`] and [`PartialEq`] then
25/// they must be [`DynEq`]-equal if and only if they are [`PartialEq`]-equal.
26/// It is therefore strongly discouraged to implement this trait for types
27/// that implement `PartialEq<Other>` or `Eq<Other>` for any type `Other` other than `Self`.
28///
29/// Note: This trait should not be implemented directly. Implement `Eq` and `Any` and use
30/// the blanket implementation.
31#[allow(private_bounds)]
32pub trait DynEq: private::EqSealed {
33    fn dyn_eq(&self, other: &dyn Any) -> bool;
34}
35
36impl<T: Eq + Any> private::EqSealed for T {}
37impl<T: Eq + Any> DynEq for T {
38    fn dyn_eq(&self, other: &dyn Any) -> bool {
39        other.downcast_ref::<Self>() == Some(self)
40    }
41}
42
43/// A dyn-compatible version of [`Hash`] trait.
44/// If two values are equal according to [`DynEq`], they must produce the same hash value.
45///
46/// Note: This trait should not be implemented directly. Implement `Hash` and `Any` and use
47/// the blanket implementation.
48#[allow(private_bounds)]
49pub trait DynHash: private::HashSealed {
50    fn dyn_hash(&self, _state: &mut dyn Hasher);
51}
52
53impl<T: Hash + Any> private::HashSealed for T {}
54impl<T: Hash + Any> DynHash for T {
55    fn dyn_hash(&self, mut state: &mut dyn Hasher) {
56        self.type_id().hash(&mut state);
57        self.hash(&mut state)
58    }
59}
60
61mod private {
62    pub(super) trait EqSealed {}
63    pub(super) trait HashSealed {}
64}