Skip to content

Support generic parameters in derive(Query) - #483

Open
lyn1dev wants to merge 2 commits into
Ralith:masterfrom
lyn1dev:feat/derive-query-generics
Open

lyn1dev wants to merge 2 commits into
Ralith:masterfrom
lyn1dev:feat/derive-query-generics

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 25, 2026 •

Copy link
Copy Markdown

Closes #359.

#[derive(Query)] previously rejected any struct or enum with generic parameters beyond its single lifetime parameter (queries must have one generic parameter: a lifetime).

Summary

Allows #[derive(Query)] structs and enums to declare generic type and const parameters (including bounds, default arguments, and where clauses) alongside the required single query lifetime parameter:

  • Passes through only user-specified bounds and where clauses on impl Query, adding no implicit : ::hecs::Component bounds so derived queries can abstract over arbitrary Query types (including Or, With, and Without).
  • Parameterizes the generated Fetch and State structs/enums over per-field fetch/state type parameters (__F0, ... / __S0, ...) for fields whose types reference non-lifetime generic parameters, while keeping non-generic fields concrete inside Fetch/State (preserving private field types on pub queries without E0446 and avoiding Fetch: 'static bounds on user type parameters).
  • Emits manual Clone for the generated Fetch type and manual Copy/Clone for the generated State type instead of #[derive(Clone, Copy)], so generic queries work with component types that do not implement Clone or Copy, and avoids shadowing when the input query lifetime is named 'q.
  • Uses ::core::panic! in enum query generation so enum queries without empty variants compile under #![no_implicit_prelude].

Testing

  • cargo test -p hecs --all-features passes (46 unit tests, 11 trybuild tests, 71 integration tests, 36 doctests, including derived_query_abstracting_over_query with Or, With, and Without).
  • cargo test -p hecs --no-default-features and cargo check -p test-no-std-macros pass.
  • cargo fmt --all -- --check, cargo clippy --all-features --all-targets -- -D warnings, and RUSTDOCFLAGS="-Dwarnings" cargo doc --no-deps --all-features pass.

AI was used to write this code.

@Ralith

Ralith commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Adds : ::hecs::Component bounds to type parameters on the generated Query, Fetch, and State definitions and impls (matching #[derive(Bundle)]).

This precludes abstracting over another query type. For example, see the queries Or, With, and Without. What if we add no implicit bounds?

@lyn1dev

lyn1dev commented Nov 26, 2026

Copy link
Copy Markdown
Author

Updated in ec466d6 to remove all implicit : ::hecs::Component bounds and pass through only user-specified bounds:

  • PreparedGenerics::new no longer adds : ::hecs::Component to type parameters.
  • Instead of placing user type parameters on <Name>Fetch / <Name>State (which forces Q: 'static via Fetch: 'static), Fetch and State are parameterized over per-field __F0, ... / __S0, ... parameters for fields referencing non-lifetime generic parameters, while non-generic fields stay concrete inside Fetch/State (avoiding private-type leakage on pub structs in trybuild export.rs).
  • Updated existing generic derive(Query) tests with explicit : Component bounds and added tests abstracting over Query (including Or, With, and Without with non-'static borrows) in tests/tests.rs, tests/derive/generics.rs, and tests/derive/export.rs.

Verified locally: cargo fmt --all -- --check, cargo test -p hecs --all-features (46 unit, 11 trybuild, 71 integration, 36 doctests), cargo test -p hecs --no-default-features, cargo check -p test-no-std-macros, cargo clippy --all-features --all-targets -- -D warnings, and RUSTDOCFLAGS="-Dwarnings" cargo doc --no-deps --all-features all pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hecs::Query should handle type parameters

2 participants