Re: [PATCH v2 1/2] rust: add projection infrastructure

From: Gary Guo

Date: Sun Mar 01 2026 - 08:10:56 EST


On Sat Feb 28, 2026 at 11:38 PM GMT, Aditya Rajan wrote:
> On Thu Feb 26, 2026 at 7:46 AM PST, Gary Guo wrote:
>> From: Gary Guo <gary@xxxxxxxxxxx>
>> diff --git a/rust/kernel/projection.rs b/rust/kernel/projection.rs
>> new file mode 100644
>> index 000000000000..186ec194f2b8
>> --- /dev/null
>> +++ b/rust/kernel/projection.rs
>> +unsafe impl<T> ProjectIndex<[T]> for usize {
>> + type Output = T;
>> +
>> + #[inline(always)]
>> + fn get(self, slice: *mut [T]) -> Option<*mut T> {
>> + if self > slice.len() {
> I am not sure but shouldn't this be `self >= slice.len()` ? I could be wrong but `slice.get(n)` should return `None` for slice 0..n ?
> There is a similar condition somewhere else in the code as well.

Good catch!

The code below

if self.end > slice.len() {
return None;
}

is rightfully `>` as it's okay to have end being equal to the length. I suppose
this is what you mean by "somewhere else in the code"?

This one though should indeed be `>=`, and I've obviously messed this one up.

Best,
Gary

>
> [...]
>
>> + // Check unaligned field. Not all users (e.g. DMA) can handle unaligned
>> + // projections. (e.g. DMA).
> Nit: Comment mentions (e.g. DMA) twice
>
> One question regarding the `in bounds` check otherwise LGTM
>
> Thanks
> Aditya