hecs_schedule/query.rs
1use std::any::type_name;
2
3use crate::{Error, Result};
4use hecs::{Entity, Query};
5
6/// Wraps the bulting QueryOne with a Result containing the entity and component instead of option
7pub struct QueryOne<'a, Q: Query> {
8 entity: Entity,
9 query: hecs::QueryOne<'a, Q>,
10}
11
12impl<'a, Q: Query> QueryOne<'a, Q> {
13 pub(crate) fn new(entity: Entity, query: hecs::QueryOne<'a, Q>) -> Self {
14 Self { entity, query }
15 }
16
17 /// Get the query result, or return an error if the entity does not satisfy the query
18 ///
19 /// Must be called at most once.
20 ///
21 /// Panics if called more than once or if it would construct a borrow that clashes with another
22 /// pre-existing borrow.
23 // Note that this uses self's lifetime, not 'a, for soundness.
24 pub fn get(&mut self) -> Result<Q::Item<'_>> {
25 match self.query.get() {
26 Some(val) => Ok(val),
27 None => Err(Error::UnsatisfiedQuery(self.entity, type_name::<Q>())),
28 }
29 }
30}