dscale 0.7.1

A fast & deterministic simulation framework for benchmarking and testing distributed systems
Documentation
// DScale: deterministic distributed systems simulator
// Copyright (C) 2026  Konstantin Shprenger

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::{MessagePtr, random::Seed, timer::TimerId};

/// Unique identifier for a process within the simulation. Pids are assigned
/// sequentially starting from 0 in the order pools are added.
pub type Pid = usize;

/// Core trait for defining process behavior in the simulation.
///
/// Each process reacts to three kinds of events: startup, incoming messages,
/// and timer firings. Inside any handler you may call the global interaction
/// functions ([`crate::send_to`], [`crate::broadcast`], [`crate::schedule_timer_after`], etc.).
pub trait Process {
    /// Called once when the simulation starts, before any messages are delivered. Provides simulation seed for process.
    fn on_start(&mut self, seed: Seed);

    /// Called when a message arrives from another process.
    fn on_message(&mut self, from: Pid, message: MessagePtr);

    /// Called when a previously scheduled timer fires.
    fn on_timer(&mut self, id: TimerId);
}

impl<T: Process + ?Sized> Process for Box<T> {
    fn on_start(&mut self, seed: Seed) {
        (**self).on_start(seed)
    }

    fn on_message(&mut self, from: Pid, message: MessagePtr) {
        (**self).on_message(from, message)
    }

    fn on_timer(&mut self, id: TimerId) {
        (**self).on_timer(id)
    }
}

pub(crate) type ProcessPtr = Box<dyn Process + Send>;