-
Notifications
You must be signed in to change notification settings - Fork 23
Introduce Rust guide #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
natelust
wants to merge
2
commits into
main
Choose a base branch
from
tickets/DM-52152
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Introduce Rust guide #732
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| LSST Rust Development Guide | ||
| =========================== | ||
|
|
||
| 1. Introduction | ||
| --------------- | ||
|
|
||
| This guide details the standards and best practices for writing Rust code within the LSST DM Stack and includes basic knowledge of the package. | ||
| Rust is being adopted to provide performance-critical components and leverage its memory safety features. | ||
| This document assumes a basic understanding of Rust and the LSST DM Stack. | ||
|
|
||
| The de-facto location, and reference implementation, of rust within the lsst science pipelines is in a package called ``rubinoxide``. | ||
| This does not mean developers are not allowed to use rust in another dedicated package, but unless there is a compelling reason to do so rust code should be placed in ``rubinoxide``. | ||
| This guide assumes the package layout of ``rubinoxide``. | ||
| Any other Rust based packages that are written should adhear to this as best as possible. | ||
|
|
||
| 2. Rust Version | ||
| --------------- | ||
|
|
||
| All LSST Rust code must be compatible with the standard Rust toolchain provided in the rubin-env conda environment. | ||
|
|
||
| 3. Resources | ||
| ------------ | ||
|
|
||
| * The `Rust Book <https://doc.rust-lang.org/book/>`_ is a good resource to find out information on the rust language. | ||
| * `crates.io <https://crates.io/>`_ is where third party crates (libraries) are stored and distributed. | ||
| This contains links to the documentation on each crate. | ||
| * Likewise the python/rust build system can be found at `maturin <https://www.maturin.rs/>`_ | ||
| * Documentation for the python binding engine PyO3 can be found `here <https://docs.rs/pyo3/latest/pyo3/>`_ | ||
|
|
||
| 4. Code Organization | ||
| -------------------- | ||
|
|
||
| Unlike the package-centric organization often seen in Python, packages containing rust code should me monolithic and not depend on other lsst packages. | ||
| * Top-Level Module: All Rust code will be bound to a single top-level module. | ||
| This module will serve as the entry point for Python interaction. | ||
| * Functional Modules: Within this top-level module, create sub-modules representing distinct functionalities (e.g., image processing, coordinate transformations, data structures). | ||
| * Avoid Package-Specific Structure: Do not mirror the Python package structure in your Rust organization. | ||
| Focus on logical groupings of functionality. | ||
|
|
||
| 5. Python Interoperability | ||
| -------------------------- | ||
|
|
||
| All Rust code intended for use within the LSST DM Stack must be exposed to Python via a well-defined API. This is achieved using the `pyO3`_ bindings. | ||
| * `pyO3`_ is Mandatory: `pyO3`_ is the only supported mechanism for exposing Rust functionality to Python. | ||
| * API Design: Rust-implemented Python functions and types should adhere to Python interface best practices instead of maximizing similarity to other internal Rust interfaces. | ||
| * Documentation: Thoroughly document your Python API using docstrings. Docstrings are written as doc comments in the rust and are automatically translated to python docstrings by pyO3. Doc strings should be written in the same numpydoc format as specified in the python section of the devguide. | ||
|
|
||
| 6. Dependencies | ||
| --------------- | ||
|
|
||
| Managing dependencies is crucial for maintaining a stable and reproducible build environment. | ||
| * RFC Process: All new Rust dependencies added to the Cargo.toml file must be approved via the LSST RFC (Request for Comments) process. | ||
| This ensures that dependencies are vetted for licensing, security, and long-term maintainability. | ||
| * Dependency Versions: Pin dependency versions in Cargo.toml to ensure reproducible builds with exact pins. | ||
| As the Cargo.lock file is also committed to git, this ensures all builds remain on equal footing. | ||
| Incrementing version should be done on a standalone commit after evaluating the effect on compiling and packaging. | ||
|
|
||
| 7. Testing | ||
| ---------- | ||
|
|
||
| * Python Unit Tests: All Rust code with a Python interface must be tested via Python unit tests. | ||
| This provides a consistent testing framework and leverages the existing LSST testing infrastructure. | ||
| These tests should be placed in the **tests** top level directory. | ||
| * pytest: Use pytest as the python testing framework. | ||
| * For functionality that does not have a public python api, or is not well covered by a python api, or is difficult to appropriately test with a python unit test rust unit tests may be written using the standard rust unit test infrastructure. | ||
| Genreally avoid a rust until test on any code that is wrapped with pyo3. | ||
| * Import Rust Modules: Python tests should import the Rust modules (exposed via `pyO3`_) and exercise their functionality. | ||
| * Comprehensive Coverage: Strive for high test coverage to ensure that all critical code paths are tested. | ||
| * Integration Tests: In addition to unit tests, consider integration tests to verify the interaction between Rust components and other parts of the LSST DM Stack. | ||
|
|
||
| 8. Code Style and Formatting | ||
| ---------------------------- | ||
|
|
||
| Consistent code style improves readability and maintainability. | ||
|
|
||
| * `rustfmt`_: Use rustfmt to automatically format your Rust code. | ||
| * `Clippy`_: Use Clippy to lint your Rust code and identify potential issues. | ||
| * Documentation Comments: Write clear and concise documentation comments for all public functions and data structures. | ||
| * To the extent possible, make apis that will be public to python feel exactly as if they are written in python. | ||
|
|
||
| 9. Logging | ||
| ----------- | ||
|
|
||
| Logging should be done using the `log`_ crate, and accompanying macros. | ||
| The standard rubinoxide package initializes the pyo3_log crate to forward all logs through to the python log handler to the approprate log level. | ||
|
|
||
| 10. Don'ts | ||
| ---------- | ||
|
|
||
| * Do not use implicit multithreading in rust | ||
| * Do not introduce any cross package rust bindings that transit through python aka how we use c++ now. | ||
| * As mentioned above, do not arange module structure according to lsst packages. Write modules by related functionality. | ||
|
|
||
| 11. Build and management system integration | ||
| ------------------------------------------- | ||
|
|
||
| * Rust packages should be built using maturin, which manages the complexities of compiling and bundling cargo products. | ||
| * Cargo additionally is to be used manage dependencies and run rust level tests. | ||
| * pip is used as the mechanism to locally depoly the wheels created by maturin | ||
| * pytest is used to run python level unit tests | ||
| * coordinating these scripts for the developer is a Makefile | ||
|
|
||
| .. _rustfmt: https://github.com/rust-lang/rustfmt | ||
| .. _Clippy: https://github.com/rust-lang/rust-clippy | ||
| .. _pyO3: https://pyo3.rs/ | ||
| .. _log: https://docs.rs/log/latest/log/ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.