Rust pyo3 function

From phenobarbital/navigator-auth · View interactive version

Rust pyo3 function is an AI agent skill from the GitHub repository phenobarbital/navigator-auth. Implement or modify a Rust function exposed to Python via PyO3 in eo-processor. Use when adding new compute kernels, fixing numerical/shape bugs in the Rust core, or wiring Rust functions into the Python module safely and consistently.

How to use this skill

Skill file

Rust + PyO3 function skill (eo-processor)

Use this skill when you need to add or change a Rust implementation that is exposed to Python. The goal is: correct math + stable API + high performance + predictable behavior.

Activation checklist (before you code)

  1. Identify the public Python name of the function (e.g., ndvi) and confirm whether this is:
  1. Find existing patterns in:
  1. Define expected behavior for:

If anything is ambiguous, decide based on existing functions in this repo (stay consistent).


Design rules (repo conventions)

1) Keep the core pure

2) Validate shapes early, fail clearly

3) Numerical stability is not optional

4) Avoid unnecessary allocations

5) Don’t introduce `unsafe` without a compelling reason


Implementation workflow (step-by-step)

Step A: Specify the API contract

Write down:

Example contract for a normalized difference:

Step B: Implement Rust function with PyO3 glue

Typical structure:

  1. Accept Python token and PyReadonlyArray* inputs.
  2. Convert to ndarray::ArrayView* using .as_array().
  3. Validate shape compatibility.
  4. Allocate output (or create new array) and fill it.
  5. Return PyArray* via into_pyarray(py) (or existing conventions).

Prefer returning a new array rather than mutating inputs.

Step C: Register with the module

Step D: Maintain Python surface coherence

Whenever you add/rename a Rust-exposed function, you almost always need to update:

If the change is internal-only, do not export it publicly.


Error handling guidance (PyO3)


Performance guidance

What to optimize first

  1. Avoid extra allocations / temporaries.
  2. Ensure tight loops with minimal branching in the inner loop.
  3. Use contiguous iteration patterns when possible.

When to benchmark

Benchmark if:

Don’t claim speedups without before/after numbers and array sizes.


Testing expectations (what to add)

Add tests that cover:

  1. Correctness: known inputs with known outputs (small arrays).
  2. Stability: near-zero denominators; ensure outputs are finite if expected.
  3. Shape behavior: mismatch raises the correct error.
  4. NaN behavior: if inputs contain NaN, outcome matches contract.
  5. Dtype behavior: float32/float64 if supported by project conventions.

For numerical comparisons:


Documentation expectations

For a new EO index function, document:

Keep docs short in README.md and move deeper material to docs/ if present.


“Done” checklist (must be true before you stop)


Common pitfalls (avoid these)


Local references (repo)

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.