Skip to main content

SimEnv

Struct SimEnv 

Source
pub struct SimEnv { /* private fields */ }
Expand description

The simulation environment. Central coordinator for a single simulation run.

SimEnv is !Send + !Sync (via Rc) and must live on one thread. For Monte Carlo parallelism, create independent SimEnv instances on separate threads.

The typical shape of every simulation: create the env, pass handle() clones into spawned processes, run, read out results.

use simu::SimEnv;

let mut env = SimEnv::with_seed(1);
let h = env.handle();
env.spawn(async move {
    h.timeout(10.0).await; // suspend for 10 simulated time units
});
env.run(); // drive the event loop until the queue drains
assert_eq!(env.now(), 10.0);

Implementations§

Source§

impl SimEnv

Source

pub fn new() -> Self

Create a new environment seeded from OS entropy.

Use with_seed when reproducibility is required.

Source

pub fn with_seed(seed: u64) -> Self

Create a new environment with a fixed RNG seed.

Given the same seed and process logic the simulation will produce identical results across runs. Uses rand’s StdRng; for a portable, cross-language stream use with_source with a SplitMix64 feed instead.

Source

pub fn with_source<R: RandomSource + 'static>(source: R) -> Self

Create a new environment driven by a custom RandomSource.

Use this to plug in an external feed — e.g. the portable SplitMix64 generator that the SimPy comparison harness re-implements in Python:

use simu::{SimEnv, rng::SplitMix64};
let env = SimEnv::with_source(SplitMix64::new(42));
Source

pub fn set_seed(&mut self, seed: u64)

Re-seed the environment’s randomness source, restarting its stream.

Takes &mut self for consistency with run — reseeding mid-run would change the draw stream, so it is an owner-level operation. Delegates to RandomSource::reseed; panics if the active source does not support reseeding.

Source

pub fn handle(&self) -> EnvHandle

Return a cloneable handle suitable for passing into spawned processes.

Source

pub fn now(&self) -> f64

Current simulation time.

Source

pub fn spawn<F>(&self, future: F) -> ProcessHandle<F::Output>
where F: Future + 'static, F::Output: 'static,

Spawn a process. The future is queued and will be polled on the next executor iteration. May be called before or during run().

Returns a ProcessHandle that resolves to the process’s output when it finishes. Drop the handle to detach (fire-and-forget).

Source

pub fn timeout(&self, delay: f64) -> Timeout

Create a Timeout that resolves after delay simulated time units.

§Panics

Panics if delay is negative or not finite — see EnvHandle::timeout.

Source

pub fn event(&self) -> (EventTrigger, EventAwaitable)

Create a paired (EventTrigger, EventAwaitable) for inter-process signalling.

Source

pub fn run(&mut self)

Run until the event queue is empty.

Source

pub fn run_until(&mut self, until: f64)

Run until simulated time reaches until.

When the event queue empties (or its next event is beyond until) the clock advances to until — but never backwards: simulated time is monotonic, so calling run_until with a boundary at or before the current time is a no-op that leaves now() unchanged. An event scheduled exactly at until is not run (its time is not strictly less than the boundary), yet now() will report until.

Trait Implementations§

Source§

impl Debug for SimEnv

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SimEnv

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for SimEnv

Source§

fn drop(&mut self)

Break the SimState ↔ process reference cycle on teardown.

A suspended process future captures an EnvHandle, which holds an Rc<RefCell<SimState>> — so SimState → processes → future → EnvHandle → SimState is a cycle. If a simulation ends with processes still suspended (e.g. one blocked forever on a resource that never frees), that cycle keeps the whole SimState alive and leaks it; across many replications the leak accumulates. Clearing the process tables drops those futures, releasing their handles so SimState can be reclaimed.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for SimEnv

§

impl !Send for SimEnv

§

impl !Sync for SimEnv

§

impl !UnwindSafe for SimEnv

§

impl Freeze for SimEnv

§

impl Unpin for SimEnv

§

impl UnsafeUnpin for SimEnv

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V