Repository navigation
Rust rewrite
Since 0.8.0 the terminal service and the logic of the fcitx5 addon are written in Rust. The behaviour is the same as 0.7.2; this page records how the rewrite was structured, the decisions behind it, and how it was checked.
One cargo workspace under rust/:
-
password-layout-core— Latin layout tables and classification, the shared state file, pacing, theCompositortrait, the Hyprland socket client and the minimal JSON reading. std only. -
password-layout— the binary:watch-tty,enter,leave,status,doctor; terminals (tty, activity, tmux), the prompt watcher. -
password-layout-addon— the addon's worker thread and its C interface, built as a static library and linked with a thin C++ shim (rust/password-layout-addon/shim/passwordlayout.cpp) into the onelibpasswordlayout.sothat fcitx5 loads.
The Makefile drives cargo; make install puts the same files in the same places as
before. The C++ sources (src/, tests/test.cpp) stay as the reference for the parity
check, make test-cpp and make bench; they are built into build/cpp/ and never
installed.
The rewrite went in four steps, each finished with all tests green before the next:
- core (Latin tables, state, pacing), 2. terminals and the CLI, 3. Hyprland client and
the fcitx5 addon, 4.
doctor, build and packaging.
Numbers as recorded during the rewrite (AGENTS.md refers to decision 7).
-
No third-party crates in the core. File locking uses
std::fs::File::lock; the rest is std. -
One cargo workspace in
rust/, next to the C++ code, not replacing it file by file. -
The state file stays byte-compatible with C++, so both versions can share
$XDG_RUNTIME_DIR/password-layout/during an upgrade (checked on a live system: the Rust binary read and wrote the state of a running C++ service). -
Compositoris a Rust trait (a fake in tests, Hyprland in the binary). -
The binary crate uses
libc, and only there; the core stays std only. The terminal side needstcgetattr,inotify_*,poll,access,getuidandsigaction, which std lacks.libcis rust-lang's bindings crate, with no transitive dependencies and the righttermios/pollfd/sigactionlayouts for every architecture, where hand-written declarations could be silently wrong on an untested one. Pinned byrust/Cargo.lock. Everyunsafeblock is one operation with a// SAFETY:comment. -
The fcitx5 addon is a thin C++ shim over a Rust static library. fcitx5 loads
addons only through its C++ interface (
AddonFactory,AddonInstance,std::functionevent handlers,InputContextmember functions), which has no C ABI; a Rust-onlycdylibwould have to reproduce libstdc++ and fcitx5 class layouts and could crash fcitx5 after an update. The shim keeps the factory, the focus and capability watchers andisPasswordField; the worker, the Hyprland client and the state are the service's Rust code behind threeextern "C"functions. The Hyprland socket uses std'sUnixStream(timeouts andMSG_NOSIGNALincluded). -
The build is replaced, the C++ sources are kept.
make,make testandmake installbuild, test and install only Rust and the shim; the C++ code stays for parity and the bench until it is removed in a later release.
-
Tests: every one of the 35 C++ tests has a Rust counterpart, plus new ones;
cargo test,cargo clippy -D warnings,cargo fmt --check, andmake testincluding an addon load test against the installed fcitx5. -
Parity check (
rust/parity/run.sh): the C++ and the Rust code are driven the same way by two small drivers with a fake compositor (rust/parity/parity_driver.cpp,rust/password-layout-core/examples/parity_driver.rs) and their output is compared:- every row of
tests/xkb-latin.tsvplus extra cases (610 lines) — Latin classification; - 14 enter/leave/set scenarios — state file and layout switches;
- 14 JSON inputs — the JSON reading;
- 6 Hyprland cases — the socket client against a fake Hyprland;
- 18
doctorcases — the realdoctorof both binaries with stubbedsystemctl,fcitx5,pacman,sudo.
Result for 0.8.0: no differences.
- every row of
-
On a real system (Omarchy, Hyprland, fcitx5 5.1.23):
statusanddoctorprint the same as 0.7.2; sudo prompts in a terminal and password fields in the browser switch the layout as before.
Measured on an i7-11700, both binaries from their packages; details of the method on Resource use.
| 0.7.2 (C++) | 0.8.0 (Rust) | |
|---|---|---|
| Service idle, 5 min side by side | 1542 wakeups, 138 ms CPU | 1545 wakeups, 140 ms CPU |
| Service Rss | 4.3–4.5 MB | 2.6–2.7 MB (no libstdc++) |
| Service Pss | 470–530 kB | 650–750 kB (Rust code is not shared) |
status, median |
3.0 ms, peak 4.6 MB | 3.0 ms, peak 2.6 MB |
doctor, median |
24 ms | 24 ms |
| Binary / addon size | 150 KB / 96 KB | 612 KB / 423 KB |