1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! A REPE plugin: a `cdylib` that any REPE host can `dlopen` and drive.
//!
//! Build it with
//!
//! ```text
//! cargo build --release --features plugin --example repe_plugin
//! ```
//!
//! which leaves a `librepe_plugin.so` (`.dylib` on macOS) in `target/release/examples`.
//! A host resolves five symbols from it and speaks ordinary REPE frames across
//! the boundary — including the C++ host in Glaze, since both ends implement the
//! same ABI from `glaze/rpc/repe/plugin.h`.
//!
//! The shape here is the one this ABI exists for: a device-control object whose
//! **fields** are readable and writable state and whose **methods** are commands,
//! published wholesale by reflecting off the type. Nothing below restates the RPC
//! surface — the derive produces it from the struct, and `#[repe::plugin]`
//! produces the C exports from the router.
//!
//! In a real plugin crate this file would be `src/lib.rs` with
//! `crate-type = ["cdylib"]` in its own `Cargo.toml`; it lives in `examples/`
//! here so the repository builds and link-checks it on every CI run.
use Router;
/// The object this plugin publishes. Every field becomes a readable and
/// writable endpoint under the plugin's root; every method in the `impl` block
/// below becomes a callable one.
object!;
/// A command failed on the device. Any `Display` error works; the crate turns it
/// into a REPE error response.
;
/// Build the router this plugin serves.
///
/// `#[repe::plugin]` turns it into the five C exports. `name` and `version`
/// default to this crate's `CARGO_PKG_NAME` and `CARGO_PKG_VERSION`, so the
/// plugin's identity comes from the manifest that already carries it and cannot
/// drift from it.
///
/// The function stays callable, which is the point: the same router that goes
/// over the ABI can be driven directly by an in-process test, with no `dlopen`
/// and no host in the loop.
///
/// `with_struct_rw` is the part worth copying. `plugin.h` permits a host to call
/// `repe_plugin_call` from several threads at once, and `with_struct`'s default
/// `Mutex` would serialize every one of them — a `/gain` read included. Behind
/// the `RwLock` this registers, reads (`identify` and every field) share the
/// guard; only `calibrate` and `reset`, which take `&mut self`, take it
/// exclusively.