stak_time/
primitive_set.rs1mod error;
2mod primitive;
3
4pub use self::{error::PrimitiveError, primitive::Primitive};
5use crate::Clock;
6use stak_vm::{Error, Heap, Memory, Number, PrimitiveSet};
7use winter_maybe_async::maybe_async;
8
9pub struct TimePrimitiveSet<T: Clock> {
11 clock: T,
12}
13
14impl<T: Clock> TimePrimitiveSet<T> {
15 pub const fn new(clock: T) -> Self {
17 Self { clock }
18 }
19}
20
21impl<T: Clock, H: Heap> PrimitiveSet<H> for TimePrimitiveSet<T> {
22 type Error = PrimitiveError;
23
24 #[maybe_async]
25 fn operate(&mut self, memory: &mut Memory<H>, primitive: usize) -> Result<(), Self::Error> {
26 match primitive {
27 Primitive::CURRENT_JIFFY => {
28 memory.push(
29 Number::from_i64(
30 self.clock
31 .current_jiffy()
32 .map_err(|_| PrimitiveError::CurrentJiffy)?
33 as _,
34 )
35 .into(),
36 )?;
37 }
38 Primitive::JIFFIES_PER_SECOND => {
39 memory.push(Number::from_i64(self.clock.jiffies_per_second() as _).into())?;
40 }
41 _ => return Err(Error::IllegalPrimitive.into()),
42 }
43
44 Ok(())
45 }
46}