-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
256 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Utilities for using `Any` as the key of a `HashMap`. | ||
// Adopted from: | ||
// https://github.com/bevyengine/bevy/blob/56bcbb097552b45e3ff48c48947ed8ee4e2c24b1/crates/bevy_utils/src/label.rs | ||
|
||
use std::any::Any; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
/// An object safe version of [`Eq`]. This trait is automatically implemented | ||
/// for any `'static` type that implements `Eq`. | ||
pub(crate) trait DynEq: Any { | ||
/// Casts the type to `dyn Any`. | ||
fn as_any(&self) -> &dyn Any; | ||
|
||
/// This method tests for `self` and `other` values to be equal. | ||
/// | ||
/// Implementers should avoid returning `true` when the underlying types are | ||
/// not the same. | ||
fn dyn_eq(&self, other: &dyn DynEq) -> bool; | ||
} | ||
|
||
impl<T> DynEq for T | ||
where | ||
T: Any + Eq, | ||
{ | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn dyn_eq(&self, other: &dyn DynEq) -> bool { | ||
if let Some(other) = other.as_any().downcast_ref::<T>() { | ||
return self == other; | ||
} | ||
false | ||
} | ||
} | ||
|
||
/// An object safe version of [`Hash`]. This trait is automatically implemented | ||
/// for any `'static` type that implements `Hash`. | ||
pub(crate) trait DynHash: DynEq { | ||
/// Casts the type to `dyn Any`. | ||
fn as_dyn_eq(&self) -> &dyn DynEq; | ||
|
||
/// Feeds this value into the given [`Hasher`]. | ||
fn dyn_hash(&self, state: &mut dyn Hasher); | ||
} | ||
|
||
impl<T> DynHash for T | ||
where | ||
T: DynEq + Hash, | ||
{ | ||
fn as_dyn_eq(&self) -> &dyn DynEq { | ||
self | ||
} | ||
|
||
fn dyn_hash(&self, mut state: &mut dyn Hasher) { | ||
T::hash(self, &mut state); | ||
self.type_id().hash(&mut state); | ||
} | ||
} |
Oops, something went wrong.