From: Claude Code Review Bot <claude-review@example.com>
To: dri-devel-reviews@example.com
Subject: Claude review: rust: ptr: add panicking index projection variant
Date: Thu, 04 Jun 2026 12:43:27 +1000 [thread overview]
Message-ID: <review-patch3-20260602-projection-syntax-rework-v2-3-6989470f5440@garyguo.net> (raw)
In-Reply-To: <20260602-projection-syntax-rework-v2-3-6989470f5440@garyguo.net>
Patch Review
This is the core patch. It adds:
1. A new `fn index()` method to the `ProjectIndex` trait (panicking variant)
2. Implementations for `usize`, `Range<usize>`, `RangeTo`, `RangeFrom`, `RangeFull`
3. New `[panic: ...]`, `[try: ...]`, `[build: ...]` keyword syntax in the `project_pointer!` macro
4. Backward-compatible shims for the old `[i]?` and `[i]` syntax
5. A new `dma_write!` arm for the keyworded syntax
6. Updated documentation and examples
**The `usize` index implementation** is clean:
```rust
fn index(self, slice: *mut [T]) -> *mut T {
// Leverage Rust built-in operators for bounds checking.
// SAFETY: All non-null and aligned pointers are valid for ZST read.
let zst_slice =
unsafe { core::slice::from_raw_parts::<()>(core::ptr::dangling(), slice.len()) };
let () = zst_slice[self];
slice.cast::<T>().wrapping_add(self)
}
```
This is a clever approach -- creating a ZST slice to leverage Rust's built-in panicking index operator for bounds checking without actually reading any real data. The `let () = zst_slice[self]` line is particularly neat: it forces the bounds check and panics if out of bounds, while the unit-type assignment ensures no actual memory read happens. The safety comment is adequate.
**The `Range<usize>` index implementation** uses a different pattern:
```rust
fn index(self, slice: *mut [T]) -> *mut [T] {
let zst_slice =
unsafe { core::slice::from_raw_parts::<()>(core::ptr::dangling(), slice.len()) };
_ = zst_slice[self.clone()];
// SAFETY: bounds checked.
unsafe { self.get(slice).unwrap_unchecked() }
}
```
Minor concern: the `self.clone()` is needed because `Range` is consumed by the index operator, but this does mean `Range` must implement `Clone`. This is fine since `Range<usize>` derives `Clone`, but it's worth noting. The `unwrap_unchecked` after the bounds check is correct -- if the ZST indexing didn't panic, the range is valid. The `_ =` discard is correct here since the ZST slice indexing returns `&[()]` which we don't need.
**Macro structure**: The compatibility shims are well-placed at the bottom, after the new keyword forms, ensuring the new syntax takes priority in macro matching:
```rust
// For compatibility
(@gen $ptr:ident, [$index:expr]? $($rest:tt)*) => {
$crate::ptr::project!(@gen $ptr, [try: $index] $($rest)*)
};
```
Good that these delegate to the new forms rather than duplicating logic.
**Documentation**: The updated doc examples are clear and demonstrate all three flavors. The macro documentation accurately describes the new syntax.
One nit on the cover letter: it says "comparde" (typo for "compared").
---
Generated by Claude Code Patch Reviewer
next prev parent reply other threads:[~2026-06-04 2:43 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-02 14:17 [PATCH v2 0/6] Rework index projection syntax Gary Guo
2026-06-02 14:17 ` [PATCH v2 1/6] rust: ptr: rename `ProjectIndex::index` to `build_index` Gary Guo
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
2026-06-02 14:17 ` [PATCH v2 2/6] rust: ptr: use `match` instead of `unwrap_or_else` for `build_index` Gary Guo
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
2026-06-02 14:17 ` [PATCH v2 3/6] rust: ptr: add panicking index projection variant Gary Guo
2026-06-04 2:43 ` Claude Code Review Bot [this message]
2026-06-02 14:17 ` [PATCH v2 4/6] rust: dma: update to keyworded index projection syntax Gary Guo
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
2026-06-02 14:17 ` [PATCH v2 5/6] gpu: nova-core: convert to keyworded " Gary Guo
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
2026-06-02 14:17 ` [PATCH v2 6/6] rust: ptr: remove implicit index " Gary Guo
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
2026-06-02 15:03 ` [PATCH v2 0/6] Rework " Danilo Krummrich
2026-06-04 2:43 ` Claude review: " Claude Code Review Bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=review-patch3-20260602-projection-syntax-rework-v2-3-6989470f5440@garyguo.net \
--to=claude-review@example.com \
--cc=dri-devel-reviews@example.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox