pub struct View<'q, Q: Query> { /* private fields */ }Expand description
Provides random access to the results of a query
Views borrow all components that they expose, from when the view is created until it’s dropped. As with any borrowing pattern, they should usually be short-lived to avoid conflicts with distant code that might want to borrow the same components.
Implementations§
Source§impl<'q, Q: Query> View<'q, Q>
impl<'q, Q: Query> View<'q, Q>
Sourcepub fn get(&self, entity: Entity) -> Option<Q::Item<'_>>where
Q: QueryShared,
pub fn get(&self, entity: Entity) -> Option<Q::Item<'_>>where
Q: QueryShared,
Retrieve the query results corresponding to entity
Will yield None if the entity does not exist or does not match the query.
Does not require exclusive access to the map, but is defined only for queries yielding only shared references.
Sourcepub fn get_mut(&mut self, entity: Entity) -> Option<Q::Item<'_>>
pub fn get_mut(&mut self, entity: Entity) -> Option<Q::Item<'_>>
Retrieve the query results corresponding to entity
Will yield None if the entity does not exist or does not match the query.
Sourcepub fn contains(&self, entity: Entity) -> bool
pub fn contains(&self, entity: Entity) -> bool
Equivalent to get(entity).is_some(), but does not require Q: QueryShared
Sourcepub unsafe fn get_unchecked(&self, entity: Entity) -> Option<Q::Item<'_>>
pub unsafe fn get_unchecked(&self, entity: Entity) -> Option<Q::Item<'_>>
Like get_mut, but allows simultaneous access to multiple entities
§Safety
Must not be invoked while any unique borrow of the fetched components of entity is live.
Sourcepub fn get_disjoint_mut<const N: usize>(
&mut self,
entities: [Entity; N],
) -> [Option<Q::Item<'_>>; N]
pub fn get_disjoint_mut<const N: usize>( &mut self, entities: [Entity; N], ) -> [Option<Q::Item<'_>>; N]
Like get_mut, but allows checked simultaneous access to multiple entities
For N > 3, the check for distinct entities will clone the array and take O(N log N) time.
§Examples
let mut world = World::new();
let a = world.spawn((1, 1.0));
let b = world.spawn((2, 4.0));
let c = world.spawn((3, 9.0));
let mut query = world.query_mut::<&mut i32>();
let mut view = query.view();
let [a,b,c] = view.get_disjoint_mut([a, b, c]);
assert_eq!(*a.unwrap(), 1);
assert_eq!(*b.unwrap(), 2);
assert_eq!(*c.unwrap(), 3);Sourcepub fn iter_mut(&mut self) -> ViewIter<'_, Q>
pub fn iter_mut(&mut self) -> ViewIter<'_, Q>
Iterate over all entities satisfying Q
Equivalent to QueryBorrow::iter.