aimdb_executor/
lib.rs

1//! AimDB Executor Traits
2//!
3//! Pure trait definitions for async execution across different runtime environments.
4//! Enables dependency inversion where the core database depends on abstractions
5//! rather than concrete runtime implementations.
6//!
7//! # Design Philosophy
8//!
9//! - **Runtime Agnostic**: No concrete runtime dependencies
10//! - **Simple Trait Structure**: 4 focused traits covering all runtime needs
11//! - **Platform Flexible**: Works across std and no_std environments
12//! - **Zero Dependencies**: Pure trait definitions with minimal coupling
13//!
14//! # Trait Structure
15//!
16//! 1. **`RuntimeAdapter`** - Platform identity and metadata
17//! 2. **`TimeOps`** - Time operations (now, sleep, duration helpers)
18//! 3. **`Logger`** - Structured logging (info, debug, warn, error)
19//! 4. **`Spawn`** - Task spawning with platform-specific tokens
20
21#![cfg_attr(not(feature = "std"), no_std)]
22
23use core::future::Future;
24
25// ============================================================================
26// Error Types
27// ============================================================================
28
29pub type ExecutorResult<T> = Result<T, ExecutorError>;
30
31#[derive(Debug)]
32#[cfg_attr(feature = "std", derive(thiserror::Error))]
33pub enum ExecutorError {
34    #[cfg_attr(feature = "std", error("Spawn failed: {message}"))]
35    SpawnFailed {
36        #[cfg(feature = "std")]
37        message: String,
38        #[cfg(not(feature = "std"))]
39        message: &'static str,
40    },
41
42    #[cfg_attr(feature = "std", error("Runtime unavailable: {message}"))]
43    RuntimeUnavailable {
44        #[cfg(feature = "std")]
45        message: String,
46        #[cfg(not(feature = "std"))]
47        message: &'static str,
48    },
49
50    #[cfg_attr(feature = "std", error("Task join failed: {message}"))]
51    TaskJoinFailed {
52        #[cfg(feature = "std")]
53        message: String,
54        #[cfg(not(feature = "std"))]
55        message: &'static str,
56    },
57}
58
59// ============================================================================
60// Core Traits (Simplified - 4 traits total)
61// ============================================================================
62
63/// Core runtime adapter trait - provides identity
64pub trait RuntimeAdapter: Send + Sync + 'static {
65    fn runtime_name() -> &'static str
66    where
67        Self: Sized;
68}
69
70/// Time operations trait - enables ctx.time() accessor
71pub trait TimeOps: RuntimeAdapter {
72    type Instant: Clone + Send + Sync + core::fmt::Debug + 'static;
73    type Duration: Clone + Send + Sync + core::fmt::Debug + 'static;
74
75    fn now(&self) -> Self::Instant;
76    fn duration_since(
77        &self,
78        later: Self::Instant,
79        earlier: Self::Instant,
80    ) -> Option<Self::Duration>;
81    fn millis(&self, ms: u64) -> Self::Duration;
82    fn secs(&self, secs: u64) -> Self::Duration;
83    fn micros(&self, micros: u64) -> Self::Duration;
84    fn sleep(&self, duration: Self::Duration) -> impl Future<Output = ()> + Send;
85}
86
87/// Logging trait - enables ctx.log() accessor
88pub trait Logger: RuntimeAdapter {
89    fn info(&self, message: &str);
90    fn debug(&self, message: &str);
91    fn warn(&self, message: &str);
92    fn error(&self, message: &str);
93}
94
95/// Task spawning trait - adapter-specific implementation
96pub trait Spawn: RuntimeAdapter {
97    type SpawnToken: Send + 'static;
98    fn spawn<F>(&self, future: F) -> ExecutorResult<Self::SpawnToken>
99    where
100        F: Future<Output = ()> + Send + 'static;
101}
102
103// ============================================================================
104// Convenience Trait Bundle
105// ============================================================================
106
107/// Complete runtime trait bundle
108pub trait Runtime: RuntimeAdapter + TimeOps + Logger + Spawn {
109    fn runtime_info(&self) -> RuntimeInfo
110    where
111        Self: Sized,
112    {
113        RuntimeInfo {
114            name: Self::runtime_name(),
115        }
116    }
117}
118
119// Auto-implement Runtime for any type with all traits
120impl<T> Runtime for T where T: RuntimeAdapter + TimeOps + Logger + Spawn {}
121
122#[derive(Debug, Clone)]
123pub struct RuntimeInfo {
124    pub name: &'static str,
125}