Skip to content

Commit

Permalink
Add compile-time dyn compatible checks for DynEq, DynHash (#17254)
Browse files Browse the repository at this point in the history
# Objective

- Shrink `bevy_utils` more.
- Refs #11478

## Solution

- Removes `assert_object_safe` from `bevy_utils` by using a compile time
check instead.

## Testing

- CI.

---

## Migration Guide

`assert_object_safe` is no longer exported by `bevy_utils`. Instead, you
can write a compile time check that your trait is "dyn compatible":

```rust
/// Assert MyTrait is dyn compatible
const _: Option<Box<dyn MyTrait>> = None;
```

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
  • Loading branch information
mgi388 and alice-i-cecile authored Jan 9, 2025
1 parent 3742e62 commit b20e23d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 48 deletions.
22 changes: 6 additions & 16 deletions crates/bevy_ecs/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub trait DynEq: Any {
fn dyn_eq(&self, other: &dyn DynEq) -> bool;
}

// Tests that this trait is dyn-compatible
const _: Option<Box<dyn DynEq>> = None;

impl<T> DynEq for T
where
T: Any + Eq,
Expand All @@ -48,6 +51,9 @@ pub trait DynHash: DynEq {
fn dyn_hash(&self, state: &mut dyn Hasher);
}

// Tests that this trait is dyn-compatible
const _: Option<Box<dyn DynHash>> = None;

impl<T> DynHash for T
where
T: DynEq + Hash,
Expand Down Expand Up @@ -201,19 +207,3 @@ macro_rules! define_label {
$crate::intern::Interner::new();
};
}

#[cfg(test)]
mod tests {
use super::{DynEq, DynHash};
use bevy_utils::assert_object_safe;

#[test]
fn dyn_eq_object_safe() {
assert_object_safe::<dyn DynEq>();
}

#[test]
fn dyn_hash_object_safe() {
assert_object_safe::<dyn DynHash>();
}
}
2 changes: 0 additions & 2 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub mod synccell;
pub mod syncunsafecell;

mod default;
mod object_safe;
pub use object_safe::assert_object_safe;
mod once;
#[cfg(feature = "std")]
mod parallel_queue;
Expand Down
30 changes: 0 additions & 30 deletions crates/bevy_utils/src/object_safe.rs

This file was deleted.

0 comments on commit b20e23d

Please sign in to comment.