Skip to main content

Command

Trait Command 

Source
pub trait Command: Send + 'static {
    type Out: CommandOutput;

    // Required method
    fn apply(self, world: &mut World) -> Self::Out;

    // Provided methods
    fn handle_error_with(
        self,
        error_handler: ErrorHandler,
    ) -> impl Command<Out = ()>
       where Self: Sized { ... }
    fn handle_error(self) -> impl Command<Out = ()>
       where Self: Sized { ... }
    fn ignore_error(self) -> impl Command<Out = ()>
       where Self: Sized { ... }
}
Expand description

A World mutation.

Should be used with Commands::queue.

The Out generic parameter is the returned “output” of the command.

§Usage

// Our world resource
#[derive(Resource, Default)]
struct Counter(u64);

// Our custom command
struct AddToCounter(u64);

impl Command for AddToCounter {
    type Out = ();

    fn apply(self, world: &mut World) {
        let mut counter = world.get_resource_or_insert_with(Counter::default);
        counter.0 += self.0;
    }
}

fn some_system(mut commands: Commands) {
    commands.queue(AddToCounter(42));
}

Required Associated Types§

Source

type Out: CommandOutput

The return type of apply.

Required Methods§

Source

fn apply(self, world: &mut World) -> Self::Out

Applies this command, causing it to mutate the provided world.

This method is used to define what a command “does” when it is ultimately applied. Because this method takes self, you can store data or settings on the type that implements this trait. This data is set by the system or other source of the command, and then ultimately read in this method.

Provided Methods§

Source

fn handle_error_with( self, error_handler: ErrorHandler, ) -> impl Command<Out = ()>
where Self: Sized,

Takes a Command that returns a Result and uses a given error handler function to convert it into a Command that internally handles an error if it occurs and returns ().

Source

fn handle_error(self) -> impl Command<Out = ()>
where Self: Sized,

Takes a Command that returns a Result and uses the fallback error handler function to convert it into a Command that internally handles an error if it occurs and returns ().

Source

fn ignore_error(self) -> impl Command<Out = ()>
where Self: Sized,

Takes a Command that returns a Result and ignores any error that occurs.

Implementors§

Source§

impl<F, Out> Command for F
where F: FnOnce(&mut World) -> Out + Send + 'static, Out: CommandOutput,

Source§

type Out = Out