1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! An instant of time

use crate::Duration;
use core::{cmp::Ordering, convert::TryFrom, ops};
use num::traits::{WrappingAdd, WrappingSub};

/// Represents an instant of time relative to a specific clock
///
/// # Example
/// Create an `Instant` that is `23 * SomeClock::PERIOD` seconds since the clock's epoch:
/// ```rust,ignore
/// Instant::<SomeClock>::new(23);
/// ```
#[derive(Debug)]
pub struct Instant<Clock>
where
    Clock: crate::Clock,
{
    ticks: Clock::Rep,
}

impl<Clock> Instant<Clock>
where
    Clock: crate::Clock,
{
    pub fn new(ticks: Clock::Rep) -> Self {
        Self { ticks }
    }

    /// Calculates the difference between two `Instance`s resulting in a [`Duration`]
    ///
    /// # Examples
    /// ```rust
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// let diff: Option<Milliseconds<_>> = Instant::<Clock>::new(5).duration_since(&Instant::<Clock>::new(3));
    /// assert_eq!(diff, Some(Milliseconds(2_i32)));
    ///
    /// let diff: Option<Microseconds<i64>> = Instant::<Clock>::new(5).duration_since(&Instant::<Clock>::new(3));
    /// assert_eq!(diff, Some(Microseconds(2_000_i64)));
    ///
    /// let diff: Option<Microseconds<i64>> = Instant::<Clock>::new(i32::MIN).duration_since(&Instant::<Clock>::new(i32::MAX));
    /// assert_eq!(diff, Some(Microseconds(1_000_i64)));
    ///
    /// let diff: Option<Seconds<i64>> = Instant::<Clock>::new(1_000).duration_since(&Instant::<Clock>::new(-1_000));
    /// assert_eq!(diff, Some(Seconds(2_i64)));
    /// ```
    pub fn duration_since<Dur>(&self, other: &Self) -> Option<Dur>
    where
        Dur: Duration,
        Dur::Rep: TryFrom<Clock::Rep>,
    {
        Dur::from_ticks(self.ticks.wrapping_sub(&other.ticks), Clock::PERIOD)
    }

    pub fn elapsed_since_epoch<Dur>(&self) -> Option<Dur>
    where
        Dur: Duration,
        Dur::Rep: TryFrom<Clock::Rep>,
        Clock::Rep: From<i32>,
    {
        Self::duration_since::<Dur>(
            &self,
            &Self {
                ticks: Clock::Rep::from(0_i32),
            },
        )
    }
}

impl<Clock> Copy for Instant<Clock> where Clock: crate::Clock {}

impl<Clock> Clone for Instant<Clock>
where
    Clock: crate::Clock,
{
    fn clone(&self) -> Self {
        Self { ticks: self.ticks }
    }
}

impl<Clock> PartialEq for Instant<Clock>
where
    Clock: crate::Clock,
{
    fn eq(&self, other: &Self) -> bool {
        self.ticks == other.ticks
    }
}

impl<Clock> Eq for Instant<Clock> where Clock: crate::Clock {}

impl<Clock> PartialOrd for Instant<Clock>
where
    Clock: crate::Clock,
{
    /// Calculates the difference between two `Instance`s resulting in a [`Duration`]
    ///
    /// # Examples
    /// ```rust
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// assert!(Instant::<Clock>::new(5) > Instant::<Clock>::new(3));
    /// assert!(Instant::<Clock>::new(5) == Instant::<Clock>::new(5));
    /// assert!(Instant::<Clock>::new(i32::MAX) < Instant::<Clock>::new(i32::MIN));
    /// ```
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(&other))
    }
}

impl<Clock> Ord for Instant<Clock>
where
    Clock: crate::Clock,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.ticks
            .wrapping_sub(&other.ticks)
            .cmp(&Clock::Rep::from(0))
    }
}

impl<Clock, Dur> ops::Add<Dur> for Instant<Clock>
where
    Clock: crate::Clock,
    Clock::Rep: TryFrom<Dur::Rep>,
    Dur: Duration,
{
    type Output = Self;

    /// Add a duration to an instant resulting in a new, later instance
    ///
    /// # Panics
    /// If [`Duration::into_ticks()`] returns [`None`]. In this case, `i32::MAX` of seconds
    /// cannot be converted to the clock precision of milliseconds with i32 storage.
    /// ```rust,should_panic
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// Instant::<Clock>::new(1) + Seconds(i32::MAX);
    /// ```
    /// See also: [`impl Add for Duration`](duration/time_units/index.html#addsub)
    ///
    /// # Examples
    /// ```rust
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// assert_eq!(Instant::<Clock>::new(1) + Seconds(3), Instant::<Clock>::new(3_001));
    /// assert_eq!(Instant::<Clock>::new(-1) + Milliseconds(5_123), Instant::<Clock>::new(5_122));
    /// assert_eq!(Instant::<Clock>::new(1) + Milliseconds(700), Instant::<Clock>::new(701));
    /// assert_eq!(Instant::<Clock>::new(1_i32) + Milliseconds(700_i64), Instant::<Clock>::new(701_i32));
    /// ```
    fn add(self, rhs: Dur) -> Self::Output {
        let add_ticks: Clock::Rep = rhs.into_ticks(Clock::PERIOD).unwrap();

        Self {
            ticks: self.ticks.wrapping_add(&add_ticks),
        }
    }
}

impl<Clock, Dur> ops::Sub<Dur> for Instant<Clock>
where
    Clock: crate::clock::Clock,
    Clock::Rep: TryFrom<Dur::Rep>,
    Dur: Duration,
{
    type Output = Self;

    /// Subtract a duration from an instant resulting in a new, earlier instance
    ///
    /// # Panics
    /// If [`Duration::into_ticks()`] returns [`None`]. In this case, `i32::MAX` of seconds
    /// cannot be converted to the clock precision of milliseconds with i32 storage.
    /// ```rust,should_panic
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// Instant::<Clock>::new(1) - Seconds(i32::MAX);
    /// ```
    /// See also [`impl Sub for Duration`](duration/time_units/index.html#addsub)
    ///
    /// # Examples
    /// ```rust
    /// # use embedded_time::{Period, time_units::*, instant::Instant};
    /// # #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
    /// struct Clock;
    /// impl embedded_time::Clock for Clock {
    ///     type Rep = i32;
    ///     const PERIOD: Period = Period::new_raw(1, 1_000);
    ///     // ...
    /// # fn now() -> Instant<Self> {unimplemented!()}
    /// }
    ///
    /// assert_eq!(Instant::<Clock>::new(1) - Seconds(3), Instant::<Clock>::new(-2_999));
    /// assert_eq!(Instant::<Clock>::new(-1) - Milliseconds(5_123), Instant::<Clock>::new(-5_124));
    /// assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700), Instant::<Clock>::new(100));
    /// assert_eq!(Instant::<Clock>::new(5_000_i32) - Milliseconds(700_i64), Instant::<Clock>::new(4_300_i32));
    /// ```
    fn sub(self, rhs: Dur) -> Self::Output {
        let sub_ticks: Clock::Rep = rhs.into_ticks(Clock::PERIOD).unwrap();

        Self {
            ticks: self.ticks.wrapping_sub(&sub_ticks),
        }
    }
}