ospf_rust_math/algebra/value_range/
bound.rs

1use std::ops::{Add, Div, Mul, Neg, Sub};
2use std::time::{Duration, Instant};
3
4use chrono::NaiveDateTime;
5
6use super::interval::Interval;
7use super::value_wrapper::ValueWrapper;
8use super::error::IllegalArgumentError;
9
10#[non_exhaustive]
11#[derive(Clone, Copy)]
12pub struct Bound<T> {
13    pub value: ValueWrapper<T>,
14    pub interval: Interval,
15}
16
17impl<T> Bound<T> {
18    pub fn new(value: ValueWrapper<T>, interval: Interval) -> Self {
19        match value {
20            ValueWrapper::Value(_) => Self { value, interval },
21            _ => Self {
22                value,
23                interval: Interval::Open,
24            },
25        }
26    }
27}
28
29impl<T, U> From<&Bound<U>> for Bound<T>
30where
31    ValueWrapper<T>: for<'a> From<&'a ValueWrapper<U>>,
32{
33    fn from(bound: &Bound<U>) -> Self {
34        Self {
35            value: ValueWrapper::from(&bound.value),
36            interval: bound.interval,
37        }
38    }
39}
40
41impl<T, U> PartialEq<U> for Bound<T>
42where
43    ValueWrapper<T>: PartialEq<U>,
44{
45    fn eq(&self, other: &U) -> bool {
46        self.value.eq(other) && self.interval == Interval::Closed
47    }
48}
49
50impl<T, U> PartialEq<Bound<U>> for Bound<T>
51where
52    ValueWrapper<T>: PartialEq<ValueWrapper<U>>,
53{
54    fn eq(&self, other: &Bound<U>) -> bool {
55        self.value.eq(&other.value) && self.interval == other.interval
56    }
57}
58
59macro_rules! bound_template {
60    ($type:ident, $rhs:ident) => {
61        impl Add<$rhs> for Bound<$type> {
62            type Output = Result<Bound<<$type as Add<$rhs>>::Output>, IllegalArgumentError>;
63
64            fn add(self, rhs: $rhs) -> Self::Output {
65                let value = (self.value + rhs)?;
66                Ok(Bound::new(value, self.interval))
67            }
68        }
69
70        impl<'a> Add<&'a $rhs> for Bound<$type> {
71            type Output = Result<Bound<<$type as Add<$rhs>>::Output>, IllegalArgumentError>;
72
73            fn add(self, rhs: &'a $rhs) -> Self::Output {
74                let value = self.value.add(rhs)?;
75                Ok(Bound::new(value, self.interval))
76            }
77        }
78
79        impl<'a> Add<$rhs> for &'a Bound<$type> {
80            type Output = Result<Bound<<$type as Add<$rhs>>::Output>, IllegalArgumentError>;
81
82            fn add(self, rhs: $rhs) -> Self::Output {
83                let value = self.value.add(rhs)?;
84                Ok(Bound::new(value, self.interval))
85            }
86        }
87
88        impl<'a, 'b> Add<&'a $rhs> for &'b Bound<$type> {
89            type Output = Result<Bound<<$type as Add<$rhs>>::Output>, IllegalArgumentError>;
90
91            fn add(self, rhs: &'a $rhs) -> Self::Output {
92                let value = self.value.add(rhs)?;
93                Ok(Bound::new(value, self.interval))
94            }
95        }
96
97        impl Sub<$rhs> for Bound<$type> {
98            type Output = Result<Bound<<$type as Sub<$rhs>>::Output>, IllegalArgumentError>;
99
100            fn sub(self, rhs: $rhs) -> Self::Output {
101                let value = self.value.sub(rhs)?;
102                Ok(Bound::new(value, self.interval))
103            }
104        }
105
106        impl<'a> Sub<&'a $rhs> for Bound<$type> {
107            type Output = Result<Bound<<$type as Sub<$rhs>>::Output>, IllegalArgumentError>;
108
109            fn sub(self, rhs: &'a $rhs) -> Self::Output {
110                let value = self.value.sub(rhs)?;
111                Ok(Bound::new(value, self.interval))
112            }
113        }
114
115        impl<'a> Sub<$rhs> for &'a Bound<$type> {
116            type Output = Result<Bound<<$type as Sub<$rhs>>::Output>, IllegalArgumentError>;
117
118            fn sub(self, rhs: $rhs) -> Self::Output {
119                let value = self.value.sub(rhs)?;
120                Ok(Bound::new(value, self.interval))
121            }
122        }
123
124        impl<'a, 'b> Sub<&'a $rhs> for &'b Bound<$type> {
125            type Output = Result<Bound<<$type as Sub<$rhs>>::Output>, IllegalArgumentError>;
126
127            fn sub(self, rhs: &'a $rhs) -> Self::Output {
128                let value = self.value.sub(rhs)?;
129                Ok(Bound::new(value, self.interval))
130            }
131        }
132    };
133}
134bound_template!(Instant, Duration);
135bound_template!(NaiveDateTime, Duration);
136bound_template!(Duration, Duration);
137
138macro_rules! signed_bound_template {
139    ($($type:ident)*) => ($(
140        impl Neg for Bound<$type> {
141            type Output = Result<Bound<<$type as Neg>::Output>, IllegalArgumentError>;
142
143            fn neg(self) -> Self::Output {
144                let value = self.value.neg()?;
145                Ok(Bound::new(value, self.interval))
146            }
147        }
148
149        impl<'a> Neg for &'a Bound<$type> {
150            type Output = Result<Bound<<$type as Neg>::Output>, IllegalArgumentError>;
151
152            fn neg(self) -> Self::Output {
153                let value = self.value.neg()?;
154                Ok(Bound::new(value, self.interval))
155            }
156        }
157    )*)
158}
159signed_bound_template! { i8 i16 i32 i64 i128 isize f32 f64 }
160
161macro_rules! real_number_bound_template {
162    ($($type:ident)*) => ($(
163        impl Add<$type> for Bound<$type> {
164            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
165
166            fn add(self, rhs: $type) -> Self::Output {
167                let value = self.value.add(rhs)?;
168                Ok(Bound::new(value, self.interval))
169            }
170        }
171
172        impl<'a> Add<&'a $type> for Bound<$type> {
173            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
174
175            fn add(self, rhs: &'a $type) -> Self::Output {
176                let value = self.value.add(rhs)?;
177                Ok(Bound::new(value, self.interval))
178            }
179        }
180
181        impl<'a> Add<$type> for &'a Bound<$type> {
182            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
183
184            fn add(self, rhs: $type) -> Self::Output {
185                let value = self.value.add(rhs)?;
186                Ok(Bound::new(value, self.interval))
187            }
188        }
189
190        impl<'a, 'b> Add<&'a $type> for &'b Bound<$type> {
191            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
192
193            fn add(self, rhs: &'a $type) -> Self::Output {
194                let value = self.value.add(rhs)?;
195                Ok(Bound::new(value, self.interval))
196            }
197        }
198
199        impl Add<Bound<$type>> for $type {
200            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
201
202            fn add(self, rhs: Bound<$type>) -> Self::Output {
203                let value = self.add(rhs.value)?;
204                Ok(Bound::new(value, rhs.interval))
205            }
206        }
207
208        impl<'a> Add<Bound<$type>> for &'a $type {
209            type Output = Result<Bound<<&'a $type as Add<$type>>::Output>, IllegalArgumentError>;
210
211            fn add(self, rhs: Bound<$type>) -> Self::Output {
212                let value = self.add(rhs.value)?;
213                Ok(Bound::new(value, rhs.interval))
214            }
215        }
216
217        impl<'a> Add<&'a Bound<$type>> for $type {
218            type Output = Result<Bound<<$type as Add<&'a $type>>::Output>, IllegalArgumentError>;
219
220            fn add(self, rhs: &'a Bound<$type>) -> Self::Output {
221                let value = self.add(rhs.value)?;
222                Ok(Bound::new(value, rhs.interval))
223            }
224        }
225
226        impl<'a, 'b> Add<&'b Bound<$type>> for &'a $type {
227            type Output = Result<Bound<<&'a $type as Add<&'b $type>>::Output>, IllegalArgumentError>;
228
229            fn add(self, rhs: &'b Bound<$type>) -> Self::Output {
230                let value = self.add(rhs.value)?;
231                Ok(Bound::new(value, rhs.interval))
232            }
233        }
234
235        impl Add<Bound<$type>> for Bound<$type> {
236            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
237
238            fn add(self, rhs: Bound<$type>) -> Self::Output {
239                let value = self.value.add(rhs.value)?;
240                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
241            }
242        }
243
244        impl<'a> Add<&'a Bound<$type>> for Bound<$type> {
245            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
246
247            fn add(self, rhs: &'a Bound<$type>) -> Self::Output {
248                let value = self.value.add(rhs.value)?;
249                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
250            }
251        }
252
253        impl<'a> Add<Bound<$type>> for &'a Bound<$type> {
254            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
255
256            fn add(self, rhs: Bound<$type>) -> Self::Output {
257                let value = self.value.add(rhs.value)?;
258                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
259            }
260        }
261
262        impl<'a, 'b> Add<&'a Bound<$type>> for &'b Bound<$type> {
263            type Output = Result<Bound<<$type as Add<$type>>::Output>, IllegalArgumentError>;
264
265            fn add(self, rhs: &'a Bound<$type>) -> Self::Output {
266                let value = self.value.add(rhs.value)?;
267                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
268            }
269        }
270
271        impl Sub<$type> for Bound<$type> {
272            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
273
274            fn sub(self, rhs: $type) -> Self::Output {
275                let value = self.value.sub(rhs)?;
276                Ok(Bound::new(value, self.interval))
277            }
278        }
279
280        impl<'a> Sub<&'a $type> for Bound<$type> {
281            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
282
283            fn sub(self, rhs: &'a $type) -> Self::Output {
284                let value = self.value.sub(rhs)?;
285                Ok(Bound::new(value, self.interval))
286            }
287        }
288
289        impl<'a> Sub<$type> for &'a Bound<$type> {
290            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
291
292            fn sub(self, rhs: $type) -> Self::Output {
293                let value = self.value.sub(rhs)?;
294                Ok(Bound::new(value, self.interval))
295            }
296        }
297
298        impl<'a, 'b> Sub<&'a $type> for &'b Bound<$type> {
299            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
300
301            fn sub(self, rhs: &'a $type) -> Self::Output {
302                let value = self.value.sub(rhs)?;
303                Ok(Bound::new(value, self.interval))
304            }
305        }
306
307        impl Sub<Bound<$type>> for $type {
308            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
309
310            fn sub(self, rhs: Bound<$type>) -> Self::Output {
311                let value = self.sub(rhs.value)?;
312                Ok(Bound::new(value, rhs.interval))
313            }
314        }
315
316        impl<'a> Sub<Bound<$type>> for &'a $type {
317            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
318
319            fn sub(self, rhs: Bound<$type>) -> Self::Output {
320                let value = self.sub(rhs.value)?;
321                Ok(Bound::new(value, rhs.interval))
322            }
323        }
324
325        impl<'a> Sub<&'a Bound<$type>> for $type {
326            type Output = Result<Bound<<$type as Sub<&'a $type>>::Output>, IllegalArgumentError>;
327
328            fn sub(self, rhs: &'a Bound<$type>) -> Self::Output {
329                let value = self.sub(rhs.value)?;
330                Ok(Bound::new(value, rhs.interval))
331            }
332        }
333
334        impl<'a, 'b> Sub<&'b Bound<$type>> for &'a $type {
335            type Output = Result<Bound<<$type as Sub<&'b $type>>::Output>, IllegalArgumentError>;
336
337            fn sub(self, rhs: &'b Bound<$type>) -> Self::Output {
338                let value = self.sub(rhs.value)?;
339                Ok(Bound::new(value, rhs.interval))
340            }
341        }
342
343        impl Sub<Bound<$type>> for Bound<$type> {
344            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
345
346            fn sub(self, rhs: Bound<$type>) -> Self::Output {
347                let value = self.value.sub(rhs.value)?;
348                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
349            }
350        }
351
352        impl<'a> Sub<&'a Bound<$type>> for Bound<$type> {
353            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
354
355            fn sub(self, rhs: &'a Bound<$type>) -> Self::Output {
356                let value = self.value.sub(rhs.value)?;
357                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
358            }
359        }
360
361        impl<'a> Sub<Bound<$type>> for &'a Bound<$type> {
362            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
363
364            fn sub(self, rhs: Bound<$type>) -> Self::Output {
365                let value = self.value.sub(rhs.value)?;
366                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
367            }
368        }
369
370        impl<'a, 'b> Sub<&'a Bound<$type>> for &'b Bound<$type> {
371            type Output = Result<Bound<<$type as Sub<$type>>::Output>, IllegalArgumentError>;
372
373            fn sub(self, rhs: &'a Bound<$type>) -> Self::Output {
374                let value = self.value.sub(rhs.value)?;
375                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
376            }
377        }
378
379        impl Mul<$type> for Bound<$type> {
380            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
381
382            fn mul(self, rhs: $type) -> Self::Output {
383                let value = self.value.mul(rhs)?;
384                Ok(Bound::new(value, self.interval))
385            }
386        }
387
388        impl<'a> Mul<&'a $type> for Bound<$type> {
389            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
390
391            fn mul(self, rhs: &'a $type) -> Self::Output {
392                let value = self.value.mul(rhs)?;
393                Ok(Bound::new(value, self.interval))
394            }
395        }
396
397        impl<'a> Mul<$type> for &'a Bound<$type> {
398            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
399
400            fn mul(self, rhs: $type) -> Self::Output {
401                let value = self.value.mul(rhs)?;
402                Ok(Bound::new(value, self.interval))
403            }
404        }
405
406        impl<'a, 'b> Mul<&'a $type> for &'b Bound<$type> {
407            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
408
409            fn mul(self, rhs: &'a $type) -> Self::Output {
410                let value = self.value.mul(rhs)?;
411                Ok(Bound::new(value, self.interval))
412            }
413        }
414
415        impl Mul<Bound<$type>> for $type {
416            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
417
418            fn mul(self, rhs: Bound<$type>) -> Self::Output {
419                let value = self.mul(rhs.value)?;
420                Ok(Bound::new(value, rhs.interval))
421            }
422        }
423
424        impl<'a> Mul<Bound<$type>> for &'a $type {
425            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
426
427            fn mul(self, rhs: Bound<$type>) -> Self::Output {
428                let value = self.mul(rhs.value)?;
429                Ok(Bound::new(value, rhs.interval))
430            }
431        }
432
433        impl<'a> Mul<&'a Bound<$type>> for $type {
434            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
435
436            fn mul(self, rhs: &'a Bound<$type>) -> Self::Output {
437                let value = self.mul(rhs.value)?;
438                Ok(Bound::new(value, rhs.interval))
439            }
440        }
441
442        impl<'a, 'b> Mul<&'b Bound<$type>> for &'a $type {
443            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
444
445            fn mul(self, rhs: &'b Bound<$type>) -> Self::Output {
446                let value = self.mul(rhs.value)?;
447                Ok(Bound::new(value, rhs.interval))
448            }
449        }
450
451        impl Mul<Bound<$type>> for Bound<$type> {
452            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
453
454            fn mul(self, rhs: Bound<$type>) -> Self::Output {
455                let value = self.value.mul(rhs.value)?;
456                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
457            }
458        }
459
460        impl<'a> Mul<&'a Bound<$type>> for Bound<$type> {
461            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
462
463            fn mul(self, rhs: &'a Bound<$type>) -> Self::Output {
464                let value = self.value.mul(rhs.value)?;
465                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
466            }
467        }
468
469        impl<'a> Mul<Bound<$type>> for &'a Bound<$type> {
470            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
471
472            fn mul(self, rhs: Bound<$type>) -> Self::Output {
473                let value = self.value.mul(rhs.value)?;
474                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
475            }
476        }
477
478        impl<'a, 'b> Mul<&'a Bound<$type>> for &'b Bound<$type> {
479            type Output = Result<Bound<<$type as Mul<$type>>::Output>, IllegalArgumentError>;
480
481            fn mul(self, rhs: &'a Bound<$type>) -> Self::Output {
482                let value = self.value.mul(rhs.value)?;
483                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
484            }
485        }
486
487        impl Div<$type> for Bound<$type> {
488            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
489
490            fn div(self, rhs: $type) -> Self::Output {
491                let value = self.value.div(rhs)?;
492                Ok(Bound::new(value, self.interval))
493            }
494        }
495
496        impl<'a> Div<&'a $type> for Bound<$type> {
497            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
498
499            fn div(self, rhs: &'a $type) -> Self::Output {
500                let value = self.value.div(rhs)?;
501                Ok(Bound::new(value, self.interval))
502            }
503        }
504
505        impl<'a> Div<$type> for &'a Bound<$type> {
506            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
507
508            fn div(self, rhs: $type) -> Self::Output {
509                let value = self.value.div(rhs)?;
510                Ok(Bound::new(value, self.interval))
511            }
512        }
513
514        impl<'a, 'b> Div<&'a $type> for &'b Bound<$type> {
515            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
516
517            fn div(self, rhs: &'a $type) -> Self::Output {
518                let value = self.value.div(rhs)?;
519                Ok(Bound::new(value, self.interval))
520            }
521        }
522
523        impl Div<Bound<$type>> for $type {
524            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
525
526            fn div(self, rhs: Bound<$type>) -> Self::Output {
527                let value = (self / rhs.value)?;
528                Ok(Bound::new(value, rhs.interval))
529            }
530        }
531
532        impl<'a> Div<Bound<$type>> for &'a $type {
533            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
534
535            fn div(self, rhs: Bound<$type>) -> Self::Output {
536                let value = (self / rhs.value)?;
537                Ok(Bound::new(value, rhs.interval))
538            }
539        }
540
541        impl<'a> Div<&'a Bound<$type>> for $type {
542            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
543
544            fn div(self, rhs: &'a Bound<$type>) -> Self::Output {
545                let value = (self / rhs.value)?;
546                Ok(Bound::new(value, rhs.interval))
547            }
548        }
549
550        impl<'a, 'b> Div<&'b Bound<$type>> for &'a $type {
551            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
552
553            fn div(self, rhs: &'b Bound<$type>) -> Self::Output {
554                let value = (self / rhs.value)?;
555                Ok(Bound::new(value, rhs.interval))
556            }
557        }
558
559        impl Div<Bound<$type>> for Bound<$type> {
560            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
561
562            fn div(self, rhs: Bound<$type>) -> Self::Output {
563                let value = (self.value / rhs.value)?;
564                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
565            }
566        }
567
568        impl<'a> Div<&'a Bound<$type>> for Bound<$type> {
569            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
570
571            fn div(self, rhs: &'a Bound<$type>) -> Self::Output {
572                let value = (self.value / rhs.value)?;
573                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
574            }
575        }
576
577        impl<'a> Div<Bound<$type>> for &'a Bound<$type> {
578            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
579
580            fn div(self, rhs: Bound<$type>) -> Self::Output {
581                let value = (self.value / rhs.value)?;
582                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
583            }
584        }
585
586        impl<'a, 'b> Div<&'a Bound<$type>> for &'b Bound<$type> {
587            type Output = Result<Bound<<$type as Div<$type>>::Output>, IllegalArgumentError>;
588
589            fn div(self, rhs: &'a Bound<$type>) -> Self::Output {
590                let value = (self.value / rhs.value)?;
591                Ok(Bound::new(value, self.interval.intersect(&rhs.interval)))
592            }
593        }
594    )*)
595}
596real_number_bound_template! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }