Skip to content

Commit d63fccd

Browse files
committed
refactor(Clock): Allow immutable Clock objects
Changed `now(&mut self)` to `now(&self)`
1 parent beb9158 commit d63fccd

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

examples/nrf52_dk/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ impl time::Clock for SysClock {
3838
type Rep = u64;
3939
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
4040

41-
fn now(&mut self) -> time::Instant<Self> {
41+
fn now(&self) -> time::Instant<Self> {
4242
self.capture_task.tasks_trigger[0].write(|write| unsafe { write.bits(1) });
4343

4444
let ticks =
4545
self.low.cc[0].read().bits() as u64 | ((self.high.cc[0].read().bits() as u64) << 32);
4646

47-
time::Instant::new(ticks as Self::Rep)
47+
Ok(time::Instant::new(ticks as Self::Rep))
4848
}
4949
}
5050

@@ -150,12 +150,12 @@ where
150150
led2.set_high()?;
151151
led3.set_high()?;
152152
led4.set_low()?;
153-
clock.delay(250_u32.milliseconds());
153+
clock.delay(250_u32.milliseconds()).unwrap();
154154

155155
led1.set_high()?;
156156
led2.set_low()?;
157157
led3.set_low()?;
158158
led4.set_high()?;
159-
clock.delay(250_u32.milliseconds());
159+
clock.delay(250_u32.milliseconds()).unwrap();
160160
}
161161
}

src/clock.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{time_int::TimeInt, Duration, Instant, Period};
1+
//! The `Clock` trait can be implemented over hardware timers or other time-keeping device
2+
3+
use crate::{time_int::TimeInt, Duration, Error, Instant, Period};
24
use core::convert::TryFrom;
35

46
/// An abstraction for time-keeping items such as hardware timers
@@ -10,15 +12,16 @@ pub trait Clock: Sized {
1012
const PERIOD: Period;
1113

1214
/// Get the current Instant
13-
fn now(&mut self) -> Instant<Self>;
15+
fn now(&self) -> Instant<Self>;
1416

1517
/// Blocking delay
16-
fn delay<Dur: Duration>(&mut self, dur: Dur)
18+
fn delay<Dur: Duration>(&self, dur: Dur)
1719
where
1820
Self::Rep: TryFrom<Dur::Rep>,
1921
{
20-
let start = self.now();
22+
let start = self.now()?;
2123
let end = start + dur;
22-
while self.now() < end {}
24+
while self.now()? < end {}
25+
Ok(())
2326
}
2427
}

src/instant.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<Clock: crate::Clock> Instant<Clock> {
3838
/// type Rep = u32;
3939
/// const PERIOD: Period = <Period>::new(1, 1_000);
4040
/// // ...
41-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
41+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
4242
/// }
4343
///
4444
/// assert_eq!(Instant::<Clock>::new(5).duration_since::<Microseconds<u64>>(&Instant::<Clock>::new(3)),
@@ -74,7 +74,7 @@ impl<Clock: crate::Clock> Instant<Clock> {
7474
/// type Rep = u32;
7575
/// const PERIOD: Period =<Period>::new(1, 1_000);
7676
/// // ...
77-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
77+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
7878
/// }
7979
///
8080
/// assert_eq!(Instant::<Clock>::new(5).duration_until::<Microseconds<u64>>(&Instant::<Clock>::new(7)),
@@ -138,7 +138,7 @@ impl<Clock: crate::Clock> PartialOrd for Instant<Clock> {
138138
/// type Rep = u32;
139139
/// const PERIOD: Period =<Period>::new(1, 1_000);
140140
/// // ...
141-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
141+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
142142
/// }
143143
///
144144
/// assert!(Instant::<Clock>::new(5) > Instant::<Clock>::new(3));
@@ -178,7 +178,7 @@ where
178178
/// type Rep = u32;
179179
/// const PERIOD: Period =<Period>::new(1, 1_000);
180180
/// // ...
181-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
181+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
182182
/// }
183183
///
184184
/// Instant::<Clock>::new(1) + Seconds(u32::MAX);
@@ -194,7 +194,7 @@ where
194194
/// type Rep = u32;
195195
/// const PERIOD: Period =<Period>::new(1, 1_000);
196196
/// // ...
197-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
197+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
198198
/// }
199199
///
200200
/// let _ = Instant::<Clock>::new(0) + Milliseconds(i32::MAX as u32 + 1);
@@ -211,7 +211,7 @@ where
211211
/// type Rep = u32;
212212
/// const PERIOD: Period =<Period>::new(1, 1_000);
213213
/// // ...
214-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
214+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
215215
/// }
216216
///
217217
/// assert_eq!(Instant::<Clock>::new(1) + Seconds(3_u32), Instant::<Clock>::new(3_001));
@@ -251,7 +251,7 @@ where
251251
/// type Rep = u32;
252252
/// const PERIOD: Period =<Period>::new(1, 1_000);
253253
/// // ...
254-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
254+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
255255
/// }
256256
///
257257
/// Instant::<Clock>::new(1) - Seconds(u32::MAX);
@@ -267,7 +267,7 @@ where
267267
/// type Rep = u32;
268268
/// const PERIOD: Period = <Period>::new(1, 1_000);
269269
/// // ...
270-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
270+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
271271
/// }
272272
///
273273
/// let _ = Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32 + 1);
@@ -284,7 +284,7 @@ where
284284
/// type Rep = u32;
285285
/// const PERIOD: Period =<Period>::new(1, 1_000);
286286
/// // ...
287-
/// # fn now(&mut self) -> Instant<Self> {unimplemented!()}
287+
/// # fn now(&self) -> Instant<Self> {unimplemented!()}
288288
/// }
289289
///
290290
/// assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700_u32), Instant::<Clock>::new(100));
@@ -315,7 +315,7 @@ mod tests {
315315
type Rep = u32;
316316
const PERIOD: Period = <Period>::new(1, 1_000);
317317

318-
fn now(&mut self) -> Instant<Self> {
318+
fn now(&self) -> Instant<Self> {
319319
unimplemented!()
320320
}
321321
}

src/lib.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@
4646
//! type Rep = u64;
4747
//! const PERIOD: Period = <Period>::new(1, 16_000_000);
4848
//!
49-
//! fn now(&mut self) -> Instant<Self> {
49+
//! fn now(&self) -> Instant<Self> {
5050
//! // ...
5151
//! # unimplemented!()
5252
//! }
5353
//! }
5454
//!
5555
//! let mut clock = SomeClock;
56-
//! let instant1 = clock.now();
56+
//! let instant1 = clock.now().unwrap();
5757
//! // ...
58-
//! let instant2 = clock.now();
58+
//! let instant2 = clock.now().unwrap();
5959
//! assert!(instant1 < instant2); // instant1 is *before* instant2
6060
//!
6161
//! // duration is the difference between the instances
@@ -70,7 +70,7 @@
7070
#![warn(missing_docs)]
7171
#![deny(intra_doc_link_resolution_failure)]
7272

73-
mod clock;
73+
pub mod clock;
7474
mod duration;
7575
mod frequency;
7676
mod instant;
@@ -104,6 +104,26 @@ pub mod units {
104104
pub use crate::frequency::units::*;
105105
}
106106

107+
/// Time-related error
108+
#[non_exhaustive]
109+
#[derive(Debug, Eq, PartialEq)]
110+
pub enum Error {
111+
/// Communication with the clock failed
112+
ClockInterfaceFailure,
113+
/// The clock has not been started
114+
ClockNotStarted,
115+
/// Reading an `Instant` from the clock failed for some reason
116+
UnableToReadFromClock,
117+
/// Negative `Duration`s are not supported
118+
NegativeDurationNotAllowed,
119+
/// An integer overflow was detected
120+
Overflow,
121+
/// An integer underflow was detected
122+
Underflow,
123+
/// A divide-by-zero was detected
124+
DivByZero,
125+
}
126+
107127
#[cfg(test)]
108128
#[allow(unused_imports)]
109129
mod tests {
@@ -115,26 +135,25 @@ mod tests {
115135
};
116136
use time::{traits::*, units::*};
117137

118-
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
119138
struct MockClock64;
120139

121140
impl time::Clock for MockClock64 {
122141
type Rep = u64;
123142
const PERIOD: time::Period = <time::Period>::new(1, 64_000_000);
124143

125-
fn now(&mut self) -> time::Instant<Self> {
144+
fn now(&self) -> time::Instant<Self> {
126145
time::Instant::new(128_000_000)
127146
}
128147
}
129148

130-
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
149+
#[derive(Debug)]
131150
struct MockClock32;
132151

133152
impl time::Clock for MockClock32 {
134153
type Rep = u32;
135154
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
136155

137-
fn now(&mut self) -> time::Instant<Self> {
156+
fn now(&self) -> time::Instant<Self> {
138157
time::Instant::new(32_000_000)
139158
}
140159
}
@@ -144,13 +163,16 @@ mod tests {
144163
u32: TryFrom<Clock::Rep>,
145164
Clock::Rep: TryFrom<u32>,
146165
{
147-
assert_eq!(clock.now().duration_since_epoch(), Ok(Seconds(2_u32)));
166+
assert_eq!(
167+
clock.now().unwrap().duration_since_epoch(),
168+
Ok(Seconds(2_u32))
169+
);
148170
}
149171

150172
#[test]
151173
fn common_types() {
152-
let then = MockClock32.now();
153-
let now = MockClock32.now();
174+
let then = MockClock32.now().unwrap();
175+
let now = MockClock32.now().unwrap();
154176

155177
let mut clock64 = MockClock64 {};
156178
let mut clock32 = MockClock32 {};
@@ -163,6 +185,11 @@ mod tests {
163185
assert!(then < now);
164186
}
165187

188+
#[test]
189+
fn clock_error() {
190+
assert_eq!(BadClock.now(), Err(time::Error::UnableToReadFromClock));
191+
}
192+
166193
struct Timestamp<Clock>(time::Instant<Clock>)
167194
where
168195
Clock: time::Clock;

0 commit comments

Comments
 (0)