agentleFS
AkashPriyadarshiiAkashPriyadarshii/cookbook/rust/SKILL.md

rust-boundaries

Skill0 starsChanged 3 months agoMITView on GitHub

Rust boundary lessons: FFI panic handling, poison-recovery locks, JSON serialization of composite keys, cross-compilation artifact discipline, and stub honesty. Use when writing JNI/FFI code, persisting engine state, or cross-compiling Rust for Android/embedded targets.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Fix: never .unwrap() on locks or FFI paths.

    line 21
  2. Very specific:

    Verify with ls -la after copy.

    line 59

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Boundary Lessons

Lessons from a production Android IME (Rust engine via JNI). Each one broke something real.

1. panic=abort + FFI = host process death

With panic = "abort" in the release profile, any panic inside a JNI call kills the entire host process — the IME crash-loops on device. .unwrap() on a mutex lock is the classic trigger (poisoned lock → unwrap panics → dead).

Fix: never .unwrap() on locks or FFI paths. Recover from poison:

fn engine() -> MutexGuard<'static, Predictor> {
    match ENGINE.get_or_init(|| Mutex::new(Predictor::new())).lock() {
        Ok(g) => g,
        Err(poisoned) => poisoned.into_inner(), // data still valid; recover
    }
}

Check: grep unwrap() in every extern fn before release.

2. Global state: OnceLock<Mutex<T>>, init-once

Engine state as static ENGINE: OnceLock<Mutex<T>>. Init once from the host, reuse across calls. OnceLock can't be cleared — "destroy" just drops on process exit; don't pretend otherwise in a destroy() function.

3. Composite keys aren't JSON map keys

HashMap<(String, String), V> won't serialize to JSON ("key must be a string"). Flatten to row structs:

#[derive(Serialize, Deserialize)]
struct Row { k1: String, k2: String, v: u32 }

Check (mandatory for any persistence): roundtrip test (save → load → state equal) + corrupt-input test (from_json(b"{corrupt") == false, state unchanged).

4. Cross-compile artifact discipline

  • Targets configured in .cargo/config.toml, not ad-hoc env vars.
  • Artifact name ≠ crate name: libakashboard_engine.so must be copied as whatever the host's System.loadLibrary("predictor") expects — here jniLibs/arm64-v8a/libpredictor.so.
  • Verify with ls -la after copy; stale .so from a previous build is silent.

5. Stubs that lie

A native function returning hardcoded success (nativeSaveModel → true) makes the caller believe persistence works. Data loss follows. Stub = return failure / log loudly, or don't ship the binding.

6. Consistency tests beat per-function ceremony

For optimized algorithms (early-exit edit distance), test equivalence against the naive version across representative inputs — catches optimization bugs that per-function unit tests miss. Property-style check, no framework needed.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.