| 1 | <!-- Copyright 2025 The Fuchsia Authors |
| 2 | |
| 3 | Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 | <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 | license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 | This file may not be copied, modified, or distributed except according to |
| 7 | those terms. --> |
| 8 | |
| 9 | # Development Guidelines |
| 10 | |
| 11 | This document covers guidelines for developing code changes. |
| 12 | |
| 13 | ## Build and Test |
| 14 | |
| 15 | This repository uses a wrapper script (`cargo.sh`) to ensure consistent |
| 16 | toolchain usage and configuration. |
| 17 | |
| 18 | > [!IMPORTANT] |
| 19 | > **NEVER** run `cargo` directly. |
| 20 | > **ALWAYS** use `./cargo.sh` for all cargo sub-commands. |
| 21 | > |
| 22 | > **Why?** `cargo.sh` ensures that the toolchains used in development match |
| 23 | > those in CI, which is important because some features are only available on |
| 24 | > specific toolchains, and because UI tests rely on the text of compiler errors, |
| 25 | > which changes between toolchain versions. |
| 26 | |
| 27 | ### Syntax |
| 28 | |
| 29 | `./cargo.sh +<toolchain> <command> [args]` |
| 30 | |
| 31 | This is equivalent to: |
| 32 | |
| 33 | `cargo +1.2.3 <command> [args]` |
| 34 | |
| 35 | ...where `1.2.3` is the toolchain version named by `<toolchain>` (e.g., `msrv` -> |
| 36 | `1.56.0`). |
| 37 | |
| 38 | ### Toolchains |
| 39 | |
| 40 | The `<toolchain>` argument is mandatory: |
| 41 | - `msrv`: Minimum Supported Rust Version. |
| 42 | - `stable`: Stable toolchain. |
| 43 | - `nightly`: Nightly toolchain. |
| 44 | - `all`: Runs on `msrv`, `stable`, and `nightly` sequentially. |
| 45 | - Version-gated: e.g., `no-zerocopy-core-error-1-81-0` (see `Cargo.toml`). |
| 46 | |
| 47 | |
| 48 | ## MSRV (Minimum Supported Rust Version) |
| 49 | |
| 50 | The MSRV is **1.56.0**. |
| 51 | |
| 52 | - **Do NOT** use features stabilized after 1.56.0 unless version-gated. |
| 53 | - **Requirement:** Ask for user approval before introducing new version-gated |
| 54 | behavior. |
| 55 | - **Verify**: Ensure code compiles on 1.56.0 (`./cargo.sh +msrv ...`). |
| 56 | |
| 57 | ### Version Gating Convention |
| 58 | |
| 59 | We use `[package.metadata.build-rs]` in `Cargo.toml` to gate features by Rust version. |
| 60 | |
| 61 | 1. **Define**: Add `no-zerocopy-<feature>-<version> = "<version>"` to `Cargo.toml`. |
| 62 | 2. **Use**: Use `#[cfg(not(no_zerocopy_<feature>_<version>))]` (note underscores). |
| 63 | 3. **Document**: For public items, use `#[cfg_attr(doc_cfg, doc(cfg(rust = "<version>")))]`. |
| 64 | |
| 65 | **Important:** The toolchains listed in `.github/workflows/ci.yml` and |
| 66 | `Cargo.toml` (under `[package.metadata.build-rs]`) must be kept in sync. If you |
| 67 | add a new version-gated feature, ensure it is reflected in both places. |
| 68 | |
| 69 | ## UI Tests |
| 70 | |
| 71 | For advice on how to add, modify, or remove UI tests (in `tests/ui-*` or |
| 72 | `zerocopy-derive/tests/ui-*`), refer to [agent_docs/ui_tests.md](./ui_tests.md). |
| 73 | |
| 74 | ## Macro Development |
| 75 | |
| 76 | - **Shared Logic:** Put shared macro logic in `src/util/macro_util.rs` to avoid |
| 77 | code duplication in generated code. |
| 78 | - **Lints:** Generated code often triggers lints. Use `#[allow(...)]` liberally |
| 79 | in generated code to suppress them. |
| 80 | ```rust |
| 81 | // GOOD: Suppress lints that might be triggered by generated names. |
| 82 | // Example: Using a variant name (PascalCase) as a field name (snake_case). |
| 83 | // Input: `enum MyEnum { VariantA }` |
| 84 | // Generated: `union Variants { __field_VariantA: ... }` |
| 85 | quote! { |
| 86 | #[allow(non_snake_case)] |
| 87 | union ___ZerocopyVariants { |
| 88 | #(#fields)* |
| 89 | } |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | ## Unsafe Code |
| 94 | |
| 95 | `unsafe` code is extremely dangerous and should be avoided unless absolutely |
| 96 | necessary. For guidelines on writing unsafe code, including pointer casts and |
| 97 | safety comments, refer to [agent_docs/unsafe_code.md](./unsafe_code.md). |