noema-actix-webapi 0.1.0

Actix-web backend runtime on Noema (modules, sqlx, UoW, swagger, WebSocket dispatch)
use std::time::SystemTime;

use noema::core::{Container, Injectable};

/// Injectable clock. Default [`SystemClock`].
pub trait Clock: Send + Sync {
    fn now(&self) -> SystemTime;
}

#[derive(Clone, Copy, Default)]
pub struct SystemClock;

impl Clock for SystemClock {
    fn now(&self) -> SystemTime {
        SystemTime::now()
    }
}

impl Injectable<Container> for SystemClock {
    fn inject(_: &Container) -> Self {
        Self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clock_resolve_is_singleton() {
        let a = noema::resolve::<dyn Clock + Send + Sync>();
        let b = noema::resolve::<dyn Clock + Send + Sync>();
        assert!(std::sync::Arc::ptr_eq(&a, &b));
        let _ = a.now();
    }
}