Skip to content

Commit 4a9a098

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

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

examples/nrf52_dk/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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 =

src/clock.rs

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

@@ -10,10 +12,10 @@ 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
{

src/instant.rs

+10-10
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
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
//! }
@@ -122,7 +122,7 @@ mod tests {
122122
type Rep = u64;
123123
const PERIOD: time::Period = <time::Period>::new(1, 64_000_000);
124124

125-
fn now(&mut self) -> time::Instant<Self> {
125+
fn now(&self) -> time::Instant<Self> {
126126
time::Instant::new(128_000_000)
127127
}
128128
}
@@ -134,7 +134,7 @@ mod tests {
134134
type Rep = u32;
135135
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
136136

137-
fn now(&mut self) -> time::Instant<Self> {
137+
fn now(&self) -> time::Instant<Self> {
138138
time::Instant::new(32_000_000)
139139
}
140140
}

0 commit comments

Comments
 (0)