capacity/
lib.rs

1#![warn(
2    clippy::unwrap_used,
3    missing_docs,
4    rust_2018_idioms,
5    unused_lifetimes,
6    unused_qualifications
7)]
8#![allow(clippy::single_match, rustdoc::bare_urls)]
9#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
10#![doc = include_str!("../README.md")]
11
12use core::marker::PhantomData;
13#[cfg(feature = "bounded")]
14use core::{fmt, fmt::Display};
15
16/// Bounded Maximums & Minimums for each Kind
17#[cfg(feature = "bounded")]
18pub trait Bounded<Kind> {
19    /// Return the Maximum that can be set for the given Kind
20    fn maximum(&self, _: &Kind) -> usize;
21    /// Return the Minimum that can be set for the given Kind    
22    fn minimum(&self, _: &Kind) -> usize;
23}
24
25/// Base trait for setting the capacity
26pub trait Setting<Kind> {
27    /// Set the capacities for the given Kind
28    fn setting(&self, _: &Kind) -> usize;
29}
30
31/// Capacity Planner
32pub struct Capacity<H, Kind> {
33    holder: H,
34    _kind: PhantomData<Kind>,
35}
36
37/// Capacity indidator when capacity setting is bounded.
38#[derive(Clone, Debug, PartialEq)]
39#[cfg(feature = "bounded")]
40pub enum CapacityIndicator {
41    /// Over the maximum
42    OverMax(usize),
43    /// Under the minimum
44    UnderMin(usize),
45    /// Capacity Within Bounds
46    WithinBounds(usize),
47}
48
49#[cfg(feature = "bounded")]
50impl Display for CapacityIndicator {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::OverMax(max) => write!(f, "Over {}", max),
54            Self::UnderMin(min) => write!(f, "Under {}", min),
55            Self::WithinBounds(cap) => write!(f, "Capacity {}", cap),
56        }
57    }
58}
59
60impl<H, Kind> Capacity<H, Kind>
61where
62    H: Setting<Kind>,
63{
64    /// Create new capacity planning.
65    #[inline]
66    pub fn with_planned(holder: H) -> Self {
67        Self {
68            holder,
69            _kind: PhantomData,
70        }
71    }
72    /// Returns the current setting without bounds.
73    #[inline(always)]
74    pub fn of_unbounded(&self, of: &Kind) -> usize {
75        self.holder.setting(of)
76    }
77}
78
79#[cfg(feature = "bounded")]
80impl<H, Kind> Capacity<H, Kind>
81where
82    H: Setting<Kind> + Bounded<Kind>,
83{
84    /// Minimum set by Kind
85    #[inline(always)]
86    pub fn minimum(&self, of: &Kind) -> usize {
87        self.holder.minimum(of)
88    }
89    /// Maximum set by Kind    
90    #[inline(always)]
91    pub fn maximum(&self, of: &Kind) -> usize {
92        self.holder.maximum(of)
93    }
94    /// Returns the current setting and if within the set bounds.
95    #[inline(always)]
96    pub fn of_bounded(&self, of: &Kind) -> CapacityIndicator {
97        if self.holder.minimum(of) > self.holder.setting(of) {
98            return CapacityIndicator::UnderMin(self.holder.minimum(of));
99        }
100        if self.holder.maximum(of) < self.holder.setting(of) {
101            return CapacityIndicator::OverMax(self.holder.maximum(of));
102        }
103        CapacityIndicator::WithinBounds(self.holder.setting(of))
104    }
105}