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
impl SimEnv
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new environment seeded from OS entropy.
Use with_seed when reproducibility is required.
Sourcepub fn with_seed(seed: u64) -> Self
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.
Sourcepub fn with_source<R: RandomSource + 'static>(source: R) -> Self
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));Sourcepub fn set_seed(&mut self, seed: u64)
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.
Sourcepub fn handle(&self) -> EnvHandle
pub fn handle(&self) -> EnvHandle
Return a cloneable handle suitable for passing into spawned processes.
Sourcepub fn spawn<F>(&self, future: F) -> ProcessHandle<F::Output> ⓘ
pub fn spawn<F>(&self, future: F) -> ProcessHandle<F::Output> ⓘ
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).
Sourcepub fn timeout(&self, delay: f64) -> Timeout ⓘ
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.
Sourcepub fn event(&self) -> (EventTrigger, EventAwaitable)
pub fn event(&self) -> (EventTrigger, EventAwaitable)
Create a paired (EventTrigger, EventAwaitable) for inter-process signalling.
Sourcepub fn run_until(&mut self, until: f64)
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 Drop for SimEnv
impl Drop for SimEnv
Source§fn drop(&mut self)
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.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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