Skip to content

Commit

Permalink
docs(Query,QueryBuilder): add doc test showcasing how to return query…
Browse files Browse the repository at this point in the history
… from function
  • Loading branch information
Indra-db committed Dec 23, 2024
1 parent 6884aca commit 1480395
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions flecs_ecs/src/core/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ use crate::sys;
/// You need to ensure that you're holding no query objects anymore when the world is destroyed.
/// This will otherwise panic.
///
/// # Example
///
/// * how to return a query / query builder from a function see example in [`QueryBuilder`]
///
/// # See also
///
/// * [`QueryBuilder`]
Expand Down
39 changes: 37 additions & 2 deletions flecs_ecs/src/core/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,34 @@ use crate::core::internals::*;
use crate::core::*;
use crate::sys;

/// Fast to iterate, but slower to create than Filter
/// Builder for [`Query`].
///
/// # Example
///
/// This example shows how to return a query or query builder from a function. The lifetime `'static`
/// is required in the type `&'static Foo` to ensure the components accessed by the query
/// live long enough to be safely used.
///
/// ```
/// use flecs_ecs::prelude::*;
///
/// #[derive(Component)]
/// struct Foo(u8);
///
/// fn foo_query(wrld: &World) -> Query<&'static Foo> {
/// query!(wrld, &Foo).build()
/// }
///
/// fn plugin(wrld: &World) {
/// let foos = foo_query(wrld);
///
/// wrld.system::<()>().each(move |_| {
/// foos.each(|foo| {
/// // ..
/// });
/// });
/// }
/// ```
pub struct QueryBuilder<'a, T>
where
T: QueryTuple,
Expand Down Expand Up @@ -207,7 +234,15 @@ where

/// Build the `query_builder` into an query
///
/// See also
/// # Returns
///
/// The built query
///
/// # Example
///
/// * how to return a query / query builder from a function see example in [`QueryBuilder`]
///
/// # See also
///
/// * C++ API: `node_builder::build`
#[doc(alias = "node_builder::build")]
Expand Down

0 comments on commit 1480395

Please sign in to comment.