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#[cfg(feature = "bounded")]
18pub trait Bounded<Kind> {
19 fn maximum(&self, _: &Kind) -> usize;
21 fn minimum(&self, _: &Kind) -> usize;
23}
24
25pub trait Setting<Kind> {
27 fn setting(&self, _: &Kind) -> usize;
29}
30
31pub struct Capacity<H, Kind> {
33 holder: H,
34 _kind: PhantomData<Kind>,
35}
36
37#[derive(Clone, Debug, PartialEq)]
39#[cfg(feature = "bounded")]
40pub enum CapacityIndicator {
41 OverMax(usize),
43 UnderMin(usize),
45 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 #[inline]
66 pub fn with_planned(holder: H) -> Self {
67 Self {
68 holder,
69 _kind: PhantomData,
70 }
71 }
72 #[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 #[inline(always)]
86 pub fn minimum(&self, of: &Kind) -> usize {
87 self.holder.minimum(of)
88 }
89 #[inline(always)]
91 pub fn maximum(&self, of: &Kind) -> usize {
92 self.holder.maximum(of)
93 }
94 #[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}