bevyengine / bevyengine/bevy

DynamicTupleStruct does not mirror the hashability of its underlying type

Open
#19,304 2 comments 0 reactions 0 assignees View on GitHub
A-Reflection C-Bug
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## Bevy version

Bevy v0.16.0.

## What you did

Tried to reflect-deserialize a type containing `HashMap` where:
```rust
#[derive(Component, Debug, Clone, Deserialize, Eq, PartialEq, Hash, Reflect)]
#[component(immutable)]
pub struct TileId(Cow<'static, str>);
```

Specifically I have data stored in an asset and do:
- Parse asset into data representation (think `json::Value`).
- Use `TypedReflectDeserializer` to deserialize the value. <-- this is where it all goes wrong
- Use `T::from_reflect(value.as_partial_reflect())` to get the final type.

## What went wrong

Received this panic:
```
bevy_reflect-0.16.0/src/map.rs:259:30:
the dynamic type `bevy_reflect::DynamicTupleStruct` (representing `game_core::map::data::TileId`) does not support hashing
```

## Additional information

- Adding `#[reflect(Hash)]` did not change the error, but adding `#[reflect(Hash, Deserialize)]` *does* fix it.
- Switching to `TileId(String)` did not change the error.
- Removing `#[component(immutable)]` did not change the error.

The bug seems to be that when `TileId` is represented as `DynamicTupleStruct`, its hashability is lost unless you do `#[reflect(Hash, Deserialize)]`. At minimum we need a better error message to give people a solution.

Contributor guide

Open the contributing guide

Research direction

Start at bevy_reflect-0.16.0/src/map.rs around line 259 and trace how TypedReflectDeserializer produces a DynamicTupleStruct for the reflected TileId. Reproduce the HashMap case from the report and verify that the dynamic representation mirrors the underlying type's hashability or provides a clearer error explaining the required reflection support.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.