{"id":24966820,"url":"https://github.com/joseph-gio/bevy-trait-query","last_synced_at":"2025-10-25T01:42:11.748Z","repository":{"id":61326662,"uuid":"549877923","full_name":"joseph-gio/bevy-trait-query","owner":"joseph-gio","description":"adds trait queries to the bevy game engine","archived":false,"fork":false,"pushed_at":"2025-03-20T10:46:26.000Z","size":221,"stargazers_count":108,"open_issues_count":5,"forks_count":15,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-29T14:12:40.871Z","etag":null,"topics":["bevy-engine","bevy-plugin","game-development","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joseph-gio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-11T22:01:45.000Z","updated_at":"2025-03-22T21:36:38.000Z","dependencies_parsed_at":"2023-12-24T15:45:04.319Z","dependency_job_id":"34a96690-dd7b-4db6-b2fa-3288925a18a5","html_url":"https://github.com/joseph-gio/bevy-trait-query","commit_stats":{"total_commits":147,"total_committers":7,"mean_commits":21.0,"dds":0.0680272108843537,"last_synced_commit":"0c0e7dd646b4a77fa3496ef5e9686107d17fdd1e"},"previous_names":["joseph-gio/bevy-trait-query","jojojet/bevy-trait-query"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseph-gio%2Fbevy-trait-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseph-gio%2Fbevy-trait-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseph-gio%2Fbevy-trait-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseph-gio%2Fbevy-trait-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joseph-gio","download_url":"https://codeload.github.com/joseph-gio/bevy-trait-query/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353745,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bevy-engine","bevy-plugin","game-development","rust"],"created_at":"2025-02-03T12:02:46.140Z","updated_at":"2025-10-25T01:42:11.736Z","avatar_url":"https://github.com/joseph-gio.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/JoJoJet/bevy-trait-query#license)\n[![Crates.io](https://img.shields.io/crates/v/bevy-trait-query.svg)](https://crates.io/crates/bevy-trait-query)\n[![Downloads](https://img.shields.io/crates/d/bevy-trait-query.svg)](https://crates.io/crates/bevy-trait-query)\n[![Docs](https://docs.rs/bevy-trait-query/badge.svg)](https://docs.rs/bevy_trait_query/latest/bevy_trait_query/)\n[![CI](https://github.com/JoJoJet/bevy-trait-query/workflows/CI/badge.svg)](https://github.com/JoJoJet/bevy-trait-query/actions)\n\n# bevy-trait-query\n\nAn implementation of trait queries for the bevy game engine.\n\nBefore using this crate, you should be familiar with bevy: https://bevyengine.org/.\n\n| Bevy Version | [Crate Version](CHANGELOG.md) |\n|--------------|---------------|\n| Preview      | Main branch   |\n| 0.17         | 0.17          |\n| 0.16         | 0.16          |\n| 0.15         | 0.7           |\n| 0.14         | 0.6           |\n| 0.13         | 0.5           |\n| 0.12         | 0.4           |\n| 0.11         | 0.3           |\n| 0.10         | 0.2           |\n| 0.9          | 0.1           |\n| 0.8          | 0.0.3         |\n\nSince version 0.16, we're following bevy's versions. Make sure the crate versions match!\n\n## Note on reliability\n\nWhile this crate has seen some use in the world with no issues yet,\nit is still quite new and experimental. Use with caution (and miri!).\n\nIf you find a bug, please [open an issue](https://github.com/JoJoJet/bevy-trait-query/issues).\n\n## Overview\n\n\u003c!-- cargo-rdme start --\u003e\n\nLet's say you have a trait that you want to implement for some of your components.\n\n```rust\n/// Components that display a message when hovered.\npub trait Tooltip {\n    /// Text displayed when hovering over an entity with this trait.\n    fn tooltip(\u0026self) -\u003e \u0026str;\n}\n```\n\nIn order to be useful within bevy, you'll want to be able to query for this trait.\n\n```rust\n\n// Just add this attribute...\n#[bevy_trait_query::queryable]\npub trait Tooltip {\n    fn tooltip(\u0026self) -\u003e \u0026str;\n}\n\n// ...and now you can use your trait in queries.\nfn show_tooltips_system(\n    tooltips: Query\u003c\u0026dyn Tooltip\u003e,\n    // ...\n) {\n    // ...\n}\n```\n\nSince Rust unfortunately lacks any kind of reflection, it is necessary to register each\ncomponent with the trait when the app gets built.\n\n```rust\n#[derive(Component)]\nstruct Player(String);\n\n#[derive(Component)]\nenum Villager {\n    Farmer,\n    // ...\n}\n\n#[derive(Component)]\nstruct Monster;\n\n/* ...trait implementations omitted for brevity... */\n\nstruct TooltipPlugin;\n\nimpl Plugin for TooltipPlugin {\n    fn build(\u0026self, app: \u0026mut App) {\n        // We must import this trait in order to register our components.\n        // If we don't register them, they will be invisible to the game engine.\n        use bevy_trait_query::RegisterExt;\n\n        app\n            .register_component_as::\u003cdyn Tooltip, Player\u003e()\n            .register_component_as::\u003cdyn Tooltip, Villager\u003e()\n            .register_component_as::\u003cdyn Tooltip, Monster\u003e()\n            .add_systems(Update, show_tooltips);\n    }\n}\n```\n\nUnlike queries for concrete types, it's possible for an entity to have multiple components\nthat match a trait query.\n\n```rust\n\nfn show_tooltips(\n    tooltips: Query\u003c\u0026dyn Tooltip\u003e,\n    // ...\n) {\n    // Iterate over each entity that has tooltips.\n    for entity_tooltips in \u0026tooltips {\n        // Iterate over each component implementing `Tooltip` for the current entity.\n        for tooltip in entity_tooltips {\n            println!(\"Tooltip: {}\", tooltip.tooltip());\n        }\n    }\n\n    // If you instead just want to iterate over all tooltips, you can do:\n    for tooltip in tooltips.iter().flatten() {\n        println!(\"Tooltip: {}\", tooltip.tooltip());\n    }\n}\n```\n\nAlternatively, if you expect to only have component implementing the trait for each entity,\nyou can use the filter [`One`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/one/struct.One.html). This has significantly better performance than iterating\nover all trait impls.\n\n```rust\nuse bevy_trait_query::One;\n\nfn show_tooltips(\n    tooltips: Query\u003cOne\u003c\u0026dyn Tooltip\u003e\u003e,\n    // ...\n) {\n    for tooltip in \u0026tooltips {\n        println!(\"Tooltip: {}\", tooltip.tooltip());\n    }\n}\n```\n\nTrait queries support basic change detection filtration.\n\n- queries requesting shared access yield [`ReadTraits`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/all/struct.ReadTraits.html) which is\n  similar to [`Ref`](https://docs.rs/bevy/latest/bevy/ecs/change_detection/struct.Ref.html)\n- queries requesting exclusive access yield [`WriteTraits`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/all/struct.WriteTraits.html) which is\n  similar to [`Mut`](https://docs.rs/bevy/latest/bevy/ecs/change_detection/struct.Mut.html)\n\nTo get all the components that implement the target trait, and have also changed in some way\nsince the last tick, you can:\n```rust\nfn show_tooltips(\n    tooltips_query: Query\u003cAll\u003c\u0026dyn Tooltip\u003e\u003e\n    // tooltips_query: Query\u003c\u0026dyn Tooltip\u003e  // \u003c-- equivalent to line above\n    // ...\n) {\n    // Iterate over all entities with at least one component implementing `Tooltip`\n    for entity_tooltips in \u0026tooltips_query {\n        // Iterate over each component for the current entity that changed since the last time the system was run.\n        for tooltip in entity_tooltips.iter_changed() {\n            println!(\"Changed Tooltip: {}\", tooltip.tooltip());\n        }\n    }\n}\n```\n\nSimilar to [`iter_changed`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/all/struct.ReadTraits.html), we have [`iter_added`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/all/struct.ReadTraits.html)\nto detect entities which have had a trait-implementing component added since the last tick.\n\nIf you know you have only one component that implements the target trait,\nyou can use [`OneAdded`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/one/struct.OneAdded.html) or [`OneChanged`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/one/struct.OneChanged.html) which behave more like the typical\n`bevy` [`Added`](https://docs.rs/bevy/latest/bevy/ecs/query/struct.Added.html)/[`Changed`](https://docs.rs/bevy/latest/bevy/ecs/query/struct.Changed.html) filters:\n```rust\nfn show_tooltips(\n    tooltips_query: Query\u003cOne\u003c\u0026dyn Tooltip\u003e, OneChanged\u003cdyn Tooltip\u003e\u003e\n    // ...\n) {\n    // Iterate over each entity that has one tooltip implementing component that has also changed\n    for tooltip in \u0026tooltips_query {\n        println!(\"Changed Tooltip: {}\", tooltip.tooltip());\n    }\n}\n```\nNote in the above example how [`OneChanged`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/one/struct.OneChanged.html) does *not* take a reference to the trait object!\n\n### Performance\n\nThe performance of trait queries is quite competitive. Here are some benchmarks for simple cases:\n\n|                   | Concrete type  | [`One\u003cdyn Trait\u003e`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/one/struct.One.html)    | [`All\u003cdyn Trait\u003e`](https://docs.rs/bevy-trait-query/latest/bevy_trait_query/all/struct.All.html) |\n|-------------------|----------------|---------------------|-------------------|\n| 1 match           | 8.395 µs       | 28.174 µs           | 81.027 µs         |\n| 2 matches         | 8.473 µs       | -                   | 106.47 µs         |\n| 1-2 matches       | -              | 14.619 µs           | 92.876 µs         |\n\n\u003c!-- cargo-rdme end --\u003e\n\n# License\n\n[MIT](LICENSE-MIT) or [APACHE-2.0](LICENSE-APACHE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseph-gio%2Fbevy-trait-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoseph-gio%2Fbevy-trait-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseph-gio%2Fbevy-trait-query/lists"}