Skip to main content

Crate simvar_utils

Crate simvar_utils 

Source
Expand description

Utility functions for simulation testing and cancellation management.

This crate provides utilities for managing worker threads and cancellation tokens in simulation environments. It supports both thread-local and global cancellation, allowing tests to gracefully terminate simulations and async operations.

§Features

  • Thread Management: Unique worker thread ID tracking
  • Cancellation Tokens: Thread-local and global cancellation support
  • Async Utilities: Run futures until simulation cancellation

§Example

use simvar_utils::{worker_thread_id, run_until_simulation_cancelled};

// Get unique thread ID
let thread_id = worker_thread_id();
println!("Worker thread ID: {}", thread_id);

// Run future until cancelled
let result = run_until_simulation_cancelled(async {
    simulate_work().await
}).await;

match result {
    Some(output) => println!("Completed: {}", output),
    None => println!("Cancelled"),
}

Functions§

cancel_global_simulation
Cancels all simulations globally.
cancel_simulation
Cancels the current thread’s simulation.
is_global_simulator_cancelled
Checks if the global simulation has been cancelled.
is_simulator_cancelled
Checks if the current thread’s simulation has been cancelled.
reset_global_simulator_cancellation_token
Resets the global simulation cancellation token.
reset_simulator_cancellation_token
Resets the thread-local simulation cancellation token.
run_until_simulation_cancelled
Runs a future until it completes or simulation is cancelled.
worker_thread_id
Returns the unique identifier for the current worker thread.