Struct rs_ecs::QueryRef

source ·
pub struct QueryRef<'w, S>
where S: QuerySpec,
{ /* private fields */ }
Expand description

Borrow of the World for a Query. Required to obtain an iterator.

Implementations§

source§

impl<S> QueryRef<'_, S>
where S: QuerySpec,

source

pub fn for_each<'q, F>(&'q mut self, f: F)
where F: FnMut(<S::Fetch as Fetch<'q>>::Item),

Visit all entities matching the query.

source

pub fn iter<'q>(&'q mut self) -> QueryIter<'q, S>

Create an iterator over the entities matching the query.

source

pub fn par_iter<'q>(&'q mut self) -> QueryParIter<'q, S>
where <S::Fetch as Fetch<'q>>::Item: Send,

Create a parallel iterator over the entities matching the query.

§Examples
use rayon::join;

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 query1 = Query::<&mut i32>::new();
let mut ref1 = query1.borrow(&world);
let mut iter1 = ref1.iter();

let mut query2 = Query::<(&u32, &mut f32)>::new();
let mut ref2 = query2.borrow(&world);
let mut iter2 = ref2.par_iter();

join(
    move || {
        for i in iter1 {
            *i -= 1;
        }
    },
    move || {
        iter2.for_each(|(u, f)| {
            *f += *u as f32;
        });
    },
);
source

pub fn map<'q>(&'q mut self) -> QueryMap<'q, S>

Create a map of the entities matching the query.

This is an alternative to query_one for repeated calls, to amortize the set-up costs of random access.

§Examples
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_i32, 1.0_f32));

let mut query = Query::<(&i32, &f32)>::new();
let mut query = query.borrow(&world);
let mut query = query.map();

let (i, f) = query.get_mut(entity).unwrap();

assert_eq!(*i, 42);
assert_eq!(*f, 1.0);

Auto Trait Implementations§

§

impl<'w, S> !RefUnwindSafe for QueryRef<'w, S>

§

impl<'w, S> !Send for QueryRef<'w, S>

§

impl<'w, S> !Sync for QueryRef<'w, S>

§

impl<'w, S> Unpin for QueryRef<'w, S>
where <<S as QuerySpec>::Fetch as Fetch<'w>>::Ref: Unpin,

§

impl<'w, S> !UnwindSafe for QueryRef<'w, S>

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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>,

§

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>,

§

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.