enfync/handle.rs
1//local shortcuts
2use crate::*;
3
4//third-party shortcuts
5
6//standard shortcuts
7use std::fmt::Debug;
8
9//-------------------------------------------------------------------------------------------------------------------
10
11/// A handle to a runtime.
12pub trait Handle: Clone + Default + TryAdopt
13{
14 /// Spawn a task on the handle's runtime.
15 fn spawn<R, F>(&self, task: F) -> PendingResult<R>
16 where
17 R: Debug + Send + Sync + 'static,
18 F: std::future::Future<Output = R> + Send + 'static;
19}
20
21//-------------------------------------------------------------------------------------------------------------------
22
23/// Try to adopt the existing runtime.
24///
25/// Returns `None` if no runtime is detected.
26pub trait TryAdopt: Sized
27{
28 fn try_adopt() -> Option<Self>;
29}
30
31//-------------------------------------------------------------------------------------------------------------------
32
33/// Try to adopt the existing runtime, otherwise fall back to the default runtime.
34pub trait AdoptOrDefault: TryAdopt + Default
35{
36 fn adopt_or_default() -> Self
37 {
38 if let Some(adoptee) = Self::try_adopt() { return adoptee; }
39 Self::default()
40 }
41}
42
43impl<T: TryAdopt + Default> AdoptOrDefault for T {}
44
45//-------------------------------------------------------------------------------------------------------------------