Conversation
| @@ -0,0 +1,70 @@ | |||
| trait Timer: Sized { | |||
| fn start(milliseconds: u32) -> Self; | |||
| fn has_elapsed(self) -> Option<Self>; | |||
There was a problem hiding this comment.
Let's add some documentation for those 2 functions.
There was a problem hiding this comment.
Documentation in Rust use three slashes:
/// Creates and starts a timer.
fn start(milliseconds: u32) -> Self;
/// Returns the remaining timer of a timer.
///
/// In particular, if the timer has elapsed, `None` is returned.
fn has_elapsed(self) -> Option<Self>;And actually I would rename has_elapsed to is_running, because Some usually means true.
ia0
left a comment
There was a problem hiding this comment.
Looks good. I guess we should try using it in this PR directly. Otherwise we don't know if it works.
| @@ -0,0 +1,91 @@ | |||
| // Copyright 2019 Google LLC | |||
There was a problem hiding this comment.
You can change the year to 2022. This is all new code.
| @@ -0,0 +1,70 @@ | |||
| trait Timer: Sized { | |||
| fn start(milliseconds: u32) -> Self; | |||
| fn has_elapsed(self) -> Option<Self>; | |||
There was a problem hiding this comment.
Documentation in Rust use three slashes:
/// Creates and starts a timer.
fn start(milliseconds: u32) -> Self;
/// Returns the remaining timer of a timer.
///
/// In particular, if the timer has elapsed, `None` is returned.
fn has_elapsed(self) -> Option<Self>;And actually I would rename has_elapsed to is_running, because Some usually means true.
| pub fn should_wink(&self, now: CtapInstant) -> bool { | ||
| self.wink_permission.is_granted(now) | ||
| pub fn should_wink(&self) -> bool { | ||
| self.wink_permission.is_some() && self.wink_permission.unwrap().has_elapsed().is_some() |
There was a problem hiding this comment.
Interesting. First I'm surprised this compiles. Second, the signature of should_wink might be problematic with the has_elapsed API. I see at least 2 options:
should_winkonly returns the status since last update, i.e.self.wink_permission.is_some()(my preference)should_winkis&mut selfand also updates the timer.
| pub fn update_wink_timeout(&mut self, now: CtapInstant) { | ||
| self.wink_permission = self.wink_permission.check_expiration(now); | ||
| pub fn update_wink_timeout(&mut self) { | ||
| self.wink_permission = Some(LibtockAlarmTimer::start(Self::WINK_TIMEOUT_DURATION)); |
There was a problem hiding this comment.
This looks wrong to me. I would expect this to update the timer, not reset it.
self.wink_permission = self.wink_permission.and_then(LibtockAlarmTimer::has_expired);
src/ctap/mod.rs
Outdated
| pub fn new_reset() -> StatefulPermission { | ||
| StatefulPermission { | ||
| permission: TimedPermission::granted(now, RESET_TIMEOUT_DURATION), | ||
| timer: Timer::start(30000), |
| pub fn clear_timer(&mut self, now: CtapInstant) { | ||
| if !self.permission.is_granted(now) { | ||
| pub fn clear_timer(&mut self) { | ||
| if self.timer.is_some() && self.timer.unwrap().has_elapsed() { |
There was a problem hiding this comment.
We should first update the timer, then check if elapsed.
self.timer = self.timer.and_then(has_expired);
if self.timer.is_none() { self.clear(); }| /// NFC only allows 19.8 seconds. | ||
| /// TODO(#15) multiplex over transports, add NFC | ||
| const INITIAL_USAGE_TIME_LIMIT: Milliseconds<ClockInt> = Milliseconds(30000 as ClockInt); | ||
| const INITIAL_USAGE_TIME_LIMIT:u32 = 30000_u32; |
src/ctap/token_state.rs
Outdated
| } | ||
| self.usage_timer = self.usage_timer.check_expiration(now); | ||
| if !self.usage_timer.is_granted(now) { | ||
| if self.usage_timer.is_none() || self.usage_timer.unwrap().has_elapsed().is_none() { |
There was a problem hiding this comment.
This should also update the timer. We could actually provide a function for convenience:
/// Updates the expiration of a timer.
pub fn update_timer<T: Timer>(timer: &mut Option<T>) {
*timer = core::mem::replace(timer, None).and_then(T::has_expired);
}8a718f9 to
22f829a
Compare
22f829a to
8922b34
Compare
|
Picked up by #596 |
Create Timer trait for future use. Implement Timer using libtock core.