Crate behaviortree_derive

Crate behaviortree_derive 

Source
Expand description

Derive macros for Behaviors. There are 4 derive macros avialable:

  • Action
  • Condition
  • Control
  • Decorator

On struct level there are the attributes

  • #[behavior(no_create)]: will suppress the derive of create_fn(…)
  • #[behavior(no_register)]: will suppress the derive of register()
  • #[behavior(no_register_with)]: will suppress the derive of register_with(…)

On field level there are the attributes

  • #[behavior(parameter)]: defines a field as a parameter for create_fn(...) and register_with(...) The values must be given when using the register_with(...) method. A parameter must implement Clone.
  • #[behavior(default = <Expression>)]: defines a default value for a field. ‘Expression’ can be any Rust expression that creates an appropriate value out of nothing.

§Usage

Using the derive macro Action, the others work respectively.

#[derive(Action)]
struct MyAction {
    // specific elements
    ...
}

impl MyAction {
    // specific implementations
    ...
}

§Result

Expands the above example to

struct MyAction {
    // specific elements
    ...
}

impl MyAction {
    // specific implementations
    ...
}

#[automatically_derived]
#[diagnostic::do_not_recommend]
impl behaviortree::behavior::Behavior for MyAction {
    fn creation_fn() -> alloc::boxed::Box<behaviortree::behavior::BehaviorCreationFn> {
        ...
    }

    fn kind() -> behaviortree::behavior::BehaviorKind {
        ...
    }
}

#[automatically_derived]
#[diagnostic::do_not_recommend]
impl behaviortree::behavior::BehaviorExecution for MyAction {
    fn as_any(&self) -> &dyn core::any::Any { self }
    fn as_any_mut(&mut self) -> &mut dyn core::any::Any { self }
    fn static_provided_ports(&self) -> behaviortree::port::PortList { Self::provided_ports() }
}

The macros also creates a function to create the behaviorCreationFunction, a default registration function and if parameters are given a register_with function. The creation of the all three functions can be controlled by attributes no_create, no_register and no_register_with independently.

pub struct MyActionWithParameters {
    #[behavior(parameter)]
    arg1: i32,
    #[behavior(parameter)]
    arg2: String,
}

#[automatically_derived]
#[diagnostic::do_not_recommend]
impl MyActionWithParameters {
    /// Behavior creation function
    fn create_fn() -> alloc::boxed::Box<behaviortree::behavior::BehaviorCreationFn> {
        ...
    }
    /// Registers the behavior.
    pub fn register(
        factory: &mut behaviortree::factory::BehaviorTreeFactory,
        name: &str,
    ) -> Result<(), behaviortree::factory::error::Error> {
        ...
    }
    /// Registers the behavior with parameter.
    pub fn register_with(
        factory: &mut behaviortree::factory::BehaviorTreeFactory,
        name: &str,
        arg1: i32,
        arg2: String,
    ) -> Result<(), behaviortree::factory::error::Error> {
        ...
    }
}

§Errors

  • if attributes are used in a wrong way

§Panics

  • if used on enums or unions or functions

Derive Macros§

Action
Derive macro for an Action type Behavior, usage.
Condition
Derive macro for an Condition type Behavior, usage.
Control
Derive macro for an Control type Behavior, usage.
Decorator
Derive macro for an Decorator type Behavior, usage.