1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] #![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] #![allow(recursion_depth_exceeding_limit)] #![warn(unnameable_types)]
54
55use derive_more::{Add, Display, Div, From, FromStr, Mul};
56
57use serde::{Deserialize, Serialize};
58use std::time::Duration;
59use thiserror::Error;
60
61#[cfg(feature = "memquota-memcost")]
62use {derive_deftly::Deftly, tor_memquota::derive_deftly_template_HasMemoryCost};
63
64#[derive(Debug, Clone, PartialEq, Eq, Error)]
66#[non_exhaustive]
67pub enum Error {
68 #[error("Value {0} was below the lower bound {1} for this type")]
70 BelowLowerBound(i32, i32),
71 #[error("Value {0} was above the lower bound {1} for this type")]
73 AboveUpperBound(i32, i32),
74 #[error("Tried to convert a negative value to an unsigned type")]
76 Negative,
77 #[error("Value could not be represented as an i32")]
80 Unrepresentable,
81 #[error("Integer overflow")]
83 Overflow,
84}
85
86#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
103#[cfg_attr(
104 feature = "memquota-memcost",
105 derive(Deftly),
106 derive_deftly(HasMemoryCost)
107)]
108pub struct BoundedInt32<const LOWER: i32, const UPPER: i32> {
109 value: i32,
111}
112
113impl<const LOWER: i32, const UPPER: i32> BoundedInt32<LOWER, UPPER> {
114 pub const LOWER: i32 = LOWER;
116 pub const UPPER: i32 = UPPER;
118
119 fn unchecked_new(value: i32) -> Self {
121 const { assert!(LOWER <= UPPER) };
124
125 BoundedInt32 { value }
126 }
127
128 pub const fn lower(&self) -> i32 {
132 LOWER
133 }
134
135 pub const fn upper(&self) -> i32 {
139 UPPER
140 }
141
142 pub fn get(&self) -> i32 {
147 self.value
148 }
149
150 pub fn get_u32(&self) -> u32 {
157 const { assert!(LOWER >= 0) };
158 self.value as u32
159 }
160
161 pub fn saturating_new(val: i32) -> Self {
165 Self::unchecked_new(Self::clamp(val))
166 }
167
168 pub fn checked_new(val: i32) -> Result<Self, Error> {
171 if val > UPPER {
172 Err(Error::AboveUpperBound(val, UPPER))
173 } else if val < LOWER {
174 Err(Error::BelowLowerBound(val, LOWER))
175 } else {
176 Ok(BoundedInt32::unchecked_new(val))
177 }
178 }
179
180 fn clamp(val: i32) -> i32 {
182 Ord::clamp(val, LOWER, UPPER)
183 }
184
185 pub fn saturating_from(val: i32) -> Self {
192 Self::unchecked_new(Self::clamp(val))
193 }
194
195 pub fn saturating_from_str(s: &str) -> Result<Self, Error> {
202 let val: i32 = s.parse().map_err(|_| Error::Unrepresentable)?;
203 Ok(Self::saturating_from(val))
204 }
205}
206
207impl<const L: i32, const U: i32> std::fmt::Display for BoundedInt32<L, U> {
208 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
209 write!(f, "{}", self.value)
210 }
211}
212
213impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for i32 {
214 fn from(val: BoundedInt32<L, U>) -> i32 {
215 val.value
216 }
217}
218
219impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for f64 {
220 fn from(val: BoundedInt32<L, U>) -> f64 {
221 val.value.into()
222 }
223}
224
225impl<const L: i32, const H: i32> TryFrom<i32> for BoundedInt32<L, H> {
226 type Error = Error;
227 fn try_from(val: i32) -> Result<Self, Self::Error> {
228 Self::checked_new(val)
229 }
230}
231
232impl<const L: i32, const H: i32> std::str::FromStr for BoundedInt32<L, H> {
233 type Err = Error;
234 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
235 Self::checked_new(s.parse().map_err(|_| Error::Unrepresentable)?)
236 }
237}
238
239impl From<BoundedInt32<0, 1>> for bool {
240 fn from(val: BoundedInt32<0, 1>) -> bool {
241 val.value == 1
242 }
243}
244
245impl From<BoundedInt32<0, 255>> for u8 {
246 fn from(val: BoundedInt32<0, 255>) -> u8 {
247 val.value as u8
248 }
249}
250
251impl From<BoundedInt32<1, 254>> for u8 {
252 fn from(val: BoundedInt32<1, 254>) -> u8 {
253 val.value as u8
254 }
255}
256
257impl<const L: i32, const H: i32> From<BoundedInt32<L, H>> for u32 {
261 fn from(val: BoundedInt32<L, H>) -> u32 {
262 val.get_u32()
263 }
264}
265
266impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for u64 {
267 type Error = Error;
268 fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
269 if val.value < 0 {
270 Err(Error::Negative)
271 } else {
272 Ok(val.value as u64)
273 }
274 }
275}
276
277impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for usize {
278 type Error = Error;
279 fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
280 if val.value < 0 {
281 Err(Error::Negative)
282 } else {
283 Ok(val.value as usize)
284 }
285 }
286}
287
288#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
293pub struct Percentage<T: Copy + Into<f64>> {
294 value: T,
296}
297
298impl<T: Copy + Into<f64>> Percentage<T> {
299 pub fn new(value: T) -> Self {
301 Self { value }
302 }
303
304 pub fn as_fraction(self) -> f64 {
318 self.value.into() / 100.0
319 }
320
321 pub fn as_percent(self) -> T {
334 self.value
335 }
336}
337
338impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>> {
339 type Error = Error;
340 fn try_from(v: i32) -> Result<Self, Error> {
341 Ok(Percentage::new(v.try_into()?))
342 }
343}
344
345#[derive(
349 Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
350)]
351pub struct IntegerMilliseconds<T> {
355 value: T,
357}
358
359impl<T> IntegerMilliseconds<T> {
360 pub fn new(value: T) -> Self {
362 IntegerMilliseconds { value }
363 }
364
365 pub fn as_millis(self) -> T {
370 self.value
371 }
372
373 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMilliseconds<U>, E>
385 where
386 F: FnOnce(T) -> Result<U, E>,
387 {
388 Ok(IntegerMilliseconds::new(f(self.value)?))
389 }
390}
391
392impl<T: TryInto<u64>> TryFrom<IntegerMilliseconds<T>> for Duration {
393 type Error = <T as TryInto<u64>>::Error;
394 fn try_from(val: IntegerMilliseconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
395 Ok(Self::from_millis(val.value.try_into()?))
396 }
397}
398
399impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMilliseconds<BoundedInt32<H, L>> {
400 type Error = Error;
401 fn try_from(v: i32) -> Result<Self, Error> {
402 Ok(IntegerMilliseconds::new(v.try_into()?))
403 }
404}
405
406#[derive(
407 Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
408)]
409pub struct IntegerSeconds<T> {
413 value: T,
415}
416
417impl<T> IntegerSeconds<T> {
418 pub fn new(value: T) -> Self {
420 IntegerSeconds { value }
421 }
422
423 pub fn as_secs(self) -> T {
428 self.value
429 }
430
431 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerSeconds<U>, E>
441 where
442 F: FnOnce(T) -> Result<U, E>,
443 {
444 Ok(IntegerSeconds::new(f(self.value)?))
445 }
446}
447
448impl<T: TryInto<u64>> TryFrom<IntegerSeconds<T>> for Duration {
449 type Error = <T as TryInto<u64>>::Error;
450 fn try_from(val: IntegerSeconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
451 Ok(Self::from_secs(val.value.try_into()?))
452 }
453}
454
455impl<const H: i32, const L: i32> TryFrom<i32> for IntegerSeconds<BoundedInt32<H, L>> {
456 type Error = Error;
457 fn try_from(v: i32) -> Result<Self, Error> {
458 Ok(IntegerSeconds::new(v.try_into()?))
459 }
460}
461
462#[derive(Deserialize, Serialize)] #[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
464pub struct IntegerMinutes<T> {
468 value: T,
470}
471
472impl<T> IntegerMinutes<T> {
473 pub fn new(value: T) -> Self {
475 IntegerMinutes { value }
476 }
477
478 pub fn as_minutes(self) -> T {
483 self.value
484 }
485
486 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMinutes<U>, E>
496 where
497 F: FnOnce(T) -> Result<U, E>,
498 {
499 Ok(IntegerMinutes::new(f(self.value)?))
500 }
501}
502
503impl<T: TryInto<u64>> TryFrom<IntegerMinutes<T>> for Duration {
504 type Error = Error;
505 fn try_from(val: IntegerMinutes<T>) -> Result<Self, Error> {
506 const SECONDS_PER_MINUTE: u64 = 60;
508 let minutes: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
509 let seconds = minutes
510 .checked_mul(SECONDS_PER_MINUTE)
511 .ok_or(Error::Overflow)?;
512 Ok(Self::from_secs(seconds))
513 }
514}
515
516impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMinutes<BoundedInt32<H, L>> {
517 type Error = Error;
518 fn try_from(v: i32) -> Result<Self, Error> {
519 Ok(IntegerMinutes::new(v.try_into()?))
520 }
521}
522
523#[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
524pub struct IntegerDays<T> {
528 value: T,
530}
531
532impl<T> IntegerDays<T> {
533 pub fn new(value: T) -> Self {
535 IntegerDays { value }
536 }
537
538 pub fn as_days(self) -> T {
543 self.value
544 }
545
546 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerDays<U>, E>
556 where
557 F: FnOnce(T) -> Result<U, E>,
558 {
559 Ok(IntegerDays::new(f(self.value)?))
560 }
561}
562
563impl<T: TryInto<u64>> TryFrom<IntegerDays<T>> for Duration {
564 type Error = Error;
565 fn try_from(val: IntegerDays<T>) -> Result<Self, Error> {
566 const SECONDS_PER_DAY: u64 = 86400;
568 let days: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
569 let seconds = days.checked_mul(SECONDS_PER_DAY).ok_or(Error::Overflow)?;
570 Ok(Self::from_secs(seconds))
571 }
572}
573
574impl<const H: i32, const L: i32> TryFrom<i32> for IntegerDays<BoundedInt32<H, L>> {
575 type Error = Error;
576 fn try_from(v: i32) -> Result<Self, Error> {
577 Ok(IntegerDays::new(v.try_into()?))
578 }
579}
580
581#[derive(Clone, Copy, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
585pub struct SendMeVersion(u8);
586
587impl SendMeVersion {
588 pub fn new(value: u8) -> Self {
590 SendMeVersion(value)
591 }
592
593 pub fn get(&self) -> u8 {
595 self.0
596 }
597}
598
599impl TryFrom<i32> for SendMeVersion {
600 type Error = Error;
601 fn try_from(v: i32) -> Result<Self, Error> {
602 let val_u8 = BoundedInt32::<0, 255>::checked_new(v)?;
603 Ok(SendMeVersion::new(val_u8.get() as u8))
604 }
605}
606
607#[cfg(doc)]
613#[doc(hidden)]
614mod compile_fail_tests {
615 fn uninhabited_saturating_new() {}
620
621 fn uninhabited_from_string() {}
626}
627
628#[cfg(test)]
629mod tests {
630 #![allow(clippy::unwrap_used)]
631 use float_cmp::assert_approx_eq;
632
633 use super::*;
634
635 type TestFoo = BoundedInt32<1, 5>;
636 type TestBar = BoundedInt32<-45, 17>;
637
638 #[test]
640 fn entire_range_parsed() {
641 let x: TestFoo = "1".parse().unwrap();
642 assert!(x.get() == 1);
643 let x: TestFoo = "2".parse().unwrap();
644 assert!(x.get() == 2);
645 let x: TestFoo = "3".parse().unwrap();
646 assert!(x.get() == 3);
647 let x: TestFoo = "4".parse().unwrap();
648 assert!(x.get() == 4);
649 let x: TestFoo = "5".parse().unwrap();
650 assert!(x.get() == 5);
651 }
652
653 #[test]
654 fn saturating() {
655 let x: TestFoo = TestFoo::saturating_new(1000);
656 let x_val: i32 = x.into();
657 assert!(x_val == TestFoo::UPPER);
658 let x: TestFoo = TestFoo::saturating_new(0);
659 let x_val: i32 = x.into();
660 assert!(x_val == TestFoo::LOWER);
661 }
662 #[test]
663 fn saturating_string() {
664 let x: TestFoo = TestFoo::saturating_from_str("1000").unwrap();
665 let x_val: i32 = x.into();
666 assert!(x_val == TestFoo::UPPER);
667 let x: TestFoo = TestFoo::saturating_from_str("0").unwrap();
668 let x_val: i32 = x.into();
669 assert!(x_val == TestFoo::LOWER);
670 }
671
672 #[test]
673 fn errors_correct() {
674 let x: Result<TestBar, Error> = "1000".parse();
675 assert!(x.unwrap_err() == Error::AboveUpperBound(1000, TestBar::UPPER));
676 let x: Result<TestBar, Error> = "-1000".parse();
677 assert!(x.unwrap_err() == Error::BelowLowerBound(-1000, TestBar::LOWER));
678 let x: Result<TestBar, Error> = "xyz".parse();
679 assert!(x.unwrap_err() == Error::Unrepresentable);
680 }
681
682 #[test]
683 fn display() {
684 let v = BoundedInt32::<99, 1000>::checked_new(345).unwrap();
685 assert_eq!(v.to_string(), "345".to_string());
686 }
687
688 #[test]
689 #[should_panic]
690 fn checked_too_high() {
691 let _: TestBar = "1000".parse().unwrap();
692 }
693
694 #[test]
695 #[should_panic]
696 fn checked_too_low() {
697 let _: TestBar = "-46".parse().unwrap();
698 }
699
700 #[test]
701 fn bounded_to_u64() {
702 let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
703 let u: u64 = b.try_into().unwrap();
704 assert_eq!(u, 77);
705
706 let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(-77).unwrap();
707 let u: Result<u64, Error> = b.try_into();
708 assert!(u.is_err());
709 }
710
711 #[test]
712 fn bounded_to_f64() {
713 let x: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
714 let f: f64 = x.into();
715 assert_approx_eq!(f64, f, 77.0);
716 }
717
718 #[test]
719 fn bounded_from_i32() {
720 let x: Result<BoundedInt32<-100, 100>, _> = 50.try_into();
721 let y: i32 = x.unwrap().into();
722 assert_eq!(y, 50);
723
724 let x: Result<BoundedInt32<-100, 100>, _> = 1000.try_into();
725 assert!(x.is_err());
726 }
727
728 #[test]
729 fn into_bool() {
730 let zero: BoundedInt32<0, 1> = BoundedInt32::saturating_from(0);
731 let one: BoundedInt32<0, 1> = BoundedInt32::saturating_from(1);
732
733 let f: bool = zero.into();
734 let t: bool = one.into();
735 assert!(!f);
736 assert!(t);
737 }
738
739 #[test]
740 fn into_u8() {
741 let zero: BoundedInt32<0, 255> = BoundedInt32::saturating_from(0);
742 let one: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1);
743 let ninety: BoundedInt32<0, 255> = BoundedInt32::saturating_from(90);
744 let max: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1000);
745
746 let a: u8 = zero.into();
747 let b: u8 = one.into();
748 let c: u8 = ninety.into();
749 let d: u8 = max.into();
750
751 assert_eq!(a, 0);
752 assert_eq!(b, 1);
753 assert_eq!(c, 90);
754 assert_eq!(d, 255);
755 }
756
757 #[test]
758 fn into_u32() {
759 let zero: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(0);
760 let one: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1);
761 let ninety: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(90);
762 let max: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1000);
763
764 assert_eq!(u32::from(zero), 0);
765 assert_eq!(u32::from(one), 1);
766 assert_eq!(u32::from(ninety), 90);
767 assert_eq!(u32::from(max), 1000);
768
769 let zero: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(0);
770 let one: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1);
771 let ninety: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(90);
772 let max: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1000);
773
774 assert_eq!(u32::from(zero), 1);
775 assert_eq!(u32::from(one), 1);
776 assert_eq!(u32::from(ninety), 90);
777 assert_eq!(u32::from(max), 1000);
778 }
779
780 #[test]
781 fn try_into_usize() {
782 let b0: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(0);
783 let b100: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(100);
784 let bn5: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(-5);
785 assert_eq!(usize::try_from(b0), Ok(0_usize));
786 assert_eq!(usize::try_from(b100), Ok(100_usize));
787 assert_eq!(usize::try_from(bn5), Err(Error::Negative));
788 }
789
790 #[test]
791 fn percents() {
792 type Pct = Percentage<u8>;
793 let p = Pct::new(100);
794 assert_eq!(p.as_percent(), 100);
795 assert_approx_eq!(f64, p.as_fraction(), 1.0);
796
797 let p = Pct::new(0);
798 assert_eq!(p.as_percent(), 0);
799 assert_approx_eq!(f64, p.as_fraction(), 0.0);
800
801 let p = Pct::new(25);
802 assert_eq!(p.as_percent(), 25);
803 assert_eq!(p.clone(), p);
804 assert_approx_eq!(f64, p.as_fraction(), 0.25);
805
806 type BPct = Percentage<BoundedInt32<0, 100>>;
807 assert_eq!(BPct::try_from(99).unwrap().as_percent().get(), 99);
808 }
809
810 #[test]
811 fn milliseconds() {
812 type Msec = IntegerMilliseconds<i32>;
813
814 let ms = Msec::new(500);
815 let d: Result<Duration, _> = ms.try_into();
816 assert_eq!(d.unwrap(), Duration::from_millis(500));
817 assert_eq!(Duration::try_from(ms * 2).unwrap(), Duration::from_secs(1));
818
819 let ms = Msec::new(-100);
820 let d: Result<Duration, _> = ms.try_into();
821 assert!(d.is_err());
822
823 type BMSec = IntegerMilliseconds<BoundedInt32<0, 1000>>;
824 let half_sec = BMSec::try_from(500).unwrap();
825 assert_eq!(
826 Duration::try_from(half_sec).unwrap(),
827 Duration::from_millis(500)
828 );
829 assert!(BMSec::try_from(1001).is_err());
830 }
831
832 #[test]
833 fn seconds() {
834 type Sec = IntegerSeconds<i32>;
835
836 let ms = Sec::new(500);
837 let d: Result<Duration, _> = ms.try_into();
838 assert_eq!(d.unwrap(), Duration::from_secs(500));
839
840 let ms = Sec::new(-100);
841 let d: Result<Duration, _> = ms.try_into();
842 assert!(d.is_err());
843
844 type BSec = IntegerSeconds<BoundedInt32<0, 3600>>;
845 let half_hour = BSec::try_from(1800).unwrap();
846 assert_eq!(
847 Duration::try_from(half_hour).unwrap(),
848 Duration::from_secs(1800)
849 );
850 assert!(BSec::try_from(9999).is_err());
851 assert_eq!(half_hour.clone(), half_hour);
852 }
853
854 #[test]
855 fn minutes() {
856 type Min = IntegerMinutes<i32>;
857
858 let t = Min::new(500);
859 let d: Duration = t.try_into().unwrap();
860 assert_eq!(d, Duration::from_secs(500 * 60));
861
862 let t = Min::new(-100);
863 let d: Result<Duration, _> = t.try_into();
864 assert_eq!(d, Err(Error::Overflow));
865
866 let t = IntegerMinutes::<u64>::new(u64::MAX);
867 let d: Result<Duration, _> = t.try_into();
868 assert_eq!(d, Err(Error::Overflow));
869
870 type BMin = IntegerMinutes<BoundedInt32<10, 30>>;
871 assert_eq!(
872 BMin::new(17_i32.try_into().unwrap()),
873 BMin::try_from(17).unwrap()
874 );
875 }
876
877 #[test]
878 fn days() {
879 type Days = IntegerDays<i32>;
880
881 let t = Days::new(500);
882 let d: Duration = t.try_into().unwrap();
883 assert_eq!(d, Duration::from_secs(500 * 86400));
884
885 let t = Days::new(-100);
886 let d: Result<Duration, _> = t.try_into();
887 assert_eq!(d, Err(Error::Overflow));
888
889 let t = IntegerDays::<u64>::new(u64::MAX);
890 let d: Result<Duration, _> = t.try_into();
891 assert_eq!(d, Err(Error::Overflow));
892
893 type BDays = IntegerDays<BoundedInt32<10, 30>>;
894 assert_eq!(
895 BDays::new(17_i32.try_into().unwrap()),
896 BDays::try_from(17).unwrap()
897 );
898 }
899
900 #[test]
901 fn sendme() {
902 let smv = SendMeVersion::new(5);
903 assert_eq!(smv.get(), 5);
904 assert_eq!(smv.clone().get(), 5);
905 assert_eq!(smv, SendMeVersion::try_from(5).unwrap());
906 }
907}