Skip to main content

dvm_api/
capacity.rs

1use serde::{Deserialize, Serialize};
2
3/// CPUs and memory: what a runner hands out, and what a run takes of it
4/// ([`SandboxSpec::footprint`](crate::SandboxSpec::footprint)).
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
6#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
7#[cfg_attr(feature = "typescript", ts(export_to = "controller-api.ts"))]
8pub struct Capacity {
9    pub cpus: u32,
10    pub memory_mib: u64,
11}
12
13impl Capacity {
14    /// What's left once `footprint` is taken out, if it fits.
15    pub fn without(self, footprint: Capacity) -> Option<Capacity> {
16        Some(Capacity {
17            cpus: self.cpus.checked_sub(footprint.cpus)?,
18            memory_mib: self.memory_mib.checked_sub(footprint.memory_mib)?,
19        })
20    }
21
22    /// This, with `footprint` given back.
23    pub fn with(self, footprint: Capacity) -> Capacity {
24        Capacity {
25            cpus: self.cpus.saturating_add(footprint.cpus),
26            memory_mib: self.memory_mib.saturating_add(footprint.memory_mib),
27        }
28    }
29}