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#![warn(clippy::cognitive_complexity)]
15#![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#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![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)]
47#![deny(clippy::string_slice)] use derive_more::{Add, Display, Div, From, FromStr, Mul};
51
52use serde::{Deserialize, Serialize};
53use std::time::Duration;
54use thiserror::Error;
55
56#[cfg(feature = "memquota-memcost")]
57use {derive_deftly::Deftly, tor_memquota::derive_deftly_template_HasMemoryCost};
58
59#[derive(Debug, Clone, PartialEq, Eq, Error)]
61#[non_exhaustive]
62pub enum Error {
63 #[error("Value {0} was below the lower bound {1} for this type")]
65 BelowLowerBound(i32, i32),
66 #[error("Value {0} was above the lower bound {1} for this type")]
68 AboveUpperBound(i32, i32),
69 #[error("Tried to convert a negative value to an unsigned type")]
71 Negative,
72 #[error("Value could not be represented as an i32")]
75 Unrepresentable,
76 #[error("Integer overflow")]
78 Overflow,
79}
80
81#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
98#[cfg_attr(
99 feature = "memquota-memcost",
100 derive(Deftly),
101 derive_deftly(HasMemoryCost)
102)]
103pub struct BoundedInt32<const LOWER: i32, const UPPER: i32> {
104 value: i32,
106}
107
108impl<const LOWER: i32, const UPPER: i32> BoundedInt32<LOWER, UPPER> {
109 pub const LOWER: i32 = LOWER;
111 pub const UPPER: i32 = UPPER;
113
114 fn unchecked_new(value: i32) -> Self {
116 const { assert!(LOWER <= UPPER) };
119
120 BoundedInt32 { value }
121 }
122
123 pub const fn lower(&self) -> i32 {
127 LOWER
128 }
129
130 pub const fn upper(&self) -> i32 {
134 UPPER
135 }
136
137 pub fn get(&self) -> i32 {
142 self.value
143 }
144
145 pub fn get_u32(&self) -> u32 {
152 const { assert!(LOWER >= 0) };
153 self.value as u32
154 }
155
156 pub fn saturating_new(val: i32) -> Self {
160 Self::unchecked_new(Self::clamp(val))
161 }
162
163 pub fn checked_new(val: i32) -> Result<Self, Error> {
166 if val > UPPER {
167 Err(Error::AboveUpperBound(val, UPPER))
168 } else if val < LOWER {
169 Err(Error::BelowLowerBound(val, LOWER))
170 } else {
171 Ok(BoundedInt32::unchecked_new(val))
172 }
173 }
174
175 fn clamp(val: i32) -> i32 {
177 Ord::clamp(val, LOWER, UPPER)
178 }
179
180 pub fn saturating_from(val: i32) -> Self {
187 Self::unchecked_new(Self::clamp(val))
188 }
189
190 pub fn saturating_from_str(s: &str) -> Result<Self, Error> {
197 let val: i32 = s.parse().map_err(|_| Error::Unrepresentable)?;
198 Ok(Self::saturating_from(val))
199 }
200}
201
202impl<const L: i32, const U: i32> std::fmt::Display for BoundedInt32<L, U> {
203 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204 write!(f, "{}", self.value)
205 }
206}
207
208impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for i32 {
209 fn from(val: BoundedInt32<L, U>) -> i32 {
210 val.value
211 }
212}
213
214impl<const L: i32, const U: i32> From<BoundedInt32<L, U>> for f64 {
215 fn from(val: BoundedInt32<L, U>) -> f64 {
216 val.value.into()
217 }
218}
219
220impl<const L: i32, const H: i32> TryFrom<i32> for BoundedInt32<L, H> {
221 type Error = Error;
222 fn try_from(val: i32) -> Result<Self, Self::Error> {
223 Self::checked_new(val)
224 }
225}
226
227impl<const L: i32, const H: i32> std::str::FromStr for BoundedInt32<L, H> {
228 type Err = Error;
229 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
230 Self::checked_new(s.parse().map_err(|_| Error::Unrepresentable)?)
231 }
232}
233
234impl From<BoundedInt32<0, 1>> for bool {
235 fn from(val: BoundedInt32<0, 1>) -> bool {
236 val.value == 1
237 }
238}
239
240impl From<BoundedInt32<0, 255>> for u8 {
241 fn from(val: BoundedInt32<0, 255>) -> u8 {
242 val.value as u8
243 }
244}
245
246impl<const L: i32, const H: i32> From<BoundedInt32<L, H>> for u32 {
247 fn from(val: BoundedInt32<L, H>) -> u32 {
248 val.value as u32
249 }
250}
251
252impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for u64 {
253 type Error = Error;
254 fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
255 if val.value < 0 {
256 Err(Error::Negative)
257 } else {
258 Ok(val.value as u64)
259 }
260 }
261}
262
263impl<const L: i32, const H: i32> TryFrom<BoundedInt32<L, H>> for usize {
264 type Error = Error;
265 fn try_from(val: BoundedInt32<L, H>) -> Result<Self, Self::Error> {
266 if val.value < 0 {
267 Err(Error::Negative)
268 } else {
269 Ok(val.value as usize)
270 }
271 }
272}
273
274#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
279pub struct Percentage<T: Copy + Into<f64>> {
280 value: T,
282}
283
284impl<T: Copy + Into<f64>> Percentage<T> {
285 pub fn new(value: T) -> Self {
287 Self { value }
288 }
289
290 pub fn as_fraction(self) -> f64 {
304 self.value.into() / 100.0
305 }
306
307 pub fn as_percent(self) -> T {
320 self.value
321 }
322}
323
324impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>> {
325 type Error = Error;
326 fn try_from(v: i32) -> Result<Self, Error> {
327 Ok(Percentage::new(v.try_into()?))
328 }
329}
330
331#[derive(
335 Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
336)]
337pub struct IntegerMilliseconds<T> {
341 value: T,
343}
344
345impl<T> IntegerMilliseconds<T> {
346 pub fn new(value: T) -> Self {
348 IntegerMilliseconds { value }
349 }
350
351 pub fn as_millis(self) -> T {
356 self.value
357 }
358
359 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMilliseconds<U>, E>
371 where
372 F: FnOnce(T) -> Result<U, E>,
373 {
374 Ok(IntegerMilliseconds::new(f(self.value)?))
375 }
376}
377
378impl<T: TryInto<u64>> TryFrom<IntegerMilliseconds<T>> for Duration {
379 type Error = <T as TryInto<u64>>::Error;
380 fn try_from(val: IntegerMilliseconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
381 Ok(Self::from_millis(val.value.try_into()?))
382 }
383}
384
385impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMilliseconds<BoundedInt32<H, L>> {
386 type Error = Error;
387 fn try_from(v: i32) -> Result<Self, Error> {
388 Ok(IntegerMilliseconds::new(v.try_into()?))
389 }
390}
391
392#[derive(
393 Add, Copy, Clone, Mul, Div, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash,
394)]
395pub struct IntegerSeconds<T> {
399 value: T,
401}
402
403impl<T> IntegerSeconds<T> {
404 pub fn new(value: T) -> Self {
406 IntegerSeconds { value }
407 }
408
409 pub fn as_secs(self) -> T {
414 self.value
415 }
416
417 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerSeconds<U>, E>
427 where
428 F: FnOnce(T) -> Result<U, E>,
429 {
430 Ok(IntegerSeconds::new(f(self.value)?))
431 }
432}
433
434impl<T: TryInto<u64>> TryFrom<IntegerSeconds<T>> for Duration {
435 type Error = <T as TryInto<u64>>::Error;
436 fn try_from(val: IntegerSeconds<T>) -> Result<Self, <T as TryInto<u64>>::Error> {
437 Ok(Self::from_secs(val.value.try_into()?))
438 }
439}
440
441impl<const H: i32, const L: i32> TryFrom<i32> for IntegerSeconds<BoundedInt32<H, L>> {
442 type Error = Error;
443 fn try_from(v: i32) -> Result<Self, Error> {
444 Ok(IntegerSeconds::new(v.try_into()?))
445 }
446}
447
448#[derive(Deserialize, Serialize)] #[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
450pub struct IntegerMinutes<T> {
454 value: T,
456}
457
458impl<T> IntegerMinutes<T> {
459 pub fn new(value: T) -> Self {
461 IntegerMinutes { value }
462 }
463
464 pub fn as_minutes(self) -> T {
469 self.value
470 }
471
472 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerMinutes<U>, E>
482 where
483 F: FnOnce(T) -> Result<U, E>,
484 {
485 Ok(IntegerMinutes::new(f(self.value)?))
486 }
487}
488
489impl<T: TryInto<u64>> TryFrom<IntegerMinutes<T>> for Duration {
490 type Error = Error;
491 fn try_from(val: IntegerMinutes<T>) -> Result<Self, Error> {
492 const SECONDS_PER_MINUTE: u64 = 60;
494 let minutes: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
495 let seconds = minutes
496 .checked_mul(SECONDS_PER_MINUTE)
497 .ok_or(Error::Overflow)?;
498 Ok(Self::from_secs(seconds))
499 }
500}
501
502impl<const H: i32, const L: i32> TryFrom<i32> for IntegerMinutes<BoundedInt32<H, L>> {
503 type Error = Error;
504 fn try_from(v: i32) -> Result<Self, Error> {
505 Ok(IntegerMinutes::new(v.try_into()?))
506 }
507}
508
509#[derive(Copy, Clone, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
510pub struct IntegerDays<T> {
514 value: T,
516}
517
518impl<T> IntegerDays<T> {
519 pub fn new(value: T) -> Self {
521 IntegerDays { value }
522 }
523
524 pub fn as_days(self) -> T {
529 self.value
530 }
531
532 pub fn try_map<U, F, E>(self, f: F) -> Result<IntegerDays<U>, E>
542 where
543 F: FnOnce(T) -> Result<U, E>,
544 {
545 Ok(IntegerDays::new(f(self.value)?))
546 }
547}
548
549impl<T: TryInto<u64>> TryFrom<IntegerDays<T>> for Duration {
550 type Error = Error;
551 fn try_from(val: IntegerDays<T>) -> Result<Self, Error> {
552 const SECONDS_PER_DAY: u64 = 86400;
554 let days: u64 = val.value.try_into().map_err(|_| Error::Overflow)?;
555 let seconds = days.checked_mul(SECONDS_PER_DAY).ok_or(Error::Overflow)?;
556 Ok(Self::from_secs(seconds))
557 }
558}
559
560impl<const H: i32, const L: i32> TryFrom<i32> for IntegerDays<BoundedInt32<H, L>> {
561 type Error = Error;
562 fn try_from(v: i32) -> Result<Self, Error> {
563 Ok(IntegerDays::new(v.try_into()?))
564 }
565}
566
567#[derive(Clone, Copy, From, FromStr, Display, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
571pub struct SendMeVersion(u8);
572
573impl SendMeVersion {
574 pub fn new(value: u8) -> Self {
576 SendMeVersion(value)
577 }
578
579 pub fn get(&self) -> u8 {
581 self.0
582 }
583}
584
585impl TryFrom<i32> for SendMeVersion {
586 type Error = Error;
587 fn try_from(v: i32) -> Result<Self, Error> {
588 let val_u8 = BoundedInt32::<0, 255>::checked_new(v)?;
589 Ok(SendMeVersion::new(val_u8.get() as u8))
590 }
591}
592
593#[cfg(doc)]
599#[doc(hidden)]
600mod compile_fail_tests {
601 fn uninhabited_saturating_new() {}
606
607 fn uninhabited_from_string() {}
612}
613
614#[cfg(test)]
615mod tests {
616 #![allow(clippy::unwrap_used)]
617 use float_cmp::assert_approx_eq;
618
619 use super::*;
620
621 type TestFoo = BoundedInt32<1, 5>;
622 type TestBar = BoundedInt32<-45, 17>;
623
624 #[test]
626 fn entire_range_parsed() {
627 let x: TestFoo = "1".parse().unwrap();
628 assert!(x.get() == 1);
629 let x: TestFoo = "2".parse().unwrap();
630 assert!(x.get() == 2);
631 let x: TestFoo = "3".parse().unwrap();
632 assert!(x.get() == 3);
633 let x: TestFoo = "4".parse().unwrap();
634 assert!(x.get() == 4);
635 let x: TestFoo = "5".parse().unwrap();
636 assert!(x.get() == 5);
637 }
638
639 #[test]
640 fn saturating() {
641 let x: TestFoo = TestFoo::saturating_new(1000);
642 let x_val: i32 = x.into();
643 assert!(x_val == TestFoo::UPPER);
644 let x: TestFoo = TestFoo::saturating_new(0);
645 let x_val: i32 = x.into();
646 assert!(x_val == TestFoo::LOWER);
647 }
648 #[test]
649 fn saturating_string() {
650 let x: TestFoo = TestFoo::saturating_from_str("1000").unwrap();
651 let x_val: i32 = x.into();
652 assert!(x_val == TestFoo::UPPER);
653 let x: TestFoo = TestFoo::saturating_from_str("0").unwrap();
654 let x_val: i32 = x.into();
655 assert!(x_val == TestFoo::LOWER);
656 }
657
658 #[test]
659 fn errors_correct() {
660 let x: Result<TestBar, Error> = "1000".parse();
661 assert!(x.unwrap_err() == Error::AboveUpperBound(1000, TestBar::UPPER));
662 let x: Result<TestBar, Error> = "-1000".parse();
663 assert!(x.unwrap_err() == Error::BelowLowerBound(-1000, TestBar::LOWER));
664 let x: Result<TestBar, Error> = "xyz".parse();
665 assert!(x.unwrap_err() == Error::Unrepresentable);
666 }
667
668 #[test]
669 fn display() {
670 let v = BoundedInt32::<99, 1000>::checked_new(345).unwrap();
671 assert_eq!(v.to_string(), "345".to_string());
672 }
673
674 #[test]
675 #[should_panic]
676 fn checked_too_high() {
677 let _: TestBar = "1000".parse().unwrap();
678 }
679
680 #[test]
681 #[should_panic]
682 fn checked_too_low() {
683 let _: TestBar = "-46".parse().unwrap();
684 }
685
686 #[test]
687 fn bounded_to_u64() {
688 let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
689 let u: u64 = b.try_into().unwrap();
690 assert_eq!(u, 77);
691
692 let b: BoundedInt32<-100, 100> = BoundedInt32::checked_new(-77).unwrap();
693 let u: Result<u64, Error> = b.try_into();
694 assert!(u.is_err());
695 }
696
697 #[test]
698 fn bounded_to_f64() {
699 let x: BoundedInt32<-100, 100> = BoundedInt32::checked_new(77).unwrap();
700 let f: f64 = x.into();
701 assert_approx_eq!(f64, f, 77.0);
702 }
703
704 #[test]
705 fn bounded_from_i32() {
706 let x: Result<BoundedInt32<-100, 100>, _> = 50.try_into();
707 let y: i32 = x.unwrap().into();
708 assert_eq!(y, 50);
709
710 let x: Result<BoundedInt32<-100, 100>, _> = 1000.try_into();
711 assert!(x.is_err());
712 }
713
714 #[test]
715 fn into_bool() {
716 let zero: BoundedInt32<0, 1> = BoundedInt32::saturating_from(0);
717 let one: BoundedInt32<0, 1> = BoundedInt32::saturating_from(1);
718
719 let f: bool = zero.into();
720 let t: bool = one.into();
721 assert!(!f);
722 assert!(t);
723 }
724
725 #[test]
726 fn into_u8() {
727 let zero: BoundedInt32<0, 255> = BoundedInt32::saturating_from(0);
728 let one: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1);
729 let ninety: BoundedInt32<0, 255> = BoundedInt32::saturating_from(90);
730 let max: BoundedInt32<0, 255> = BoundedInt32::saturating_from(1000);
731
732 let a: u8 = zero.into();
733 let b: u8 = one.into();
734 let c: u8 = ninety.into();
735 let d: u8 = max.into();
736
737 assert_eq!(a, 0);
738 assert_eq!(b, 1);
739 assert_eq!(c, 90);
740 assert_eq!(d, 255);
741 }
742
743 #[test]
744 fn into_u32() {
745 let zero: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(0);
746 let one: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1);
747 let ninety: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(90);
748 let max: BoundedInt32<0, 1000> = BoundedInt32::saturating_from(1000);
749
750 assert_eq!(u32::from(zero), 0);
751 assert_eq!(u32::from(one), 1);
752 assert_eq!(u32::from(ninety), 90);
753 assert_eq!(u32::from(max), 1000);
754
755 let zero: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(0);
756 let one: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1);
757 let ninety: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(90);
758 let max: BoundedInt32<1, 1000> = BoundedInt32::saturating_from(1000);
759
760 assert_eq!(u32::from(zero), 1);
761 assert_eq!(u32::from(one), 1);
762 assert_eq!(u32::from(ninety), 90);
763 assert_eq!(u32::from(max), 1000);
764 }
765
766 #[test]
767 fn try_into_usize() {
768 let b0: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(0);
769 let b100: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(100);
770 let bn5: BoundedInt32<-10, 300> = BoundedInt32::saturating_from(-5);
771 assert_eq!(usize::try_from(b0), Ok(0_usize));
772 assert_eq!(usize::try_from(b100), Ok(100_usize));
773 assert_eq!(usize::try_from(bn5), Err(Error::Negative));
774 }
775
776 #[test]
777 fn percents() {
778 type Pct = Percentage<u8>;
779 let p = Pct::new(100);
780 assert_eq!(p.as_percent(), 100);
781 assert_approx_eq!(f64, p.as_fraction(), 1.0);
782
783 let p = Pct::new(0);
784 assert_eq!(p.as_percent(), 0);
785 assert_approx_eq!(f64, p.as_fraction(), 0.0);
786
787 let p = Pct::new(25);
788 assert_eq!(p.as_percent(), 25);
789 assert_eq!(p.clone(), p);
790 assert_approx_eq!(f64, p.as_fraction(), 0.25);
791
792 type BPct = Percentage<BoundedInt32<0, 100>>;
793 assert_eq!(BPct::try_from(99).unwrap().as_percent().get(), 99);
794 }
795
796 #[test]
797 fn milliseconds() {
798 type Msec = IntegerMilliseconds<i32>;
799
800 let ms = Msec::new(500);
801 let d: Result<Duration, _> = ms.try_into();
802 assert_eq!(d.unwrap(), Duration::from_millis(500));
803 assert_eq!(Duration::try_from(ms * 2).unwrap(), Duration::from_secs(1));
804
805 let ms = Msec::new(-100);
806 let d: Result<Duration, _> = ms.try_into();
807 assert!(d.is_err());
808
809 type BMSec = IntegerMilliseconds<BoundedInt32<0, 1000>>;
810 let half_sec = BMSec::try_from(500).unwrap();
811 assert_eq!(
812 Duration::try_from(half_sec).unwrap(),
813 Duration::from_millis(500)
814 );
815 assert!(BMSec::try_from(1001).is_err());
816 }
817
818 #[test]
819 fn seconds() {
820 type Sec = IntegerSeconds<i32>;
821
822 let ms = Sec::new(500);
823 let d: Result<Duration, _> = ms.try_into();
824 assert_eq!(d.unwrap(), Duration::from_secs(500));
825
826 let ms = Sec::new(-100);
827 let d: Result<Duration, _> = ms.try_into();
828 assert!(d.is_err());
829
830 type BSec = IntegerSeconds<BoundedInt32<0, 3600>>;
831 let half_hour = BSec::try_from(1800).unwrap();
832 assert_eq!(
833 Duration::try_from(half_hour).unwrap(),
834 Duration::from_secs(1800)
835 );
836 assert!(BSec::try_from(9999).is_err());
837 assert_eq!(half_hour.clone(), half_hour);
838 }
839
840 #[test]
841 fn minutes() {
842 type Min = IntegerMinutes<i32>;
843
844 let t = Min::new(500);
845 let d: Duration = t.try_into().unwrap();
846 assert_eq!(d, Duration::from_secs(500 * 60));
847
848 let t = Min::new(-100);
849 let d: Result<Duration, _> = t.try_into();
850 assert_eq!(d, Err(Error::Overflow));
851
852 let t = IntegerMinutes::<u64>::new(u64::MAX);
853 let d: Result<Duration, _> = t.try_into();
854 assert_eq!(d, Err(Error::Overflow));
855
856 type BMin = IntegerMinutes<BoundedInt32<10, 30>>;
857 assert_eq!(
858 BMin::new(17_i32.try_into().unwrap()),
859 BMin::try_from(17).unwrap()
860 );
861 }
862
863 #[test]
864 fn days() {
865 type Days = IntegerDays<i32>;
866
867 let t = Days::new(500);
868 let d: Duration = t.try_into().unwrap();
869 assert_eq!(d, Duration::from_secs(500 * 86400));
870
871 let t = Days::new(-100);
872 let d: Result<Duration, _> = t.try_into();
873 assert_eq!(d, Err(Error::Overflow));
874
875 let t = IntegerDays::<u64>::new(u64::MAX);
876 let d: Result<Duration, _> = t.try_into();
877 assert_eq!(d, Err(Error::Overflow));
878
879 type BDays = IntegerDays<BoundedInt32<10, 30>>;
880 assert_eq!(
881 BDays::new(17_i32.try_into().unwrap()),
882 BDays::try_from(17).unwrap()
883 );
884 }
885
886 #[test]
887 fn sendme() {
888 let smv = SendMeVersion::new(5);
889 assert_eq!(smv.get(), 5);
890 assert_eq!(smv.clone().get(), 5);
891 assert_eq!(smv, SendMeVersion::try_from(5).unwrap());
892 }
893}