Skip to content

Rust rewrite

Mike edited this page Oct 28, 2026 · 1 revision

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.

Layout

One cargo workspace under rust/:

  • password-layout-core — Latin layout tables and classification, the shared state file, pacing, the Compositor trait, 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 one libpasswordlayout.so that 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:

  1. core (Latin tables, state, pacing), 2. terminals and the CLI, 3. Hyprland client and the fcitx5 addon, 4. doctor, build and packaging.

Decisions

Numbers as recorded during the rewrite (AGENTS.md refers to decision 7).

  1. No third-party crates in the core. File locking uses std::fs::File::lock; the rest is std.
  2. One cargo workspace in rust/, next to the C++ code, not replacing it file by file.
  3. 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).
  4. Compositor is a Rust trait (a fake in tests, Hyprland in the binary).
  5. The binary crate uses libc, and only there; the core stays std only. The terminal side needs tcgetattr, inotify_*, poll, access, getuid and sigaction, which std lacks. libc is rust-lang's bindings crate, with no transitive dependencies and the right termios/pollfd/sigaction layouts for every architecture, where hand-written declarations could be silently wrong on an untested one. Pinned by rust/Cargo.lock. Every unsafe block is one operation with a // SAFETY: comment.
  6. The fcitx5 addon is a thin C++ shim over a Rust static library. fcitx5 loads addons only through its C++ interface (AddonFactory, AddonInstance, std::function event handlers, InputContext member functions), which has no C ABI; a Rust-only cdylib would 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 and isPasswordField; the worker, the Hyprland client and the state are the service's Rust code behind three extern "C" functions. The Hyprland socket uses std's UnixStream (timeouts and MSG_NOSIGNAL included).
  7. The build is replaced, the C++ sources are kept. make, make test and make install build, 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.

How it was checked

  • Tests: every one of the 35 C++ tests has a Rust counterpart, plus new ones; cargo test, cargo clippy -D warnings, cargo fmt --check, and make test including 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.tsv plus 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 doctor cases — the real doctor of both binaries with stubbed systemctl, fcitx5, pacman, sudo.

    Result for 0.8.0: no differences.

  • On a real system (Omarchy, Hyprland, fcitx5 5.1.23): status and doctor print the same as 0.7.2; sudo prompts in a terminal and password fields in the browser switch the layout as before.

Resource use against 0.7.2

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

Clone this wiki locally