LendingIterator

Trait LendingIterator 

Source
pub trait LendingIterator: for<'a> Lifetime<'a> {
    // Required method
    fn next(&mut self) -> Option<<Self as Lifetime<'_>>::Item>;

    // Provided method
    fn for_each<F>(self, f: F)
       where Self: Sized,
             F: FnMut(<Self as Lifetime<'_>>::Item) { ... }
}
Expand description

A trait similar to Iterator, where the returned Items only live until next is called again.

See also: https://sabrinajewson.org/blog/the-better-alternative-to-lifetime-gats

§Examples

Unlike the regular Iterators the LendingIterator does not support for loops. A while let loop can be used instead:

while let Some(item) = iter.next() {
    println!("{item:?}");
}

See Query for more examples, since it is the only type currently implementing LendingIterator.

Required Methods§

Source

fn next(&mut self) -> Option<<Self as Lifetime<'_>>::Item>

Advances the iterator and returns the next value.

The value’s lifetime ends before next is called again.

Provided Methods§

Source

fn for_each<F>(self, f: F)
where Self: Sized, F: FnMut(<Self as Lifetime<'_>>::Item),

Calls a closure on each element of the iterator.

This is an alternative to using a LendingIterator in a while let loop.

§Examples

See Query for more examples, since it is the only type currently implementing LendingIterator.

use checs::{ComponentVec, IntoQuery, LendingIterator};

let ints: ComponentVec<i32> = ComponentVec::new();
let mut floats: ComponentVec<f32> = ComponentVec::new();

let query = (&ints, &mut floats).into_query();

query.for_each(|(entity, (int, float))| {
    println!("{entity:?}: ({int}, {float})");
});
Examples found in repository?
examples/basic.rs (lines 45-47)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Iter, C> LendingIterator for Query<Iter, C>
where Iter: Iterator<Item = Entity>, C: GetRefOrMut,