1

I'm trying to write an ECS system that can query components by value, reference, or mutable reference. For this, I want a different implementation for various tuple types as an Archetype.

trait Archetype { /* bla bla */ }
impl<C> Archetype for (C,) where C: Component {}
impl<C> Archetype for (&C,) where C: Component {} // Error, conflicts with previous impl
impl<C> Archetype for (&mut C,) where C: Component {} // Error conflicts with first impl

My guess is because rust is interpreting the "C" in the first impl to be any type that implements component, including "&C" and "&mut C". If I remove the first impl, the other two won't conflict. Is there a way to have that first impl and specify it's for owned types only?

2
  • 1
    For the more general case of impl for T conflicting with impl for &T, you probably need negative bounds, which is still only a proposal as of the time of writing. For the needs of ECS specifically, you may wish to take a look at how Bevy does things (specifically, Res and ResMut). Commented Dec 20, 2024 at 3:46
  • Thanks for the response! My goal was to have a pure function with normal parameters, instead of a function with a "Query<Archetype>" parameter. But in order to infer copy types, I would probably need a newtype. I'd really hope not to, but it may be necessary. impl for fn(C) and fn(&C) also conflict, but it's a warning not an error. Commented Dec 20, 2024 at 3:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.