titan_api_types/common/
bounded.rs1#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7use std::fmt::Debug;
8
9pub struct BoundedValue<T> {
13 pub min: T,
15 pub max: T,
17}
18
19impl<T> BoundedValue<T> {
20 pub const fn new(min: T, max: T) -> Self {
22 Self { min, max }
23 }
24
25 #[inline]
27 pub fn contains<V>(&self, value: V) -> bool
28 where
29 V: PartialOrd<T>,
30 {
31 (value >= self.min) && (value <= self.max)
32 }
33}
34
35impl<T> Debug for BoundedValue<T>
37where
38 T: Debug,
39{
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 f.debug_struct("BoundedValue")
42 .field("min", &self.min)
43 .field("max", &self.max)
44 .finish()
45 }
46}
47
48impl<T> Default for BoundedValue<T>
50where
51 T: Default,
52{
53 fn default() -> Self {
54 Self {
55 min: Default::default(),
56 max: Default::default(),
57 }
58 }
59}
60
61impl<T> Clone for BoundedValue<T>
63where
64 T: Clone,
65{
66 fn clone(&self) -> Self {
67 Self {
68 min: self.min.clone(),
69 max: self.max.clone(),
70 }
71 }
72}
73
74impl<T> Copy for BoundedValue<T> where T: Copy {}
76
77impl<T> PartialEq for BoundedValue<T>
79where
80 T: PartialEq,
81{
82 fn eq(&self, other: &Self) -> bool {
83 (self.min == other.min) && (self.max == other.max)
84 }
85}
86
87impl<T> Eq for BoundedValue<T> where T: Eq {}
88
89#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
93pub struct BoundedValueWithDefault<T> {
94 pub min: T,
96 pub max: T,
98 pub default: T,
100}
101
102impl<T> BoundedValueWithDefault<T> {
103 pub const fn new(min: T, max: T, default: T) -> Self {
105 Self { min, max, default }
106 }
107
108 #[inline]
110 pub fn contains<V>(&self, value: V) -> bool
111 where
112 V: PartialOrd<T>,
113 {
114 (value >= self.min) && (value <= self.max)
115 }
116}
117
118impl<T> Debug for BoundedValueWithDefault<T>
120where
121 T: Debug,
122{
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 f.debug_struct("BoundedValueWithDefault")
125 .field("min", &self.min)
126 .field("max", &self.max)
127 .field("default", &self.default)
128 .finish()
129 }
130}
131
132impl<T> Default for BoundedValueWithDefault<T>
134where
135 T: Default,
136{
137 fn default() -> Self {
138 Self {
139 min: Default::default(),
140 max: Default::default(),
141 default: Default::default(),
142 }
143 }
144}
145
146impl<T> Clone for BoundedValueWithDefault<T>
148where
149 T: Clone,
150{
151 fn clone(&self) -> Self {
152 Self {
153 min: self.min.clone(),
154 max: self.max.clone(),
155 default: self.default.clone(),
156 }
157 }
158}
159
160impl<T> Copy for BoundedValueWithDefault<T> where T: Copy {}
162
163impl<T> PartialEq for BoundedValueWithDefault<T>
165where
166 T: PartialEq,
167{
168 fn eq(&self, other: &Self) -> bool {
169 (self.min == other.min) && (self.max == other.max) && (self.default == other.default)
170 }
171}
172
173impl<T> Eq for BoundedValueWithDefault<T> where T: Eq {}