odem-rs 0.3.0

Object-based Discrete-Event Modelling in Rust using async/await
Documentation
//! This example demonstrates transient entities backed by a fixed-size object
//! pool.
//!
//! A generator creates 1000 network packets at regular intervals. Each packet
//! is allocated from an `Array`-backed `Pool` with a capacity of 10, simulates
//! a network traversal, and then terminates — automatically returning its slot
//! to the pool. This pattern is useful for modeling high-throughput systems
//! where entities have short lifetimes and memory must be bounded.

use core::pin::pin;
use odem_rs::{prelude::*, util::pool::Array};

// 1. Define the transient entity.
struct Packet {
	#[allow(dead_code)]
	id: usize,
}

impl Behavior for Packet {
	type Output = ();

	async fn actions(&self, sim: &Sim) {
		// Simulate network traversal time.
		sim.advance(10.0).await;
		// When this function ends, the Packet terminates. If no other
		// entity holds a reference to it, the Pool will reclaim its memory.
	}
}

// 2. Define the main simulation driver with the generator.
fn main() {
	simulation(async |sim| {
		let pool = pin!(Pool::new(Array::<_, 10>::new()));

		for i in 0..1000 {
			// Wait for some time before creating the next packet.
			sim.advance(1.0).await;

			// Allocate a new Packet from the pool.
			// Panics if an allocation is attempted when all slots are in use.
			let packet = pool.alloc(Agent::new(Packet { id: i }));

			// Activate the new packet, starting its lifecycle.
			sim.activate(packet);
		}
	})
	.expect("deadlock impossible");
}