Skip to content

Commit 773e021

Browse files
committed
refactor(Clock): Changed error handling scheme
- Remove the crate-level, monolithic `Error` enum - Add crate-level `Error` trait - Add associated error type to `Clock` trait for implementation-specific errors - Add clock::Error enum with a single variant, `Other(E)` where E is an impl-specific error type
1 parent 30d15f3 commit 773e021

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

examples/nrf52_dk/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ impl SysClock {
3737
impl time::Clock for SysClock {
3838
type Rep = u64;
3939
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
40+
type ImplError = ();
4041

41-
fn now(&self) -> Result<time::Instant<Self>, time::Error> {
42+
fn now(&self) -> Result<time::Instant<Self>, time::clock::Error<Self::ImplError>> {
4243
self.capture_task.tasks_trigger[0].write(|write| unsafe { write.bits(1) });
4344

4445
let ticks =

src/clock.rs

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

6+
/// Potential `Clock` errors
7+
#[non_exhaustive]
8+
#[derive(Debug, Eq, PartialEq)]
9+
pub enum Error<E: crate::Error = ()> {
10+
/// specific implementation error
11+
Other(E),
12+
}
13+
614
/// An abstraction for time-keeping items such as hardware timers
715
pub trait Clock: Sized {
816
/// The type to hold the tick count
@@ -11,17 +19,20 @@ pub trait Clock: Sized {
1119
/// The duration of one clock tick in seconds, AKA the clock precision.
1220
const PERIOD: Period;
1321

22+
/// Implementation-specific error type
23+
type ImplError: crate::Error;
24+
1425
/// Get the current Instant
1526
///
1627
/// # Errors
17-
/// - Error: The current instant was not readable
18-
fn now(&self) -> Result<Instant<Self>, Error>;
28+
/// Implementation-specific error returned through [`Error::Other(ImplError)`]
29+
fn now(&self) -> Result<Instant<Self>, Error<Self::ImplError>>;
1930

2031
/// Blocking delay
2132
///
2233
/// # Errors
23-
/// - Error: The current instant was not readable, actual delay (if any) is unknown
24-
fn delay<Dur: Duration>(&self, dur: Dur) -> Result<(), Error>
34+
/// Implementation-specific error returned through [`Error::Other(ImplError)`]
35+
fn delay<Dur: Duration>(&self, dur: Dur) -> Result<(), Error<Self::ImplError>>
2536
where
2637
Self::Rep: TryFrom<Dur::Rep>,
2738
{

src/instant.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl<Clock: crate::Clock> Instant<Clock> {
3838
/// type Rep = u32;
3939
/// const PERIOD: Period = <Period>::new(1, 1_000);
4040
/// // ...
41-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
41+
/// # type ImplError = ();
42+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
4243
/// }
4344
///
4445
/// assert_eq!(Instant::<Clock>::new(5).duration_since::<Microseconds<u64>>(&Instant::<Clock>::new(3)),
@@ -74,7 +75,8 @@ impl<Clock: crate::Clock> Instant<Clock> {
7475
/// type Rep = u32;
7576
/// const PERIOD: Period =<Period>::new(1, 1_000);
7677
/// // ...
77-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
78+
/// # type ImplError = ();
79+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
7880
/// }
7981
///
8082
/// assert_eq!(Instant::<Clock>::new(5).duration_until::<Microseconds<u64>>(&Instant::<Clock>::new(7)),
@@ -138,7 +140,8 @@ impl<Clock: crate::Clock> PartialOrd for Instant<Clock> {
138140
/// type Rep = u32;
139141
/// const PERIOD: Period =<Period>::new(1, 1_000);
140142
/// // ...
141-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
143+
/// # type ImplError = ();
144+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
142145
/// }
143146
///
144147
/// assert!(Instant::<Clock>::new(5) > Instant::<Clock>::new(3));
@@ -178,7 +181,8 @@ where
178181
/// type Rep = u32;
179182
/// const PERIOD: Period =<Period>::new(1, 1_000);
180183
/// // ...
181-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
184+
/// # type ImplError = ();
185+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
182186
/// }
183187
///
184188
/// Instant::<Clock>::new(1) + Seconds(u32::MAX);
@@ -194,7 +198,8 @@ where
194198
/// type Rep = u32;
195199
/// const PERIOD: Period =<Period>::new(1, 1_000);
196200
/// // ...
197-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
201+
/// # type ImplError = ();
202+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
198203
/// }
199204
///
200205
/// let _ = Instant::<Clock>::new(0) + Milliseconds(i32::MAX as u32 + 1);
@@ -211,7 +216,8 @@ where
211216
/// type Rep = u32;
212217
/// const PERIOD: Period =<Period>::new(1, 1_000);
213218
/// // ...
214-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
219+
/// # type ImplError = ();
220+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
215221
/// }
216222
///
217223
/// assert_eq!(Instant::<Clock>::new(1) + Seconds(3_u32), Instant::<Clock>::new(3_001));
@@ -251,7 +257,8 @@ where
251257
/// type Rep = u32;
252258
/// const PERIOD: Period =<Period>::new(1, 1_000);
253259
/// // ...
254-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
260+
/// # type ImplError = ();
261+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
255262
/// }
256263
///
257264
/// Instant::<Clock>::new(1) - Seconds(u32::MAX);
@@ -267,7 +274,8 @@ where
267274
/// type Rep = u32;
268275
/// const PERIOD: Period = <Period>::new(1, 1_000);
269276
/// // ...
270-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
277+
/// # type ImplError = ();
278+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
271279
/// }
272280
///
273281
/// let _ = Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32 + 1);
@@ -284,7 +292,8 @@ where
284292
/// type Rep = u32;
285293
/// const PERIOD: Period =<Period>::new(1, 1_000);
286294
/// // ...
287-
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {unimplemented!()}
295+
/// # type ImplError = ();
296+
/// # fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {unimplemented!()}
288297
/// }
289298
///
290299
/// assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700_u32), Instant::<Clock>::new(100));
@@ -314,8 +323,9 @@ mod tests {
314323
impl time::Clock for Clock {
315324
type Rep = u32;
316325
const PERIOD: Period = <Period>::new(1, 1_000);
326+
type ImplError = ();
317327

318-
fn now(&self) -> Result<Instant<Self>, time::Error> {
328+
fn now(&self) -> Result<Instant<Self>, time::clock::Error<Self::ImplError>> {
319329
unimplemented!()
320330
}
321331
}

src/lib.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
//! impl embedded_time::Clock for SomeClock {
4646
//! type Rep = u64;
4747
//! const PERIOD: Period = <Period>::new(1, 16_000_000);
48-
//!
49-
//! fn now(&self) -> Result<Instant<Self>, embedded_time::Error> {
48+
//! type ImplError = ();
49+
//!
50+
//! fn now(&self) -> Result<Instant<Self>, embedded_time::clock::Error<Self::ImplError>> {
5051
//! // ...
5152
//! # unimplemented!()
5253
//! }
@@ -78,6 +79,7 @@ mod period;
7879
mod time_int;
7980

8081
pub use clock::Clock;
82+
use core::fmt;
8183
pub use duration::Duration;
8284
pub use instant::Instant;
8385
pub use period::Period;
@@ -104,44 +106,36 @@ pub mod units {
104106
pub use crate::frequency::units::*;
105107
}
106108

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-
}
109+
/// An implementation-specific error
110+
pub trait Error: fmt::Debug {}
111+
impl Error for () {}
126112

127113
#[cfg(test)]
128114
#[allow(unused_imports)]
129115
mod tests {
130-
use crate as time;
116+
use crate::{self as time, clock, traits::*, units::*};
131117
use core::{
132118
convert::TryFrom,
133119
convert::TryInto,
134120
fmt::{self, Formatter},
135121
};
136-
use time::{traits::*, units::*};
122+
123+
#[non_exhaustive]
124+
#[derive(Debug, Eq, PartialEq)]
125+
pub enum ClockImplError {
126+
NotStarted,
127+
}
128+
129+
impl crate::Error for ClockImplError {}
137130

138131
struct MockClock64;
139132

140133
impl time::Clock for MockClock64 {
141134
type Rep = u64;
142135
const PERIOD: time::Period = <time::Period>::new(1, 64_000_000);
136+
type ImplError = ClockImplError;
143137

144-
fn now(&self) -> Result<time::Instant<Self>, time::Error> {
138+
fn now(&self) -> Result<time::Instant<Self>, time::clock::Error<Self::ImplError>> {
145139
Ok(time::Instant::new(128_000_000))
146140
}
147141
}
@@ -152,8 +146,9 @@ mod tests {
152146
impl time::Clock for MockClock32 {
153147
type Rep = u32;
154148
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
149+
type ImplError = ClockImplError;
155150

156-
fn now(&self) -> Result<time::Instant<Self>, time::Error> {
151+
fn now(&self) -> Result<time::Instant<Self>, time::clock::Error<Self::ImplError>> {
157152
Ok(time::Instant::new(32_000_000))
158153
}
159154
}
@@ -164,9 +159,10 @@ mod tests {
164159
impl time::Clock for BadClock {
165160
type Rep = u32;
166161
const PERIOD: time::Period = time::Period::new(1, 16_000_000);
162+
type ImplError = ClockImplError;
167163

168-
fn now(&self) -> Result<time::Instant<Self>, time::Error> {
169-
Err(time::Error::UnableToReadFromClock)
164+
fn now(&self) -> Result<time::Instant<Self>, time::clock::Error<Self::ImplError>> {
165+
Err(time::clock::Error::Other(ClockImplError::NotStarted))
170166
}
171167
}
172168

@@ -199,7 +195,10 @@ mod tests {
199195

200196
#[test]
201197
fn clock_error() {
202-
assert_eq!(BadClock.now(), Err(time::Error::UnableToReadFromClock));
198+
assert_eq!(
199+
BadClock.now(),
200+
Err(time::clock::Error::Other(ClockImplError::NotStarted))
201+
);
203202
}
204203

205204
struct Timestamp<Clock>(time::Instant<Clock>)

0 commit comments

Comments
 (0)