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::services::{Services, with_services};

impl Services {
    fn kv_set<T: 'static + Send + Sync>(&self, key: &str, value: T) {
        self.kv.insert(key.to_string(), Box::new(value));
    }
    fn kv_get<T: 'static + Clone + Send + Sync>(&self, key: &str) -> T {
        self.kv
            .get(key)
            .expect("no key")
            .downcast_ref::<T>()
            .cloned()
            .expect("wrong type cast")
    }
    fn kv_modify<T: 'static + Send + Sync>(&self, key: &str, f: impl FnOnce(&mut T)) {
        let mut entry = self.kv.get_mut(key).expect("no key");
        f(entry.downcast_mut::<T>().expect("wrong type cast"));
    }
}

/// Stores a value under the given key, replacing any previous value.
pub fn set<T: 'static + Send + Sync>(key: &str, value: T) {
    with_services(|services| services.kv_set(key, value))
}

/// Retrieves a clone of the value stored under the given key.
/// Panics if the key is missing or the type does not match.
pub fn get<T: 'static + Clone + Send + Sync>(key: &str) -> T {
    with_services(|services| services.kv_get(key))
}

/// Mutates the value stored under the given key in place.
/// Panics if the key is missing or the type does not match.
pub fn modify<T: 'static + Send + Sync>(key: &str, f: impl FnOnce(&mut T)) {
    with_services(|services| services.kv_modify(key, f))
}