Skip to content

Commit 46aad8b

Browse files
committed
Refine rustdoc for Compat and COMPATS
1 parent 7a81ded commit 46aad8b

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

spawns-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ linkme = { version = "0.3.25", optional = true }
2424
[dev-dependencies]
2525
futures = "0.3.30"
2626
static_assertions = "1.1.0"
27+
tokio = { version = "1", features = ["rt"] }
28+
smol = { version = "2" }
2729

2830
[package.metadata.docs.rs]
2931
all-features = true

spawns-core/src/compat.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::Task;
22
use linkme::distributed_slice;
33
use 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]
77
pub 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]
2157
pub static COMPATS: [Compat] = [..];
2258

0 commit comments

Comments
 (0)