pub trait Behavior<C = ()>{
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§
Required Methods§
Sourceasync fn actions(&self, sim: &Sim<C>) -> Self::Output
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§
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.