Struct Query

Source
pub struct Query<S>
where S: QuerySpec,
{ /* private fields */ }
Expand description

Query to get an iterator over all entities with a certain combination of components.

Queries are specified through their type argument, by composing the type of their result. The following type specifications are possible:

  • &C - shared, immutable reference to components of type C
  • &mut C - exclusive, mutable reference
  • (P, Q, R) - combine multiple queries
  • Option<Q> - optional component(s)
  • With<Q, C> to filter Q for presence of C
  • Without<Q, C> to filter Q for absence of C
  • Matches<Q> to indicate which entities match Q`

Note that Entities are components themselves, so they can be optionally obtained in a query, like Query<Entity, &C, &mut D>.

Queries are provided as stand-alone structs to allow for prepared queries that can be re-used, as an optimzation. Hence, queries need to borrow the World before their results can be iterated (see Query::borrow).

§Examples

let mut world = World::new();

let entity1 = world.alloc();
world.insert(entity1, (42_i32, 23_u32, 1.0_f32));

let entity2 = world.alloc();
world.insert(entity2, (0_i32, true));

let mut query = Query::<(&mut i32, Option<(&u32, &mut f32)>)>::new();
for (i, opt) in query.borrow(&world).iter() {
    if let Some((u, f)) = opt {
        *i += *u as i32;
        *f += 1.0
    } else {
        *i -= 1;
    }
}

assert_eq!(*world.query_one::<&i32>(entity1).unwrap().get(), 65);
assert_eq!(*world.query_one::<&f32>(entity1).unwrap().get(), 2.0);
assert_eq!(*world.query_one::<&i32>(entity2).unwrap().get(), -1);

Use of a prepared query that is stored and reused for optimization:

#[derive(Default)]
struct System {
    query: Query<(&'static i32, &'static mut bool)>
}

impl System {
    pub fn update(&mut self, world: &World) {
        for (i, b) in self.query.borrow(world).iter() {
            *b = *i >= 0;
        }
    }
}

fn main() {
    let mut world = World::new();

    let entity = world.alloc();
    world.insert(entity, (23_i32, false));

    let mut system = System::default();

    for _ in 0..42 {
        system.update(&world);
    }
}

Implementations§

Source§

impl<S> Query<S>
where S: QuerySpec,

Source

pub fn new() -> Self

Create a query.

§Examples
let mut immutable_query = Query::<(&i32,)>::new();
let mut mutable_query = Query::<(&i32, &mut bool)>::new();
let mut query_with_entity = Query::<(&Entity, &i32, &mut bool)>::new();
Source

pub fn borrow<'w>(&'w mut self, world: &'w World) -> QueryRef<'w, S>

Borrow the World to allow for iterating the query.

§Examples
let mut world = World::new();
let mut query = Query::<(&i32, &bool)>::new();
let mut borrow = query.borrow(&world);

for (i, b) in borrow.iter() {
    println!("{}, {}", i, b);
}

Trait Implementations§

Source§

impl<S> Default for Query<S>
where S: QuerySpec,

Source§

fn default() -> Self

Create a query.

Source§

impl<S> Send for Query<S>
where S: QuerySpec,

Auto Trait Implementations§

§

impl<S> Freeze for Query<S>
where <<S as QuerySpec>::Fetch as Fetch<'static>>::Ty: Freeze,

§

impl<S> RefUnwindSafe for Query<S>
where <<S as QuerySpec>::Fetch as Fetch<'static>>::Ty: RefUnwindSafe, <<S as QuerySpec>::Fetch as Fetch<'static>>::Ptr: RefUnwindSafe,

§

impl<S> Sync for Query<S>
where <<S as QuerySpec>::Fetch as Fetch<'static>>::Ty: Sync, <<S as QuerySpec>::Fetch as Fetch<'static>>::Ptr: Sync,

§

impl<S> Unpin for Query<S>
where <<S as QuerySpec>::Fetch as Fetch<'static>>::Ty: Unpin,

§

impl<S> UnwindSafe for Query<S>
where <<S as QuerySpec>::Fetch as Fetch<'static>>::Ty: UnwindSafe, <<S as QuerySpec>::Fetch as Fetch<'static>>::Ptr: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.