Skip to content

Migrate to Rust 2024 edition #144

Description

@inureyes

Problem / Background

The codebase currently uses Rust 2021 edition (edition = "2021" in Cargo.toml). Rust 2024 edition is now stable and brings several language changes that affect this project. Migration should be done proactively to stay current and benefit from new language features.

Running cargo fix --edition and the edition migration lint reveals 4 areas of required changes, ordered below by risk and difficulty. The current toolchain is rustc 1.94.1.


Migration Areas

1. [HIGH RISK] Temporary value drop order changes (21 warnings, NOT auto-fixable)

Rust 2024 changes the drop order of temporary values in tail expressions. This is the most dangerous area because it can cause runtime behavioral changes including potential deadlocks.

Affected files:

  • src/device/readers/tpu_grpc.rs:120 — A MutexGuard is held while create_channel().await produces temporaries whose drop order changes. The lock release timing could change, risking deadlock in async context.
  • src/main.rsTerminalManager has a custom Drop impl (terminal restoration). Drop order change could cause terminal state corruption on exit.
  • src/view/data_collection/local_collector.rs — Similar temporary drop order issues with async operations.

Fix approach: Manually review each warning site. Bind temporaries to explicit let variables to make drop order deterministic and edition-independent. Each site needs case-by-case analysis of the Drop implementations involved.

Reference: https://doc.rust-lang.org/edition-guide/rust-2024/temporary-tail-expr-scope.html

2. [MEDIUM RISK] gen keyword reservation

Rust 2024 reserves gen as a keyword (for future generator syntax). The codebase uses gen as variable/field names in multiple places:

  • src/api/metrics/gpu.rs — local variable gen from PCIe generation parsing
  • src/api/metrics/npu/tenstorrent.rs — same pattern
  • src/device/readers/amd.rs — 5 occurrences accessing link.gen field from external amdgpu crate
  • src/device/readers/nvidia.rs — similar usage

Fix approach:

  • For local variables: rename to pcie_gen or generation.
  • For external crate fields (e.g., link.gen from amdgpu crate): must use r#gen raw identifier syntax until upstream crate updates. Check if upstream crates have 2024-edition-compatible releases.

3. [LOW RISK] Macro expr fragment specifier changes (32+ fixes, auto-fixable)

Rust 2024 expands what $x:expr matches (now includes const { ... } blocks). cargo fix --edition auto-converts to $x:expr_2021 to preserve behavior, but this is a transitional specifier.

Affected files:

  • src/parsing/macros.rs — 32 occurrences
  • src/common/error_handling.rs — 3 macros (handle_error!, log_error_default!, log_error_return!)
  • src/device/readers/common_cache.rs — 2 macros (build_detail_map!, cache_device_static_info!)

Fix approach: For most macros in this project, accepting the broader expr (2024 semantics) is fine. Review each macro to decide: use new $x:expr (accepts more syntax) or explicitly restrict with $x:expr_2021.

4. [LOW RISK] ref in if let patterns + if let chaining (auto-fixable)

Rust 2024 makes ref unnecessary in if let Some(ref x) patterns due to improved ergonomics, and changes how if let chains work.

Affected files:

  • src/device/memory_linux.rs — 1 occurrence
  • src/device/memory_linux/tests.rs — 3 occurrences
  • src/device/readers/nvidia.rsif let Ok(status) = NVML_STATUS.lock() converted to match expression

Fix approach: Simple mechanical changes: if let Some(ref x) becomes if let Some(x).


Acceptance Criteria

  • Fix temporary drop order issues in tpu_grpc.rs (bind temporaries to let variables, verify no deadlock)
  • Fix temporary drop order issues in main.rs (ensure TerminalManager drops correctly)
  • Fix temporary drop order issues in local_collector.rs
  • Rename local gen variables to non-keyword names (pcie_gen or generation)
  • Handle external crate gen fields (check upstream, use r#gen if needed)
  • Review and update macro fragment specifiers (expr_2021 vs expr)
  • Remove unnecessary ref in if let patterns
  • Update Cargo.toml edition from "2021" to "2024"
  • Run full test suite and verify no behavioral regressions
  • Test TUI mode for terminal restoration correctness
  • Test TPU gRPC reader under concurrent access

Technical Considerations

  • The HIGH RISK items (drop order) cannot be auto-fixed and require careful manual review. Incorrect handling can lead to deadlocks or terminal corruption at runtime without any compile-time warning.
  • The gen keyword issue may require coordination with upstream crate maintainers (particularly the amdgpu crate).
  • The macro and ref changes are largely mechanical and can be auto-fixed, but should be reviewed for correctness.
  • Consider running the migration in phases: fix all issues first on 2021 edition (edition-compatible fixes), then flip the edition flag last.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions