I'm pretty certain this is the exact use-case for specialization, but can any super-smart person find a way to do this without specialization?
I'm trying to find a way to automatically implement Component for any Rust type without boilerplate, but still allow explicitly implementing Component manually. Related to the bevy stable TypeId issue.
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::any::TypeId;
struct Component1;
struct Component0;
impl Component for Component0 {
fn id() -> u64 {
0
}
}
trait Component {
fn id() -> u64;
}
impl<T: 'static> Component for T {
fn id() -> u64 {
let mut hasher = DefaultHasher::new();
let id = TypeId::of::<T>();
id.hash(&mut hasher);
hasher.finish()
}
}
fn main() {
dbg!(Component1::id());
dbg!(Component0::id());
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `Component` for type `Component0`:
--> src/main.rs:18:1
|
8 | impl Component for Component0 {
| ----------------------------- first implementation here
...
18 | impl<T: 'static> Component for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Component0`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0119`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.