Skip to main content

crux_time/protocol/
mod.rs

1#[cfg(feature = "chrono")]
2pub mod chrono;
3pub mod duration;
4pub mod instant;
5
6use crux_core::capability::Operation;
7use facet::Facet;
8use serde::{Deserialize, Serialize};
9
10pub use duration::Duration;
11pub use instant::Instant;
12
13#[derive(Facet, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15#[repr(C)]
16pub enum TimeRequest {
17    Now,
18    NotifyAt { id: TimerId, instant: Instant },
19    NotifyAfter { id: TimerId, duration: Duration },
20    Clear { id: TimerId },
21}
22
23#[derive(Facet, Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub struct TimerId(pub usize);
25
26#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28#[repr(C)]
29pub enum TimeResponse {
30    Now { instant: Instant },
31    InstantArrived { id: TimerId },
32    DurationElapsed { id: TimerId },
33    Cleared { id: TimerId },
34}
35
36impl Operation for TimeRequest {
37    type Output = TimeResponse;
38
39    #[cfg(feature = "typegen")]
40    fn register_types(
41        generator: &mut crux_core::type_generation::serde::TypeGen,
42    ) -> crux_core::type_generation::serde::Result {
43        generator.register_type::<Instant>()?;
44        generator.register_type::<Duration>()?;
45        generator.register_type::<Self>()?;
46        generator.register_type::<Self::Output>()?;
47        Ok(())
48    }
49}