#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
pub record: usize,
pub payload: usize,
pub readings: usize,
pub depth: usize,
pub object_members: usize,
pub entries: usize,
}
impl Limits {
pub const HARD_DEPTH: usize = 512;
pub const DEFAULT: Self = Self {
record: 128 * 1024,
payload: 64 * 1024,
readings: 4096,
depth: 8,
object_members: 256,
entries: 4096,
};
pub const UNLIMITED: Self = Self {
record: usize::MAX,
payload: usize::MAX,
readings: usize::MAX,
depth: usize::MAX,
object_members: usize::MAX,
entries: usize::MAX,
};
#[must_use]
pub const fn record(mut self, bytes: usize) -> Self {
self.record = bytes;
self
}
#[must_use]
pub const fn payload(mut self, bytes: usize) -> Self {
self.payload = bytes;
self
}
#[must_use]
pub const fn readings(mut self, count: usize) -> Self {
self.readings = count;
self
}
#[must_use]
pub const fn depth(mut self, depth: usize) -> Self {
self.depth = depth;
self
}
#[must_use]
pub const fn object_members(mut self, count: usize) -> Self {
self.object_members = count;
self
}
#[must_use]
pub const fn entries(mut self, count: usize) -> Self {
self.entries = count;
self
}
}
const _: () = {
assert!(Limits::DEFAULT.entries >= 100 * 8);
assert!(Limits::DEFAULT.payload > 932 * 8);
assert!(Limits::DEFAULT.readings > 6 * 100);
assert!(Limits::DEFAULT.depth >= 4);
assert!(Limits::DEFAULT.depth <= Limits::HARD_DEPTH);
assert!(Limits::DEFAULT.record >= Limits::DEFAULT.payload);
};
impl Default for Limits {
fn default() -> Self {
Self::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::Limits;
#[test]
fn a_caller_can_adjust_one_bound_without_naming_the_rest() {
let l = Limits::DEFAULT.payload(4096).readings(16);
assert_eq!(l.payload, 4096);
assert_eq!(l.readings, 16);
assert_eq!(l.record, Limits::DEFAULT.record, "the rest is untouched");
}
}