vibe-ready 0.2.0

Composable runtime, logging, scheduling, and storage foundations for vibe-coding Rust projects.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use crate::api::engine_error::{VibeEngineError, VibeEngineErrorCode};
use crate::log::log_def::DESC;
use crate::log_e;
use std::any::Any;
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use threadpool::ThreadPool;
use tokio::runtime::{Handle, Runtime};
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot;

pub(crate) type VibeEngineTask = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;

fn panic_payload_message(payload: &Box<dyn Any + Send + 'static>) -> String {
    if let Some(s) = payload.downcast_ref::<&str>() {
        (*s).to_string()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "unknown panic payload".to_string()
    }
}

#[derive(Clone)]
/// Executes user callbacks on the engine callback thread pool.
///
/// Use this when an integration receives events on an async/runtime thread but
/// wants user callbacks to run on the configured callback pool.
///
/// # Examples
///
/// ```no_run
/// use vibe_ready::{VibeEngine, VibeEngineConfig, VibeResult};
///
/// # fn demo() -> VibeResult<()> {
/// let engine = VibeEngine::create(VibeEngineConfig::builder().build())?;
/// let callbacks = engine.executor().callback();
/// callbacks.execute(|| println!("callback ran"));
/// # engine.destroy_with_timeout(std::time::Duration::from_secs(1))?;
/// # Ok(())
/// # }
/// ```
pub struct VibeCallbackExecutor {
    cb_pool: ThreadPool,
}

impl VibeCallbackExecutor {
    pub(crate) fn new(cb_pool: ThreadPool) -> Self {
        Self { cb_pool }
    }

    /// Executes a zero-argument callback on the callback pool.
    ///
    /// # Returns
    ///
    /// This method returns `()` after queuing the callback.
    pub fn execute<F>(&self, cb: F)
    where
        F: FnOnce() + Send + 'static,
    {
        self.cb_pool.execute(cb);
    }

    /// Converts a one-shot single-argument callback into a pool-dispatched callback.
    ///
    /// # Returns
    ///
    /// A `FnOnce(R)` wrapper that moves the argument into the callback pool.
    pub fn once<F, R>(&self, cb: F) -> impl FnOnce(R) + Send + 'static
    where
        F: FnOnce(R) + Send + 'static,
        R: Send + 'static,
    {
        let executor = self.clone();
        move |value| executor.execute(move || cb(value))
    }

    /// Converts a one-shot two-argument callback into a pool-dispatched callback.
    ///
    /// # Returns
    ///
    /// A `FnOnce(R1, R2)` wrapper that moves both arguments into the callback pool.
    pub fn once2<F, R1, R2>(&self, cb: F) -> impl FnOnce(R1, R2) + Send + 'static
    where
        F: FnOnce(R1, R2) + Send + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        move |value1, value2| executor.execute(move || cb(value1, value2))
    }

    /// Converts a one-shot three-argument callback into a pool-dispatched callback.
    ///
    /// # Returns
    ///
    /// A `FnOnce(R1, R2, R3)` wrapper that moves all arguments into the callback pool.
    pub fn once3<F, R1, R2, R3>(&self, cb: F) -> impl FnOnce(R1, R2, R3) + Send + 'static
    where
        F: FnOnce(R1, R2, R3) + Send + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
    {
        let executor = self.clone();
        move |value1, value2, value3| executor.execute(move || cb(value1, value2, value3))
    }

    /// Returns a boxed one-shot three-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `FnOnce` that dispatches the original callback on the pool.
    pub fn once3_boxed<F, R1, R2, R3>(
        &self,
        cb: F,
    ) -> Box<dyn FnOnce(R1, R2, R3) + Send + Sync + 'static>
    where
        F: FnOnce(R1, R2, R3) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
    {
        let executor = self.clone();
        Box::new(move |value1, value2, value3| {
            executor.execute(move || cb(value1, value2, value3));
        })
    }

    /// Returns a boxed zero-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn` that can be cloned or stored by APIs expecting trait objects.
    pub fn boxed_fn0<F>(&self, cb: F) -> Box<dyn Fn() + Send + Sync + 'static>
    where
        F: Fn() + Send + Sync + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move || {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone());
        })
    }

    /// Returns a boxed one-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(R)` that dispatches each invocation on the callback pool.
    pub fn boxed_fn<F, R>(&self, cb: F) -> Box<dyn Fn(R) + Send + Sync + 'static>
    where
        F: Fn(R) + Send + Sync + 'static,
        R: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value));
        })
    }

    /// Returns a boxed two-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(R1, R2)` that dispatches each invocation on the callback pool.
    pub fn boxed_fn2<F, R1, R2>(&self, cb: F) -> Box<dyn Fn(R1, R2) + Send + Sync + 'static>
    where
        F: Fn(R1, R2) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2));
        })
    }

    /// Returns an unboxed two-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// An `impl Fn(R1, R2)` wrapper that dispatches each invocation on the pool.
    pub fn fn2<F, R1, R2>(&self, cb: F) -> impl Fn(R1, R2) + Send + Sync + 'static
    where
        F: Fn(R1, R2) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        move |value1, value2| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2));
        }
    }

    /// Returns a cloneable unboxed two-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A cloneable `impl Fn(R1, R2)` wrapper for APIs that duplicate callbacks.
    pub fn fn2_cloneable<F, R1, R2>(&self, cb: F) -> impl Fn(R1, R2) + Clone + Send + Sync + 'static
    where
        F: Fn(R1, R2) + Clone + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        move |value1, value2| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2));
        }
    }

    /// Returns a boxed three-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(R1, R2, R3)` that dispatches each invocation on the pool.
    pub fn boxed_fn3<F, R1, R2, R3>(&self, cb: F) -> Box<dyn Fn(R1, R2, R3) + Send + Sync + 'static>
    where
        F: Fn(R1, R2, R3) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2, value3| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2, value3));
        })
    }

    /// Returns a boxed four-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(R1, R2, R3, R4)` dispatched on the callback pool.
    pub fn boxed_fn4<F, R1, R2, R3, R4>(
        &self,
        cb: F,
    ) -> Box<dyn Fn(R1, R2, R3, R4) + Send + Sync + 'static>
    where
        F: Fn(R1, R2, R3, R4) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
        R4: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2, value3, value4| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2, value3, value4));
        })
    }

    /// Returns a boxed five-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(R1, R2, R3, R4, R5)` dispatched on the callback pool.
    pub fn boxed_fn5<F, R1, R2, R3, R4, R5>(
        &self,
        cb: F,
    ) -> Box<dyn Fn(R1, R2, R3, R4, R5) + Send + Sync + 'static>
    where
        F: Fn(R1, R2, R3, R4, R5) + Send + Sync + 'static,
        R1: Send + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
        R4: Send + 'static,
        R5: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2, value3, value4, value5| {
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(value1, value2, value3, value4, value5));
        })
    }

    /// Returns a boxed callback wrapper that accepts a borrowed first argument.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&R1, R2)` wrapper. The borrowed value is cloned before the
    /// callback is moved to the pool.
    pub fn boxed_ref_fn2<F, R1, R2>(&self, cb: F) -> Box<dyn Fn(&R1, R2) + Send + Sync + 'static>
    where
        F: Fn(&R1, R2) + Send + Sync + 'static,
        R1: Clone + Send + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2| {
            let owned_value1 = value1.clone();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned_value1, value2));
        })
    }

    /// Returns a boxed string-slice callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&str)` wrapper that owns the string before dispatching.
    pub fn boxed_str_fn<F>(&self, cb: F) -> Box<dyn Fn(&str) + Send + Sync + 'static>
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value| {
            let owned = value.to_string();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned));
        })
    }

    /// Returns a boxed `&str` plus one-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&str, R2)` wrapper that owns the string before dispatching.
    pub fn boxed_str_fn2<F, R2>(&self, cb: F) -> Box<dyn Fn(&str, R2) + Send + Sync + 'static>
    where
        F: Fn(&str, R2) + Send + Sync + 'static,
        R2: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value, value2| {
            let owned = value.to_string();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned, value2));
        })
    }

    /// Returns a boxed `&str` plus two-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&str, R2, R3)` wrapper that owns the string before dispatching.
    pub fn boxed_str_fn3<F, R2, R3>(
        &self,
        cb: F,
    ) -> Box<dyn Fn(&str, R2, R3) + Send + Sync + 'static>
    where
        F: Fn(&str, R2, R3) + Send + Sync + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value, value2, value3| {
            let owned = value.to_string();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned, value2, value3));
        })
    }

    /// Returns a boxed `&str` plus three-argument callback wrapper.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&str, R2, R3, R4)` wrapper that owns the string before dispatching.
    pub fn boxed_str_fn4<F, R2, R3, R4>(
        &self,
        cb: F,
    ) -> Box<dyn Fn(&str, R2, R3, R4) + Send + Sync + 'static>
    where
        F: Fn(&str, R2, R3, R4) + Send + Sync + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
        R4: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value, value2, value3, value4| {
            let owned = value.to_string();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned, value2, value3, value4));
        })
    }

    /// Returns a boxed callback wrapper with first and fourth string arguments.
    ///
    /// # Returns
    ///
    /// A boxed `Fn(&str, R2, R3, &str)` wrapper that owns both string slices
    /// before dispatching.
    pub fn boxed_str_str4_fn<F, R2, R3>(
        &self,
        cb: F,
    ) -> Box<dyn Fn(&str, R2, R3, &str) + Send + Sync + 'static>
    where
        F: Fn(&str, R2, R3, &str) + Send + Sync + 'static,
        R2: Send + 'static,
        R3: Send + 'static,
    {
        let executor = self.clone();
        let cb = Arc::new(cb);
        Box::new(move |value1, value2, value3, value4| {
            let owned_value1 = value1.to_string();
            let owned_value4 = value4.to_string();
            let cb_clone = Arc::clone(&cb);
            executor.execute(move || cb_clone(&owned_value1, value2, value3, &owned_value4));
        })
    }
}

#[derive(Clone)]
/// Executes futures and callbacks on the engine-owned runtime infrastructure.
///
/// # Examples
///
/// ```no_run
/// use vibe_ready::{VibeEngine, VibeEngineConfig, VibeResult};
///
/// # fn demo() -> VibeResult<()> {
/// let engine = VibeEngine::create(VibeEngineConfig::builder().build())?;
/// let executor = engine.executor();
/// let value = executor.invoke(async { "ready" })?;
/// assert_eq!(value, "ready");
/// # engine.destroy_with_timeout(std::time::Duration::from_secs(1))?;
/// # Ok(())
/// # }
/// ```
pub struct VibeEngineExecutor {
    inner: Arc<VibeEngineExecutorInner>,
}

pub(crate) struct VibeRuntimeHandle {
    handle: Handle,
    _runtime: Option<Arc<Runtime>>,
}

impl VibeRuntimeHandle {
    pub(crate) fn owned(runtime: Arc<Runtime>) -> Self {
        Self {
            handle: runtime.handle().clone(),
            _runtime: Some(runtime),
        }
    }

    pub(crate) fn external(handle: Handle) -> Self {
        Self {
            handle,
            _runtime: None,
        }
    }

    fn handle(&self) -> &Handle {
        &self.handle
    }
}

struct VibeEngineExecutorInner {
    callback_executor: VibeCallbackExecutor,
    async_tx: Mutex<Option<Sender<VibeEngineTask>>>,
    sync_tx: Mutex<Option<Sender<VibeEngineTask>>>,
    rt: VibeRuntimeHandle,
    shutdown_rx: Mutex<Receiver<()>>,
}

impl VibeEngineExecutor {
    pub(crate) fn new(
        cb_pool: ThreadPool,
        async_tx: Sender<VibeEngineTask>,
        sync_tx: Sender<VibeEngineTask>,
        rt: VibeRuntimeHandle,
        shutdown_rx: Receiver<()>,
    ) -> Self {
        Self {
            inner: Arc::new(VibeEngineExecutorInner {
                callback_executor: VibeCallbackExecutor::new(cb_pool),
                async_tx: Mutex::new(Some(async_tx)),
                sync_tx: Mutex::new(Some(sync_tx)),
                rt,
                shutdown_rx: Mutex::new(shutdown_rx),
            }),
        }
    }

    /// Returns the callback executor associated with this engine.
    ///
    /// # Returns
    ///
    /// A cheap clone of [`VibeCallbackExecutor`].
    pub fn callback(&self) -> VibeCallbackExecutor {
        self.inner.callback_executor.clone()
    }

    pub(crate) fn block_on_with_timeout<T, Fut>(
        &self,
        future: Fut,
        timeout: Duration,
    ) -> Result<T, VibeEngineError>
    where
        T: Send + 'static,
        Fut: Future<Output = Result<T, VibeEngineError>> + Send + 'static,
    {
        let handle = self.inner.rt.handle().clone();
        self.run_blocking_on_engine_rt(move || {
            handle.block_on(async move {
                tokio::time::timeout(timeout, future).await.map_err(|_| {
                    VibeEngineError::from_error_code(VibeEngineErrorCode::TimeoutError)
                })?
            })
        })?
    }

    pub(crate) fn shutdown_queues(&self, timeout: Duration) -> Result<(), VibeEngineError> {
        let async_tx = self
            .inner
            .async_tx
            .lock()
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError))?
            .take();
        let sync_tx = self
            .inner
            .sync_tx
            .lock()
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError))?
            .take();

        if async_tx.is_none() && sync_tx.is_none() {
            return Ok(());
        }

        drop(async_tx);
        drop(sync_tx);

        let shutdown_rx = self
            .inner
            .shutdown_rx
            .lock()
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError))?;
        shutdown_rx
            .recv_timeout(timeout)
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::TimeoutError))
    }

    fn run_blocking_on_engine_rt<R>(
        &self,
        f: impl FnOnce() -> R + Send + 'static,
    ) -> Result<R, VibeEngineError>
    where
        R: Send + 'static,
    {
        let engine_rt_id = self.inner.rt.handle().id();
        match Handle::try_current() {
            Ok(current) if current.id() == engine_rt_id => {
                std::panic::catch_unwind(AssertUnwindSafe(|| tokio::task::block_in_place(f)))
                    .map_err(|payload| {
                        log_e!(
                            "run_blocking_on_engine_rt",
                            DESC,
                            format!(
                                "engine runtime task panicked: {}",
                                panic_payload_message(&payload)
                            )
                        );
                        VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError)
                    })
            }
            Ok(_) => {
                let handle =
                    std::thread::spawn(move || std::panic::catch_unwind(AssertUnwindSafe(f)));
                match handle.join() {
                    Ok(Ok(v)) => Ok(v),
                    Ok(Err(payload)) => {
                        log_e!(
                            "run_blocking_on_engine_rt",
                            DESC,
                            format!(
                                "helper thread task panicked: {}",
                                panic_payload_message(&payload)
                            )
                        );
                        Err(VibeEngineError::from_error_code(
                            VibeEngineErrorCode::RuntimeError,
                        ))
                    }
                    Err(payload) => {
                        log_e!(
                            "run_blocking_on_engine_rt",
                            DESC,
                            format!(
                                "helper thread panicked: {}",
                                panic_payload_message(&payload)
                            )
                        );
                        Err(VibeEngineError::from_error_code(
                            VibeEngineErrorCode::RuntimeError,
                        ))
                    }
                }
            }
            Err(_) => std::panic::catch_unwind(AssertUnwindSafe(f)).map_err(|payload| {
                log_e!(
                    "run_blocking_on_engine_rt",
                    DESC,
                    format!(
                        "non-runtime thread task panicked: {}",
                        panic_payload_message(&payload)
                    )
                );
                VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError)
            }),
        }
    }

    /// Runs a future on the engine synchronous queue and waits for its output.
    ///
    /// # Returns
    ///
    /// `Ok(F)` with the future output, or [`VibeEngineError`] if the task could
    /// not be queued or the runtime failed while waiting.
    pub fn invoke<T, F>(&self, future: T) -> Result<F, VibeEngineError>
    where
        T: Future<Output = F> + Send + 'static,
        F: Send + 'static,
    {
        let (result_tx, result_rx) = oneshot::channel();
        let invoke_future = async move {
            let result = future.await;
            let _ = result_tx.send(result);
        };

        let handle = self.inner.rt.handle().clone();
        let sync_tx = self
            .inner
            .sync_tx
            .lock()
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError))?
            .clone()
            .ok_or_else(|| VibeEngineError::from_code(VibeEngineErrorCode::PostError))?;
        self.run_blocking_on_engine_rt(move || {
            let task: VibeEngineTask = Box::pin(invoke_future);
            if handle.block_on(sync_tx.send(task)).is_err() {
                log_e!("invoke", DESC, "runtime handle block_on error");
                return Err(VibeEngineError::from_code(VibeEngineErrorCode::PostError));
            }
            result_rx.blocking_recv().map_err(|e| {
                log_e!("invoke", DESC, format!("blocking_recv error {}", e));
                VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError)
            })
        })?
    }

    /// Posts a fire-and-forget future to the engine asynchronous queue.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the future is queued, or [`VibeEngineError`] if queuing fails.
    pub fn post<T>(&self, future: T) -> Result<(), VibeEngineError>
    where
        T: Future<Output = ()> + Send + 'static,
    {
        let handle = self.inner.rt.handle().clone();
        let async_tx = self
            .inner
            .async_tx
            .lock()
            .map_err(|_| VibeEngineError::from_error_code(VibeEngineErrorCode::RuntimeError))?
            .clone()
            .ok_or_else(|| VibeEngineError::from_code(VibeEngineErrorCode::PostError))?;
        self.run_blocking_on_engine_rt(move || {
            let task: VibeEngineTask = Box::pin(future);
            handle.block_on(async_tx.send(task)).map_err(|error| {
                log_e!("post", DESC, format!("send async task error: {}", error));
                VibeEngineError::from_code(VibeEngineErrorCode::PostError)
            })
        })?
    }
}

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

    #[test]
    fn callback_executor_supports_multi_argument_wrappers() {
        let executor = VibeCallbackExecutor::new(ThreadPool::new(1));
        let (tx, rx) = mpsc::channel();
        let callback = executor.once3(move |a: i32, b: i32, c: i32| {
            tx.send(a + b + c).expect("send callback result");
        });

        callback(1, 2, 3);

        assert_eq!(rx.recv_timeout(Duration::from_secs(1)).unwrap(), 6);
    }

    #[test]
    fn callback_executor_supports_boxed_string_wrappers() {
        let executor = VibeCallbackExecutor::new(ThreadPool::new(1));
        let (tx, rx) = mpsc::channel();
        let callback = executor.boxed_str_fn4(move |name, a: i32, b: i32, c: i32| {
            tx.send(format!("{name}:{a}:{b}:{c}"))
                .expect("send callback result");
        });

        callback("demo", 1, 2, 3);

        assert_eq!(
            rx.recv_timeout(Duration::from_secs(1)).unwrap(),
            "demo:1:2:3"
        );
    }
}

#[cfg(test)]
mod strict_tests {
    use super::*;
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/test/unit/api/engine_executor_tests.rs"
    ));
}