aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Salzedo <[email protected]>2021-03-21 16:45:53 -0700
committerJoshua Salzedo <[email protected]>2021-03-21 16:45:53 -0700
commitdcdd768e0360683db747906e4780e5470d1961a1 (patch)
tree923355fbe683fc6ff959d6ae5869d76264f2db65
parentd453b9dd95589d95239ea3f86f15da647d253aef (diff)
Add Struct/impl documentation for embassy::time::Instant
-rw-r--r--embassy/src/time/instant.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/embassy/src/time/instant.rs b/embassy/src/time/instant.rs
index 06ab84c75..9a544fc46 100644
--- a/embassy/src/time/instant.rs
+++ b/embassy/src/time/instant.rs
@@ -6,6 +6,7 @@ use super::{now, Duration};
6 6
7#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 7#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))] 8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9/// An Instant in time, based on the MCU's clock ticks since startup.
9pub struct Instant { 10pub struct Instant {
10 ticks: u64, 11 ticks: u64,
11} 12}
@@ -14,44 +15,54 @@ impl Instant {
14 pub const MIN: Instant = Instant { ticks: u64::MIN }; 15 pub const MIN: Instant = Instant { ticks: u64::MIN };
15 pub const MAX: Instant = Instant { ticks: u64::MAX }; 16 pub const MAX: Instant = Instant { ticks: u64::MAX };
16 17
18 /// Returns an Instant representing the current time.
17 pub fn now() -> Instant { 19 pub fn now() -> Instant {
18 Instant { ticks: now() } 20 Instant { ticks: now() }
19 } 21 }
20 22
23 /// Instant as clock ticks since MCU start.
21 pub const fn from_ticks(ticks: u64) -> Self { 24 pub const fn from_ticks(ticks: u64) -> Self {
22 Self { ticks } 25 Self { ticks }
23 } 26 }
24 27
28 /// Instant as milliseconds since MCU start.
25 pub const fn from_millis(millis: u64) -> Self { 29 pub const fn from_millis(millis: u64) -> Self {
26 Self { 30 Self {
27 ticks: millis * TICKS_PER_SECOND as u64 / 1000, 31 ticks: millis * TICKS_PER_SECOND as u64 / 1000,
28 } 32 }
29 } 33 }
30 34 /// Instant representing seconds since MCU start.
31 pub const fn from_secs(seconds: u64) -> Self { 35 pub const fn from_secs(seconds: u64) -> Self {
32 Self { 36 Self {
33 ticks: seconds * TICKS_PER_SECOND as u64, 37 ticks: seconds * TICKS_PER_SECOND as u64,
34 } 38 }
35 } 39 }
36 40
41 /// Instant as ticks since MCU start.
42
37 pub const fn as_ticks(&self) -> u64 { 43 pub const fn as_ticks(&self) -> u64 {
38 self.ticks 44 self.ticks
39 } 45 }
46 /// Instant as seconds since MCU start.
40 47
41 pub const fn as_secs(&self) -> u64 { 48 pub const fn as_secs(&self) -> u64 {
42 self.ticks / TICKS_PER_SECOND as u64 49 self.ticks / TICKS_PER_SECOND as u64
43 } 50 }
51 /// Instant as miliseconds since MCU start.
44 52
45 pub const fn as_millis(&self) -> u64 { 53 pub const fn as_millis(&self) -> u64 {
46 self.ticks * 1000 / TICKS_PER_SECOND as u64 54 self.ticks * 1000 / TICKS_PER_SECOND as u64
47 } 55 }
48 56
57 /// Duration between this Instant and another Instant
58 /// Panics on over/underflow.
49 pub fn duration_since(&self, earlier: Instant) -> Duration { 59 pub fn duration_since(&self, earlier: Instant) -> Duration {
50 Duration { 60 Duration {
51 ticks: self.ticks.checked_sub(earlier.ticks).unwrap(), 61 ticks: self.ticks.checked_sub(earlier.ticks).unwrap(),
52 } 62 }
53 } 63 }
54 64
65 /// Duration between this Instant and another Instant
55 pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> { 66 pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
56 if self.ticks < earlier.ticks { 67 if self.ticks < earlier.ticks {
57 None 68 None
@@ -62,6 +73,8 @@ impl Instant {
62 } 73 }
63 } 74 }
64 75
76 /// Returns the duration since the "earlier" Instant.
77 /// If the "earlier" instant is in the future, the duration is set to zero.
65 pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { 78 pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
66 Duration { 79 Duration {
67 ticks: if self.ticks < earlier.ticks { 80 ticks: if self.ticks < earlier.ticks {
@@ -72,6 +85,7 @@ impl Instant {
72 } 85 }
73 } 86 }
74 87
88 /// Duration elapsed since this Instant.
75 pub fn elapsed(&self) -> Duration { 89 pub fn elapsed(&self) -> Duration {
76 Instant::now() - *self 90 Instant::now() - *self
77 } 91 }