| 1 | use crate::convert::*; |
| 2 | use crate::Duration; |
| 3 | |
| 4 | /// Sealed trait to prevent downstream implementations. |
| 5 | mod sealed { |
| 6 | /// A trait that cannot be implemented by downstream users. |
| 7 | pub trait Sealed {} |
| 8 | impl Sealed for i64 {} |
| 9 | impl Sealed for f64 {} |
| 10 | } |
| 11 | |
| 12 | /// Create [`Duration`]s from numeric literals. |
| 13 | /// |
| 14 | /// # Examples |
| 15 | /// |
| 16 | /// Basic construction of [`Duration`]s. |
| 17 | /// |
| 18 | /// ```rust |
| 19 | /// # use time::{Duration, ext::NumericalDuration}; |
| 20 | /// assert_eq!(5.nanoseconds(), Duration::nanoseconds(5)); |
| 21 | /// assert_eq!(5.microseconds(), Duration::microseconds(5)); |
| 22 | /// assert_eq!(5.milliseconds(), Duration::milliseconds(5)); |
| 23 | /// assert_eq!(5.seconds(), Duration::seconds(5)); |
| 24 | /// assert_eq!(5.minutes(), Duration::minutes(5)); |
| 25 | /// assert_eq!(5.hours(), Duration::hours(5)); |
| 26 | /// assert_eq!(5.days(), Duration::days(5)); |
| 27 | /// assert_eq!(5.weeks(), Duration::weeks(5)); |
| 28 | /// ``` |
| 29 | /// |
| 30 | /// Signed integers work as well! |
| 31 | /// |
| 32 | /// ```rust |
| 33 | /// # use time::{Duration, ext::NumericalDuration}; |
| 34 | /// assert_eq!((-5).nanoseconds(), Duration::nanoseconds(-5)); |
| 35 | /// assert_eq!((-5).microseconds(), Duration::microseconds(-5)); |
| 36 | /// assert_eq!((-5).milliseconds(), Duration::milliseconds(-5)); |
| 37 | /// assert_eq!((-5).seconds(), Duration::seconds(-5)); |
| 38 | /// assert_eq!((-5).minutes(), Duration::minutes(-5)); |
| 39 | /// assert_eq!((-5).hours(), Duration::hours(-5)); |
| 40 | /// assert_eq!((-5).days(), Duration::days(-5)); |
| 41 | /// assert_eq!((-5).weeks(), Duration::weeks(-5)); |
| 42 | /// ``` |
| 43 | /// |
| 44 | /// Just like any other [`Duration`], they can be added, subtracted, etc. |
| 45 | /// |
| 46 | /// ```rust |
| 47 | /// # use time::ext::NumericalDuration; |
| 48 | /// assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds()); |
| 49 | /// assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds()); |
| 50 | /// ``` |
| 51 | /// |
| 52 | /// When called on floating point values, any remainder of the floating point value will be |
| 53 | /// truncated. Keep in mind that floating point numbers are inherently imprecise and have |
| 54 | /// limited capacity. |
| 55 | pub trait NumericalDuration: sealed::Sealed { |
| 56 | /// Create a [`Duration`] from the number of nanoseconds. |
| 57 | fn nanoseconds(self) -> Duration; |
| 58 | /// Create a [`Duration`] from the number of microseconds. |
| 59 | fn microseconds(self) -> Duration; |
| 60 | /// Create a [`Duration`] from the number of milliseconds. |
| 61 | fn milliseconds(self) -> Duration; |
| 62 | /// Create a [`Duration`] from the number of seconds. |
| 63 | fn seconds(self) -> Duration; |
| 64 | /// Create a [`Duration`] from the number of minutes. |
| 65 | fn minutes(self) -> Duration; |
| 66 | /// Create a [`Duration`] from the number of hours. |
| 67 | fn hours(self) -> Duration; |
| 68 | /// Create a [`Duration`] from the number of days. |
| 69 | fn days(self) -> Duration; |
| 70 | /// Create a [`Duration`] from the number of weeks. |
| 71 | fn weeks(self) -> Duration; |
| 72 | } |
| 73 | |
| 74 | impl NumericalDuration for i64 { |
| 75 | fn nanoseconds(self) -> Duration { |
| 76 | Duration::nanoseconds(self) |
| 77 | } |
| 78 | |
| 79 | fn microseconds(self) -> Duration { |
| 80 | Duration::microseconds(self) |
| 81 | } |
| 82 | |
| 83 | fn milliseconds(self) -> Duration { |
| 84 | Duration::milliseconds(self) |
| 85 | } |
| 86 | |
| 87 | fn seconds(self) -> Duration { |
| 88 | Duration::seconds(self) |
| 89 | } |
| 90 | |
| 91 | fn minutes(self) -> Duration { |
| 92 | Duration::minutes(self) |
| 93 | } |
| 94 | |
| 95 | fn hours(self) -> Duration { |
| 96 | Duration::hours(self) |
| 97 | } |
| 98 | |
| 99 | fn days(self) -> Duration { |
| 100 | Duration::days(self) |
| 101 | } |
| 102 | |
| 103 | fn weeks(self) -> Duration { |
| 104 | Duration::weeks(self) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl NumericalDuration for f64 { |
| 109 | fn nanoseconds(self) -> Duration { |
| 110 | Duration::nanoseconds(self as _) |
| 111 | } |
| 112 | |
| 113 | fn microseconds(self) -> Duration { |
| 114 | Duration::nanoseconds((self * Nanosecond::per(Microsecond) as Self) as _) |
| 115 | } |
| 116 | |
| 117 | fn milliseconds(self) -> Duration { |
| 118 | Duration::nanoseconds((self * Nanosecond::per(Millisecond) as Self) as _) |
| 119 | } |
| 120 | |
| 121 | fn seconds(self) -> Duration { |
| 122 | Duration::nanoseconds((self * Nanosecond::per(Second) as Self) as _) |
| 123 | } |
| 124 | |
| 125 | fn minutes(self) -> Duration { |
| 126 | Duration::nanoseconds((self * Nanosecond::per(Minute) as Self) as _) |
| 127 | } |
| 128 | |
| 129 | fn hours(self) -> Duration { |
| 130 | Duration::nanoseconds((self * Nanosecond::per(Hour) as Self) as _) |
| 131 | } |
| 132 | |
| 133 | fn days(self) -> Duration { |
| 134 | Duration::nanoseconds((self * Nanosecond::per(Day) as Self) as _) |
| 135 | } |
| 136 | |
| 137 | fn weeks(self) -> Duration { |
| 138 | Duration::nanoseconds((self * Nanosecond::per(Week) as Self) as _) |
| 139 | } |
| 140 | } |
| 141 | |