Struct Lazy

Source
pub struct Lazy { /* private fields */ }
Expand description

Lazy updates can be used for world updates that need to borrow a lot of resources and as such should better be done at the end. They work lazily in the sense that they are dispatched when calling world.maintain().

Lazy updates are dispatched in the order that they are requested. Multiple updates sent from one system may be overridden by updates sent from other systems.

Please note that the provided methods take &self so there’s no need to get Lazy mutably. This resource is added to the world by default.

Implementations§

Source§

impl Lazy

Source

pub fn exec<F>(&self, f: F)
where F: FnOnce(&mut World) + Send + Sync + 'static,

Lazily executes a closure with world access.

§Examples
struct Pos;

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct Execution;

impl<'a> System<'a> for Execution {
    type SystemData = (Entities<'a>, Read<'a, Lazy>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        for entity in ent.join() {
            lazy.exec(move |world| {
                if world.is_alive(entity) {
                    println!("Entity {:?} is alive.", entity);
                }
            });
        }
    }
}
Source

pub fn exec_async<F>(&self, f: F)
where F: FnOnce(&mut World) -> BoxFuture<'static, ()> + Send + Sync + 'static,

Same as Lazy::exec but with async response.

Source

pub fn insert<C>(&self, e: Entity, c: C)
where C: Component + Send + Sync,

Lazily inserts a component for an entity.

§Examples
struct Pos(f32, f32);

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct InsertPos;

impl<'a> System<'a> for InsertPos {
    type SystemData = (Entities<'a>, Read<'a, Lazy>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        let a = ent.create();
        lazy.insert(a, Pos(1.0, 1.0));
    }
}
Source

pub fn insert_many<C, I>(&self, iter: I)
where C: Component + Send + Sync, I: IntoIterator<Item = (Entity, C)> + Send + Sync + 'static,

Lazily inserts components for entities.

§Examples
struct Pos(f32, f32);

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct InsertPos;

impl<'a> System<'a> for InsertPos {
    type SystemData = (Entities<'a>, Read<'a, Lazy>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        let a = ent.create();
        let b = ent.create();

        lazy.insert_many(vec![(a, Pos(3.0, 1.0)), (b, Pos(0.0, 4.0))]);
    }
}
Source

pub fn remove<C>(&self, e: Entity)
where C: Component,

Lazily removes a component.

§Examples
struct Pos;

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct RemovePos;

impl<'a> System<'a> for RemovePos {
    type SystemData = (Entities<'a>, Read<'a, Lazy>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        for entity in ent.join() {
            lazy.remove::<Pos>(entity);
        }
    }
}
Source

pub fn remove_many<C, I>(&self, iter: I)
where C: Component, I: IntoIterator<Item = Entity> + Send + Sync + 'static,

Lazily removes a component.

§Examples
struct Pos;

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct RemovePos;

impl<'a> System<'a> for RemovePos {
    type SystemData = (Entities<'a>, Read<'a, Lazy>);

    fn run(&mut self, (ent, lazy): Self::SystemData) {
        for entity in ent.join() {
            lazy.remove::<Pos>(entity);
        }
    }
}
Source

pub fn create_entity(&self, world: &World) -> LazyBuilder<'_>

Creates a new LazyBuilder which inserts components using Lazy. This means that the components won’t be available immediately, but only after a maintain on World is performed.

§Examples
struct Pos(f32, f32);

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

let my_entity = lazy.create_entity(&world).with(Pos(1.0, 3.0)).build();
Source

pub async fn maintain(&self, world: &mut World)

Executes all stored lazy updates

Trait Implementations§

Source§

impl Clone for Lazy

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Lazy

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Lazy

§

impl RefUnwindSafe for Lazy

§

impl Send for Lazy

§

impl Sync for Lazy

§

impl Unpin for Lazy

§

impl UnwindSafe for Lazy

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> Any for T
where T: Any,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryDefault for T
where T: Default,

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.
Source§

impl<T> Resource for T
where T: Any + Send + Sync,