pub struct Resource { /* private fields */ }Expand description
A cloneable handle to a capacity-limited resource pool.
Units are acquired by calling request and awaiting
the returned future. If no unit is available the calling process is
suspended and woken in FIFO order when one becomes free.
Resource wraps an Rc<RefCell<>> internally, so cloning is cheap and
all clones share the same pool. It is !Send + !Sync — consistent with
SimEnv.
Two processes sharing a single-unit pump — the second queues until the first one’s guard drops:
use simu::{SimEnv, Resource};
let mut env = SimEnv::with_seed(0);
let pump = Resource::new(1);
for _ in 0..2 {
let h = env.handle();
let p = pump.clone(); // same pool — share by cloning, never Arc
env.spawn(async move {
let _guard = p.request().await; // second process suspends here
h.timeout(3.0).await; // use the pump for 3 time units
}); // guard drops → pump handed to the next waiter
}
env.run();
assert_eq!(env.now(), 6.0); // servings ran back-to-back, not in parallelFIFO ordering is the degenerate WaitQueue<()> case: every waiter shares
the same (unit) key, so they are served purely in insertion order.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Resource
impl !Send for Resource
impl !Sync for Resource
impl !UnwindSafe for Resource
impl Freeze for Resource
impl Unpin for Resource
impl UnsafeUnpin for Resource
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> ⓘ
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 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> ⓘ
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