Re: [PATCH v2 2/3] rust: dma: convert the read/write macros to return Result

Danilo Krummrich
2025-04-10T15:34:33Z

On Thu, Apr 10, 2025 at 03:11:01PM +0000, Benno Lossin wrote:
> On Thu Apr 10, 2025 at 1:54 PM CEST, Danilo Krummrich wrote:
> > On Thu, Apr 10, 2025 at 11:58:17AM +0300, Abdiel Janulgue wrote:
> >> @@ -78,13 +74,14 @@ impl Drop for DmaSampleDriver {
> >> fn drop(&mut self) {
> >> dev_info!(self.pdev.as_ref(), "Unload DMA test driver.\n");
> >>
> >> - let _ = || -> Result {
> >> - for (i, value) in TEST_VALUES.into_iter().enumerate() {
> >> - assert_eq!(kernel::dma_read!(self.ca[i].h), value.0);
> >> - assert_eq!(kernel::dma_read!(self.ca[i].b), value.1);
> >> - }
> >> - Ok(())
> >> - }();
> >> + for (i, value) in TEST_VALUES.into_iter().enumerate() {
> >> + let val0 = kernel::dma_read!(self.ca[i].h);
> >> + let val1 = kernel::dma_read!(self.ca[i].b);
> >> + assert!(val0.is_ok());
> >> + assert!(val1.is_ok());
> >> + assert_eq!(val0.unwrap(), value.0);
> >> + assert_eq!(val1.unwrap(), value.1);
> >
> > Maybe use if-let to avoid the unwrap?
> >
> > if let Ok(val0) = val0 {
> > assert_eq!(val0, value.0);
> > }
> >
> > I know it's a bit pointless, since we know it must be ok, but the educational
> > message of the example should be to check and not to unwrap, so maybe that's
> > better.
>
> The if-let will silently ignore any errors, so I don't think that it's
> fit for example code either.

Yes, but we still have the assert!() before, so the full sequence would be:

assert!(val0.is_ok());

if let Ok(val0) = val0 {
assert_eq!(val0, value.0);
}

The intention would be to avoid patterns that shouldn't be used in "real" code;
assert!() should be obvious not to use for real code.