Skip to main content

Container

Struct Container 

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

A cloneable handle to a continuous-quantity resource (e.g., a tank of liquid, a battery, an inventory of medication).

put(amount) adds material; get(amount) removes it. Both operations suspend the calling process when they cannot immediately complete:

  • get suspends when the current level is below the requested amount.
  • put suspends when adding the amount would exceed the container’s capacity.

Waiters are served in strict head-of-line FIFO within each queue: a freshly-arriving request never takes level/space ahead of an already-queued waiter, even when the current level would let it complete immediately. A blocked head-of-queue request therefore holds the line for everyone behind it (matching SimPy’s Container). All clones share the same internal state (cheap Rc clone). Container is !Send + !Sync, consistent with SimEnv.

A fuel tank: the car needs more than is in stock, so it waits for the tanker truck’s delivery:

use simu::{SimEnv, Container};

let mut env = SimEnv::with_seed(0);
let tank = Container::new(100.0, 20.0); // capacity 100, starts at 20

// A truck delivers 80 units at t = 5.
let h = env.handle();
let t = tank.clone();
env.spawn(async move {
    h.timeout(5.0).await;
    t.put(80.0).await; // fits (20 + 80 ≤ 100), resolves immediately
});

// A car wants 50 units — more than the current level, so it suspends.
let h2 = env.handle();
let t2 = tank.clone();
env.spawn(async move {
    t2.get(50.0).await; // woken by the delivery
    assert_eq!(h2.now(), 5.0);
});

env.run();
assert_eq!(tank.level(), 50.0); // 20 + 80 − 50

Implementations§

Source§

impl Container

Source

pub fn empty(capacity: f64) -> Self

Create an empty container with the given capacity.

§Panics

Panics if capacity <= 0.

Source

pub fn new(capacity: f64, initial_level: f64) -> Self

Create a container with the given capacity and initial level.

§Panics

Panics if capacity <= 0, initial_level < 0, or initial_level > capacity.

Source

pub fn level(&self) -> f64

Current level (amount of material present).

Source

pub fn capacity(&self) -> f64

Maximum capacity.

Source

pub fn get_queue_len(&self) -> usize

Number of consumers currently blocked in the get queue (waiting for enough material). Excludes abandoned (canceled) requests.

Source

pub fn put_queue_len(&self) -> usize

Number of producers currently blocked in the put queue (waiting for enough free space). Excludes abandoned (canceled) requests.

Source

pub fn put(&self, amount: f64) -> ContainerPutRequest

Add amount to the container.

Resolves immediately if level + amount <= capacity; otherwise suspends until enough space is available.

§Panics

Panics if amount <= 0, or if amount > capacity — the latter could never complete and, under strict head-of-line FIFO, would block every later waiter behind it, so it is treated as a programming error. (SimPy blocks forever here instead; diverging is deliberate.)

Source

pub fn get(&self, amount: f64) -> ContainerGetRequest

Remove amount from the container.

Resolves immediately if level >= amount; otherwise suspends until enough material is available.

§Panics

Panics if amount <= 0, or if amount > capacity — the latter could never complete and, under strict head-of-line FIFO, would block every later waiter behind it, so it is treated as a programming error. (SimPy blocks forever here instead; diverging is deliberate.)

Trait Implementations§

Source§

impl Clone for Container

Source§

fn clone(&self) -> Container

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Container

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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