@@ -2,7 +2,7 @@ use crate::Task;
22use linkme:: distributed_slice;
33use std:: sync:: OnceLock ;
44
5- /// Compat encapsulate functions to find async runtimes to spawn task.
5+ /// Item of [COMPATS] to encapsulate functions to spawn task for async runtime .
66#[ non_exhaustive]
77pub enum Compat {
88 /// Named global function to spawn task.
@@ -16,7 +16,43 @@ pub enum Compat {
1616 Local ( fn ( ) -> Option < fn ( Task ) > ) ,
1717}
1818
19- /// [DistributedSlice][linkme::DistributedSlice] to collect [Compat]s.
19+ /// [DistributedSlice][linkme::DistributedSlice] to collect [Compat]s from
20+ /// [distributed_slice][linkme::distributed_slice].
21+ ///
22+ /// Here are examples for `tokio` and `smol`, see [linkme] for details.
23+ ///
24+ /// ```rust,no_run
25+ /// use spawns_core as spawns;
26+ ///
27+ /// use linkme::distributed_slice;
28+ /// use spawns::{Compat, Task, COMPATS};
29+ ///
30+ /// #[distributed_slice(COMPATS)]
31+ /// static TOKIO: Compat = Compat::Local(tokio_local);
32+ ///
33+ /// fn tokio_spawn(task: Task) {
34+ /// let Task { future, .. } = task;
35+ /// let handle = tokio::runtime::Handle::current();
36+ /// handle.spawn(Box::into_pin(future));
37+ /// }
38+ ///
39+ /// fn tokio_local() -> Option<fn(Task)> {
40+ /// tokio::runtime::Handle::try_current()
41+ /// .ok()
42+ /// .map(|_| tokio_spawn as fn(Task))
43+ /// }
44+ ///
45+ /// #[distributed_slice(COMPATS)]
46+ /// static SMOL: Compat = Compat::NamedGlobal {
47+ /// name: "smol",
48+ /// spawn: smol_global,
49+ /// };
50+ ///
51+ /// fn smol_global(task: Task) {
52+ /// let Task { future, .. } = task;
53+ /// smol::spawn(Box::into_pin(future)).detach()
54+ /// }
55+ /// ```
2056#[ distributed_slice]
2157pub static COMPATS : [ Compat ] = [ ..] ;
2258
0 commit comments