Skip to content

chore: Upgrade Rust edition to 2024 - #236

Merged
ayushag-nv merged 3 commits into
mainfrom
gk-rust-2024
Jul 31, 2026
Merged

chore: Upgrade Rust edition to 2024#236
ayushag-nv merged 3 commits into
mainfrom
gk-rust-2024

Conversation

@grahamking

@grahamking grahamking commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This gives us if-let chains.

Also upgrade rand so we have a single version of that dependency.

Summary by CodeRabbit

  • Refactor

    • Simplified internal conditional logic across request validation, routing, streaming, translation, metrics, and error handling.
    • Preserved existing runtime behavior, validation results, response formats, and shutdown handling.
  • Chores

    • Updated the project to newer Rust language and build conventions.
    • Updated random-number generation compatibility and improved compiler/linter compliance.
    • Reformatted imports and tests for consistency.

For if-let chains.

Also upgrade `rand` so we have a single version.

Signed-off-by: Graham King <grahamk@nvidia.com>
Signed-off-by: Graham King <grahamk@nvidia.com>
@grahamking
grahamking requested a review from a team as a code owner July 31, 2026 20:37
Signed-off-by: Graham King <grahamk@nvidia.com>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The workspace now uses Rust 2024, resolver 3, and rand 0.10. Rust code adopts updated random APIs, precise lifetime captures, and let-chain conditionals. Runtime behavior remains unchanged.

Changes

Rust modernization

Layer / File(s) Summary
Toolchain and random API updates
Cargo.toml, crates/libsy/src/algorithms/rand.rs, crates/switchyard-components/src/request_processors/random_routing.rs
The workspace uses Rust 2024, resolver 3, and rand 0.10 APIs.
libsy control flow and stream signatures
crates/libsy/src/algorithms/util/*, crates/libsy/src/core/*, crates/libsy/src/observability.rs
Nested conditions use let-chain syntax. Stream return types declare precise lifetime captures.
Switchyard component conditionals
crates/switchyard-components/src/{backends,contracts,intake,request_processors,response_processors,stats}/*, crates/switchyard-components/tests/support/mod.rs
Validation, authentication, response construction, statistics, and test support use equivalent chained conditions.
Translation codec control flow
crates/switchyard-translation/src/codecs/*, crates/switchyard-translation/src/helpers.rs, crates/switchyard-translation/src/util.rs
Buffered and streaming codecs use let-chain conditions without changing emitted content or events.
Client, Python, and server updates
crates/libsy-llm-client/src/error.rs, crates/switchyard-py/src/*, crates/switchyard-server/src/*
Error handling, serialization, shutdown, environment tests, and observability flushing use updated Rust syntax.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit hops through Rust so bright,
With chained lets arranged just right.
Rand seeds dance, streams capture air,
New editions bloom everywhere.
No behavior changed along the way—
Clean code celebrates today!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary change: upgrading the Rust edition to 2024.
Docstring Coverage ✅ Passed Docstring coverage is 88.64% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
crates/switchyard-components/src/stats/accumulator.rs (1)

588-592: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the latency-sampling invariant.

Add a short comment stating that this branch replaces the smallest retained sample so the heap keeps the largest MAX_LATENCY_SAMPLES values.

As per coding guidelines, comments are required for non-obvious private helpers and complex logic.

Proposed comment
         } else if let Some(smallest_sample) = self.samples.peek()
             && latency_ms > smallest_sample.0.value()
                 && let Some(mut smallest_sample) = self.samples.peek_mut() {
+                    // Retain the largest samples by replacing the smallest retained value.
                     *smallest_sample = sample;
                 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/switchyard-components/src/stats/accumulator.rs` around lines 588 -
592, The replacement branch in the latency sampling logic needs a concise
comment documenting its invariant. Add the comment immediately above the branch
using self.samples and smallest_sample to state that it replaces the smallest
retained sample, keeping the largest MAX_LATENCY_SAMPLES values in the heap.

Source: Coding guidelines

crates/switchyard-translation/src/util.rs (1)

425-428: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the stable collision mapping.

mapped_tool_id adds a stable suffix when different raw IDs sanitize to the same candidate. Add a concise comment above the helper so this wire-ID invariant remains explicit.

Suggested comment
+// Keeps sanitized tool IDs unique and stable when raw IDs collide.
 fn mapped_tool_id(

As per coding guidelines, add comments for non-obvious private helpers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/switchyard-translation/src/util.rs` around lines 425 - 428, Document
the stable collision mapping in the private helper containing the mapped_tool_id
logic, immediately above the collision-handling code near used_ids and
stable_suffix. Add a concise comment stating that different raw IDs sanitizing
to the same candidate receive a stable suffix, preserving the wire-ID invariant.

Source: Coding guidelines

crates/switchyard-translation/src/helpers.rs (1)

172-181: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use a let-chain for trailing-frame parsing.

The workspace uses Rust 1.96.1 and edition 2024. Remove #[allow(clippy::collapsible_if)] and combine the non-empty check with the parser binding.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/switchyard-translation/src/helpers.rs` around lines 172 - 181, Update
the trailing-frame handling around codec.decode_event to remove the
#[allow(clippy::collapsible_if)] attribute and combine the
frame.trim_end().is_empty() guard with the parse_json_sse_frame binding using a
Rust let-chain. Preserve the existing error mapping and event-yielding behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/libsy/src/algorithms/rand.rs`:
- Line 82: Replace panicking rand::make_rng() calls at
crates/libsy/src/algorithms/rand.rs:82 and
crates/switchyard-components/src/request_processors/random_routing.rs:118 with
fallible SeedableRng::try_from_rng initialization using SysRng, mapping failures
into each constructor’s existing error type; preserve the current successful
initialization behavior.

In `@crates/switchyard-server/src/config.rs`:
- Around line 736-748: Update the empty API key test around VALID_CONFIG and
error_message so it no longer calls std::env::set_var or std::env::remove_var.
Inject the API-key environment value through the configuration or validation
dependency, or run this case in an isolated subprocess, while preserving the
assertion that an empty API key produces the expected error message.

---

Nitpick comments:
In `@crates/switchyard-components/src/stats/accumulator.rs`:
- Around line 588-592: The replacement branch in the latency sampling logic
needs a concise comment documenting its invariant. Add the comment immediately
above the branch using self.samples and smallest_sample to state that it
replaces the smallest retained sample, keeping the largest MAX_LATENCY_SAMPLES
values in the heap.

In `@crates/switchyard-translation/src/helpers.rs`:
- Around line 172-181: Update the trailing-frame handling around
codec.decode_event to remove the #[allow(clippy::collapsible_if)] attribute and
combine the frame.trim_end().is_empty() guard with the parse_json_sse_frame
binding using a Rust let-chain. Preserve the existing error mapping and
event-yielding behavior.

In `@crates/switchyard-translation/src/util.rs`:
- Around line 425-428: Document the stable collision mapping in the private
helper containing the mapped_tool_id logic, immediately above the
collision-handling code near used_ids and stable_suffix. Add a concise comment
stating that different raw IDs sanitizing to the same candidate receive a stable
suffix, preserving the wire-ID invariant.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c4eaf03a-2fe1-48cc-94a1-67a5ce3ebf48

📥 Commits

Reviewing files that changed from the base of the PR and between a67536f and 9c2702d.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock, !Cargo.lock
📒 Files selected for processing (30)
  • Cargo.toml
  • crates/libsy-llm-client/src/error.rs
  • crates/libsy/src/algorithms/rand.rs
  • crates/libsy/src/algorithms/util/affinity.rs
  • crates/libsy/src/algorithms/util/tool_signals.rs
  • crates/libsy/src/core/algorithm.rs
  • crates/libsy/src/core/driver.rs
  • crates/libsy/src/observability.rs
  • crates/switchyard-components/src/backends/common.rs
  • crates/switchyard-components/src/backends/context_overflow.rs
  • crates/switchyard-components/src/contracts/types.rs
  • crates/switchyard-components/src/intake/client.rs
  • crates/switchyard-components/src/intake/payload.rs
  • crates/switchyard-components/src/request_processors/random_routing.rs
  • crates/switchyard-components/src/response_processors/stats.rs
  • crates/switchyard-components/src/stats/accumulator.rs
  • crates/switchyard-components/tests/support/mod.rs
  • crates/switchyard-py/src/py_serde.rs
  • crates/switchyard-py/src/server_bindings.rs
  • crates/switchyard-server/src/config.rs
  • crates/switchyard-server/src/metrics.rs
  • crates/switchyard-server/src/observability.rs
  • crates/switchyard-translation/src/codecs/anthropic/buffered.rs
  • crates/switchyard-translation/src/codecs/anthropic/stream.rs
  • crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
  • crates/switchyard-translation/src/codecs/openai_chat/stream.rs
  • crates/switchyard-translation/src/codecs/responses/buffered.rs
  • crates/switchyard-translation/src/codecs/responses/stream.rs
  • crates/switchyard-translation/src/helpers.rs
  • crates/switchyard-translation/src/util.rs

Comment thread crates/libsy/src/algorithms/rand.rs
Comment thread crates/switchyard-server/src/config.rs
@ayushag-nv
ayushag-nv enabled auto-merge (squash) July 31, 2026 20:48
@ayushag-nv
ayushag-nv merged commit d363dfc into main Jul 31, 2026
17 checks passed
@ayushag-nv
ayushag-nv deleted the gk-rust-2024 branch July 31, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants