Skip to main content

nexus_rt/
driver.rs

1//! Installer trait for event source installation.
2
3use crate::world::WorldBuilder;
4
5/// Install-time trait for event sources.
6///
7/// An installer registers its resources into [`WorldBuilder`] and returns a
8/// concrete poller. The poller 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 poller 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 IoPoller {
22///     poller_id: ResourceId,
23///     events_id: ResourceId,
24/// }
25///
26/// impl Installer for IoInstaller {
27///     type Poller = IoPoller;
28///
29///     fn install(self, world: &mut WorldBuilder) -> IoPoller {
30///         let poller_id = world.register(Poller::new());
31///         let events_id = world.register(MioEvents::with_capacity(self.capacity));
32///         IoPoller { poller_id, events_id }
33///     }
34/// }
35///
36/// // Poller has its own poll signature — NOT a trait method.
37/// impl IoPoller {
38///     fn poll(&mut self, world: &mut World) {
39///         // get resources via pre-resolved IDs, poll mio, dispatch
40///     }
41/// }
42///
43/// let mut wb = WorldBuilder::new();
44/// let io = wb.install_driver(IoInstaller { capacity: 1024 });
45/// let mut world = wb.build();
46///
47/// loop {
48///     io.poll(&mut world);
49/// }
50/// ```
51pub trait Installer {
52    /// The concrete poller returned after installation.
53    type Poller;
54
55    /// Register resources into the world and return a poller for dispatch.
56    fn install(self, world: &mut WorldBuilder) -> Self::Poller;
57}