Skip to main content

sim_table_lazy/
backend.rs

1//! Library registration for the lazy table backend: the [`LazyBackend`]
2//! `TableBackend` implementation, the [`LazyTableLib`] manifest, and the
3//! [`install_lazy_table_lib`] entry point.
4
5use std::sync::Arc;
6
7use sim_kernel::{Cx, Lib, LibManifest, Linker, Result, Symbol, TableBackend, Value};
8
9use crate::LazyTable;
10
11/// Table backend that constructs [`LazyTable`] objects for the `lazy` backend
12/// name.
13///
14/// Registered with the context table registry by [`install_lazy_table_lib`].
15/// Tables built through this backend hold the supplied entries eagerly; lazy
16/// loaders are added afterwards via [`LazyTable::put_lazy`].
17pub struct LazyBackend;
18
19impl TableBackend for LazyBackend {
20    fn name(&self) -> &str {
21        "lazy"
22    }
23
24    fn new_table(&self, cx: &mut Cx, entries: Vec<(Symbol, Value)>) -> Result<Value> {
25        cx.factory()
26            .opaque(Arc::new(LazyTable::with_entries(entries)))
27    }
28}
29
30/// Loadable library manifest for the lazy table backend.
31///
32/// Declares the `table/lazy` library identity and ABI; backend registration is
33/// performed separately by [`install_lazy_table_lib`].
34pub struct LazyTableLib;
35
36impl Lib for LazyTableLib {
37    fn manifest(&self) -> LibManifest {
38        sim_table_core::backend_manifest(
39            Symbol::qualified("table", "lazy"),
40            env!("CARGO_PKG_VERSION"),
41        )
42    }
43
44    fn load(&self, _cx: &mut sim_kernel::LoadCx, _linker: &mut Linker<'_>) -> Result<()> {
45        Ok(())
46    }
47}
48
49/// Register the [`LazyBackend`] and load the [`LazyTableLib`] into `cx`.
50///
51/// Idempotent: if the `table/lazy` library is already present the call is a
52/// no-op. After installation the `lazy` backend can be selected as the active
53/// table backend via the context table registry.
54pub fn install_lazy_table_lib(cx: &mut Cx) -> Result<()> {
55    let lib_id = Symbol::qualified("table", "lazy");
56    if cx.registry().lib(&lib_id).is_some() {
57        return Ok(());
58    }
59    cx.table_registry_mut().register(Arc::new(LazyBackend));
60    cx.load_lib(&LazyTableLib).map(|_| ())
61}