nexus_rt/driver.rs
1//! Driver trait for event source installation.
2
3use crate::world::WorldBuilder;
4
5/// Install-time trait for event sources.
6///
7/// A driver registers its resources into [`WorldBuilder`] and returns a
8/// concrete handle. The handle is a thin struct of pre-resolved
9/// [`ResourceId`](crate::ResourceId)s — it knows how to reach into
10/// [`World`](crate::World) but owns nothing.
11///
12/// Each handle defines its own `poll()` signature. This is intentional:
13/// different drivers need different parameters (e.g. a timer driver
14/// needs `Instant`, an IO driver does not).
15///
16/// # Examples
17///
18/// ```ignore
19/// struct IoInstaller { capacity: usize }
20///
21/// struct IoHandle {
22/// poller_id: ResourceId,
23/// events_id: ResourceId,
24/// }
25///
26/// impl Driver for IoInstaller {
27/// type Handle = IoHandle;
28///
29/// fn install(self, world: &mut WorldBuilder) -> IoHandle {
30/// world.register(Poller::new());
31/// world.register(MioEvents::with_capacity(self.capacity));
32/// let poller_id = world.registry().id::<Poller>();
33/// let events_id = world.registry().id::<MioEvents>();
34/// IoHandle { poller_id, events_id }
35/// }
36/// }
37///
38/// // Handle has its own poll signature — NOT a trait method.
39/// impl IoHandle {
40/// fn poll(&mut self, world: &mut World) {
41/// // get resources via pre-resolved IDs, poll mio, dispatch
42/// }
43/// }
44///
45/// let mut wb = WorldBuilder::new();
46/// let io = wb.install_driver(IoInstaller { capacity: 1024 });
47/// let mut world = wb.build();
48///
49/// loop {
50/// io.poll(&mut world);
51/// }
52/// ```
53pub trait Driver {
54 /// The concrete handle returned after installation.
55 type Handle;
56
57 /// Register resources into the world and return a handle for dispatch.
58 fn install(self, world: &mut WorldBuilder) -> Self::Handle;
59}