Skip to content

Commit 114534b

Browse files
committed
Make timer traits fallible
1 parent ab070b4 commit 114534b

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/lib.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
//! // ..
104104
//!
105105
//! /// "waits" until the count down is over
106-
//! fn wait(&mut self) -> nb::Result<(), Infallible>;
106+
//! fn try_wait(&mut self) -> nb::Result<(), Infallible>;
107107
//! }
108108
//!
109109
//! # fn main() {}
@@ -257,7 +257,7 @@
257257
//! use stm32f30x_hal::{Led, Serial1, Timer6};
258258
//! use std::convert::Infallible;
259259
//!
260-
//! /// `futures` version of `CountDown.wait`
260+
//! /// `futures` version of `CountDown.try_wait`
261261
//! ///
262262
//! /// This returns a future that must be polled to completion
263263
//! fn wait<T>(mut timer: T) -> impl Future<Item = T, Error = Infallible>
@@ -266,7 +266,7 @@
266266
//! {
267267
//! let mut timer = Some(timer);
268268
//! future::poll_fn(move || {
269-
//! try_nb!(timer.as_mut().unwrap().wait());
269+
//! try_nb!(timer.as_mut().unwrap().try_wait());
270270
//!
271271
//! Ok(Async::Ready(timer.take().unwrap()))
272272
//! })
@@ -349,13 +349,13 @@
349349
//! }
350350
//!
351351
//! # mod stm32f30x_hal {
352-
//! # use std::convert::Infallible;
352+
//! # use core::convert::Infallible;
353353
//! # pub struct Timer6;
354354
//! # impl ::hal::timer::CountDown for Timer6 {
355355
//! # type Time = ();
356356
//! #
357-
//! # fn start<T>(&mut self, _: T) where T: Into<()> {}
358-
//! # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
357+
//! # fn try_start<T>(&mut self, _: T) -> Result<(), Infallible> where T: Into<()> {}
358+
//! # fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
359359
//! # }
360360
//! #
361361
//! # pub struct Serial1;
@@ -509,22 +509,23 @@
509509
//!
510510
//! use hal::prelude::*;
511511
//!
512-
//! enum Error<E> {
512+
//! enum Error<SE, TE> {
513513
//! /// Serial interface error
514-
//! Serial(E),
515-
//! TimedOut,
514+
//! Serial(SE),
515+
//! /// Timeout error
516+
//! TimedOut(TE),
516517
//! }
517518
//!
518519
//! fn read_with_timeout<S, T>(
519520
//! serial: &mut S,
520521
//! timer: &mut T,
521522
//! timeout: T::Time,
522-
//! ) -> Result<u8, Error<S::Error>>
523+
//! ) -> Result<u8, Error<S::Error, T::Error>>
523524
//! where
524-
//! T: hal::timer::CountDown,
525+
//! T: hal::timer::CountDown<Error = ()>,
525526
//! S: hal::serial::Read<u8>,
526527
//! {
527-
//! timer.start(timeout);
528+
//! timer.try_start(timeout).map_err(Error::TimedOut)?;
528529
//!
529530
//! loop {
530531
//! match serial.read() {
@@ -536,16 +537,16 @@
536537
//! Ok(byte) => return Ok(byte),
537538
//! }
538539
//!
539-
//! match timer.wait() {
540+
//! match timer.try_wait() {
540541
//! Err(nb::Error::Other(e)) => {
541-
//! // The error type specified by `timer.wait()` is `!`, which
542+
//! // The error type specified by `timer.try_wait()` is `!`, which
542543
//! // means no error can actually occur. The Rust compiler
543544
//! // still forces us to provide this match arm, though.
544545
//! unreachable!()
545546
//! },
546547
//! // no timeout yet, try again
547548
//! Err(nb::Error::WouldBlock) => continue,
548-
//! Ok(()) => return Err(Error::TimedOut),
549+
//! Ok(()) => return Err(Error::TimedOut(())),
549550
//! }
550551
//! }
551552
//! }
@@ -949,8 +950,8 @@ pub trait PwmPin {
949950
///
950951
///
951952
/// let before = qei.try_count().unwrap();
952-
/// timer.start(1.s());
953-
/// block!(timer.wait());
953+
/// timer.try_start(1.s()).unwrap();
954+
/// block!(timer.try_wait());
954955
/// let after = qei.try_count().unwrap();
955956
///
956957
/// let speed = after.wrapping_sub(before);
@@ -965,14 +966,14 @@ pub trait PwmPin {
965966
/// # impl hal::Qei for Qei1 {
966967
/// # type Error = Infallible;
967968
/// # type Count = u16;
968-
/// # fn try_count(&self) -> Result<u16, Self::Error> { 0 }
969+
/// # fn try_count(&self) -> Result<u16, Self::Error> { Ok(0) }
969970
/// # fn try_direction(&self) -> Result<::hal::Direction, Self::Error> { unimplemented!() }
970971
/// # }
971972
/// # struct Timer6;
972973
/// # impl hal::timer::CountDown for Timer6 {
973974
/// # type Time = Seconds;
974-
/// # fn start<T>(&mut self, _: T) where T: Into<Seconds> {}
975-
/// # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
975+
/// # fn try_start<T>(&mut self, _: T) -> Result<(), Infallible> where T: Into<Seconds> { Ok(()) }
976+
/// # fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
976977
/// # }
977978
/// ```
978979
#[cfg(feature = "unproven")]

src/timer.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Timers
22
3-
use core::convert::Infallible;
43
use nb;
54

65
/// A count down timer
76
///
87
/// # Contract
98
///
10-
/// - `self.start(count); block!(self.wait());` MUST block for AT LEAST the time specified by
9+
/// - `self.start(count); block!(self.try_wait());` MUST block for AT LEAST the time specified by
1110
/// `count`.
1211
///
1312
/// *Note* that the implementer doesn't necessarily have to be a *downcounting* timer; it could also
@@ -35,12 +34,12 @@ use nb;
3534
/// };
3635
///
3736
/// Led.on();
38-
/// timer.start(1.s());
39-
/// block!(timer.wait()); // blocks for 1 second
37+
/// timer.try_start(1.s()).unwrap();
38+
/// block!(timer.try_wait()); // blocks for 1 second
4039
/// Led.off();
4140
/// }
4241
///
43-
/// # use std::convert::Infallible;
42+
/// # use core::convert::Infallible;
4443
/// # struct Seconds(u32);
4544
/// # trait U32Ext { fn s(self) -> Seconds; }
4645
/// # impl U32Ext for u32 { fn s(self) -> Seconds { Seconds(self) } }
@@ -51,17 +50,23 @@ use nb;
5150
/// # }
5251
/// # struct Timer6;
5352
/// # impl hal::timer::CountDown for Timer6 {
53+
/// # type Error = Infallible;
5454
/// # type Time = Seconds;
55-
/// # fn start<T>(&mut self, _: T) where T: Into<Seconds> {}
56-
/// # fn wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
55+
/// # fn try_start<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<Seconds> { Ok(()) }
56+
/// # fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
5757
/// # }
5858
/// ```
5959
pub trait CountDown {
60+
/// An enumeration of `CountDown` errors.
61+
///
62+
/// For infallible implementations, will be `Infallible`
63+
type Error;
64+
6065
/// The unit of time used by this timer
6166
type Time;
6267

6368
/// Starts a new count down
64-
fn start<T>(&mut self, count: T)
69+
fn try_start<T>(&mut self, count: T) -> Result<(), Self::Error>
6570
where
6671
T: Into<Self::Time>;
6772

@@ -71,24 +76,21 @@ pub trait CountDown {
7176
///
7277
/// - If `Self: Periodic`, the timer will start a new count down right after the last one
7378
/// finishes.
74-
/// - Otherwise the behavior of calling `wait` after the last call returned `Ok` is UNSPECIFIED.
79+
/// - Otherwise the behavior of calling `try_wait` after the last call returned `Ok` is UNSPECIFIED.
7580
/// Implementers are suggested to panic on this scenario to signal a programmer error.
76-
fn wait(&mut self) -> nb::Result<(), Infallible>;
81+
fn try_wait(&mut self) -> nb::Result<(), Self::Error>;
7782
}
7883

7984
/// Marker trait that indicates that a timer is periodic
8085
pub trait Periodic {}
8186

8287
/// Trait for cancelable countdowns.
8388
pub trait Cancel: CountDown {
84-
/// Error returned when a countdown can't be canceled.
85-
type Error;
86-
8789
/// Tries to cancel this countdown.
8890
///
8991
/// # Errors
9092
///
9193
/// An error will be returned if the countdown has already been canceled or was never started.
9294
/// An error is also returned if the countdown is not `Periodic` and has already expired.
93-
fn cancel(&mut self) -> Result<(), Self::Error>;
95+
fn try_cancel(&mut self) -> Result<(), Self::Error>;
9496
}

0 commit comments

Comments
 (0)