Trait Behavior

Source
pub trait Behavior<C = ()>
where C: Config + ?Sized,
{ type Output; // Required method async fn actions(&self, sim: &Sim<C>) -> Self::Output; // Provided method fn name(&self) -> &'static str { ... } }
Expand description

Defines the primary asynchronous behavior and output type for an Agent.

This trait provides a convenient way to specify what an agent does. By implementing Behavior for a type T, you allow Agent::new(T) to automatically construct an agent using the provided actions method.

The name method determines the default base name for the agent’s label, defaulting to the type name of T.

A blanket implementation Behavior<C> for (I, A) exists, allowing you to define agents by simply pairing a state type I with an appropriate async fn(&I, &Sim<C>) -> R function, without needing a separate impl block or a newtype wrapper.

§Examples

struct MyAgentState(f64);

impl Behavior for MyAgentState {
    type Output = (); // No return value

    async fn actions(&self, sim: &Sim) {
        println!("Waiting for {} time units.", self.0);
        sim.advance(self.0).await;
        println!("Done waiting.");
    }
}
// Use Agent::new thanks to the Behavior implementation
let agent = pin!(Agent::new(MyAgentState(5.0)));
sim.activate(agent).await;

Using the tuple blanket implementation:

struct MyState(u32);
async fn my_async_fn(state: &MyState, sim: &Sim) { /* ... */ }
// Use Agent::new with a tuple (state, function)
let agent = pin!(Agent::new((MyState(10), my_async_fn)));
sim.activate(agent);

Required Associated Types§

Source

type Output

The type returned by the agent’s actions method upon successful completion.

Required Methods§

Source

async fn actions(&self, sim: &Sim<C>) -> Self::Output

The core asynchronous logic of the agent.

This method receives an immutable reference to the agent’s state and the simulation context. It defines the sequence of operations and simulation interactions (like sim.advance()) the agent performs.

The returned Future cannot capture non-'static data from the surrounding environment, enforced implicitly by how Agent constructs the underlying task. Send is not required as simulations are single-threaded.

Provided Methods§

Source

fn name(&self) -> &'static str

Returns the base name for this agent type.

Defaults to the type_name of Self. Override this if a different base name is desired for logging or identification.

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.

Implementations on Foreign Types§

Source§

impl<C, I, A, R> Behavior<C> for (I, A)
where C: Config + ?Sized, I: Any, A: AsyncFn(&I, &Sim<C>) -> R,

Source§

type Output = R

Source§

fn actions(&self, sim: &Sim<C>) -> impl Future<Output = R>

Source§

fn name(&self) -> &'static str

Implementors§