Re: [PATCH v2 2/5] rust: dma: add DMA addressing capabilities
From: Danilo Krummrich
Date: Wed Jul 16 2025 - 14:32:22 EST
On Wed Jul 16, 2025 at 7:55 PM CEST, Danilo Krummrich wrote:
> On Wed Jul 16, 2025 at 7:32 PM CEST, Daniel Almeida wrote:
>> Hi Danilo,
>>
>>> + #[inline]
>>> + pub const fn new(n: usize) -> Result<Self> {
>>> + Ok(Self(match n {
>>> + 0 => 0,
>>> + 1..=64 => u64::MAX >> (64 - n),
>>> + _ => return Err(EINVAL),
>>> + }))
>>> + }
>>> +
>>
>> Isn’t this equivalent to genmask_u64(0..=n) ? See [0].
>
> Instead of the match this can use genmask_checked_u64() and convert the Option
> to a Result, once genmask is upstream.
>
>> You should also get a compile-time failure if n is out of bounds by default using
>> genmask.
>
> No, we can't use genmask_u64(), `n` is not guaranteed to be known at compile
> time, so we'd need to use genmask_checked_u64().
>
> Of course, we could have a separate DmaMask constructor, e.g. with a const
> generic -- not sure that's worth though.
On the other hand, it doesn't hurt. Guess I will add another constructor with a
const generic. :)
I also quickly tried genmask and I have a few questions:
(1) Why does genmask not use a const generic? I think this makes it more
obvious that it's only intended to be used from const context.
(2) Why is there no build_assert() when the range exceeds the number of bits
of the target type? I would expect genmask_u64(0..100) to fail.
(3) OOC, why did you choose u32 as argument type?