From mboxrd@z Thu Jan 1 00:00:00 1970 From: Claude Code Review Bot 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 Message-ID: In-Reply-To: <20260602-projection-syntax-rework-v2-3-6989470f5440@garyguo.net> References: <20260602-projection-syntax-rework-v2-0-6989470f5440@garyguo.net> <20260602-projection-syntax-rework-v2-3-6989470f5440@garyguo.net> X-Mailer: Claude Code Patch Reviewer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 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`, `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::().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` 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` 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