diff --git a/apps/vault/src/shims.rs b/apps/vault/src/shims.rs new file mode 100644 index 000000000..c2d13ce43 --- /dev/null +++ b/apps/vault/src/shims.rs @@ -0,0 +1,157 @@ +// shims for Tock calls, to avoid having to recode some fairly pervasive patterns in OpenSK + + +#[derive(Copy, Clone, Debug)] +pub struct ClockValue { + num_ticks: isize, + clock_frequency: ClockFrequency, +} + +impl ClockValue { + pub const fn new(num_ticks: isize, clock_hz: usize) -> ClockValue { + ClockValue { + num_ticks, + clock_frequency: ClockFrequency { hz: clock_hz }, + } + } + + pub fn num_ticks(&self) -> isize { + self.num_ticks + } + + // Computes (value * factor) / divisor, even when value * factor >= isize::MAX. + fn scale_int(value: isize, factor: isize, divisor: isize) -> isize { + // As long as isize is not i64, this should be fine. If not, this is an alternative: + // factor * (value / divisor) + ((value % divisor) * factor) / divisor + ((value as i64 * factor as i64) / divisor as i64) as isize + } + + pub fn ms(&self) -> isize { + ClockValue::scale_int(self.num_ticks, 1000, self.clock_frequency.hz() as isize) + } + + pub fn ms_f64(&self) -> f64 { + 1000.0 * (self.num_ticks as f64) / (self.clock_frequency.hz() as f64) + } + + pub fn wrapping_add(self, duration: Duration) -> ClockValue { + // This is a precision preserving formula for scaling an isize. + let duration_ticks = + ClockValue::scale_int(duration.ms, self.clock_frequency.hz() as isize, 1000); + ClockValue { + num_ticks: self.num_ticks.wrapping_add(duration_ticks), + clock_frequency: self.clock_frequency, + } + } + + pub fn wrapping_sub(self, other: ClockValue) -> Option> { + if self.clock_frequency == other.clock_frequency { + let clock_duration = ClockValue { + num_ticks: self.num_ticks - other.num_ticks, + clock_frequency: self.clock_frequency, + }; + Some(Duration::from_ms(clock_duration.ms())) + } else { + None + } + } +} + + +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Duration { + ms: T, +} + +impl Duration { + pub const fn from_ms(ms: T) -> Duration { + Duration { ms } + } +} + +impl Duration +where + T: Copy, +{ + pub fn ms(&self) -> T { + self.ms + } +} + +impl Sub for Duration +where + T: Sub, +{ + type Output = Duration; + + fn sub(self, other: Duration) -> Duration { + Duration { + ms: self.ms - other.ms, + } + } +} + +#[derive(Copy, Clone, Debug)] +pub struct Timestamp { + ms: T, +} + +impl Timestamp { + pub const fn from_ms(ms: T) -> Timestamp { + Timestamp { ms } + } +} + +impl Timestamp +where + T: Copy, +{ + pub fn ms(&self) -> T { + self.ms + } +} + +impl Timestamp { + pub fn from_clock_value(value: ClockValue) -> Timestamp { + Timestamp { ms: value.ms() } + } +} + +impl Timestamp { + pub fn from_clock_value(value: ClockValue) -> Timestamp { + Timestamp { ms: value.ms_f64() } + } +} + +impl Sub for Timestamp +where + T: Sub, +{ + type Output = Duration; + + fn sub(self, other: Timestamp) -> Duration { + Duration::from_ms(self.ms - other.ms) + } +} + +impl Add> for Timestamp +where + T: Copy + Add, +{ + type Output = Timestamp; + + fn add(self, duration: Duration) -> Timestamp { + Timestamp { + ms: self.ms + duration.ms(), + } + } +} + +impl AddAssign> for Timestamp +where + T: Copy + AddAssign, +{ + fn add_assign(&mut self, duration: Duration) { + self.ms += duration.ms(); + } +}