Skip to contentSkip to conversation

rust: dma: convert the read/write macros to return Result

PATCH Benno Lossin 2 participants 2 messages Apr 10, 2025 Rust

lore
Share & tools
lore
#0 lore raw
Quoted 35 lines · Danilo Krummrich
Quoted 27 lines · Benno Lossin
Quoted 26 lines · Danilo Krummrich
Quoted 8 lines · Abdiel Janulgue
(quoted diff) +0 -4
@@ -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);

Review impl Drop for DmaSampleDriver { · line 83

Quoted 10 lines
(quoted diff) +7 -3
-            }
-            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);
	}
Quoted 6 lines
Quoted 3 lines

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.

Quoted 7 lines

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);
	}

Ah right, missed that.

Quoted 2 lines

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.

Yeah, I'm not sure if this is that valuable. I think having "real code" is better, but I don't have any idea what to do in this case.

Why does this sample do the validation in the drop method in the first place? I guess the same code on the C side would do this in remove or whatever the equivalent thing is there, but would there be the option to report an error? Or is remove an infallible operation? In that case assert! probably is still the best option.

---
Cheers,
Benno

#1 lore raw
Quoted 46 lines · Benno Lossin
Quoted 35 lines · Danilo Krummrich
Quoted 27 lines · Benno Lossin
Quoted 26 lines · Danilo Krummrich
Quoted 8 lines · Abdiel Janulgue
(quoted diff) +0 -4
@@ -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);

Review impl Drop for DmaSampleDriver { · line 83

Quoted 10 lines
(quoted diff) +7 -3
-            }
-            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);
	}
Quoted 6 lines
Quoted 3 lines

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.

Quoted 7 lines

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);
	}

Ah right, missed that.

Quoted 8 lines
Quoted 2 lines

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.

Yeah, I'm not sure if this is that valuable. I think having "real code" is better, but I don't have any idea what to do in this case.

Why does this sample do the validation in the drop method in the first place?

I assume there is no specific reason, maybe Abdiel wanted to have a bit more lifecycle for the allocation than just probe().

I guess we could just move it to probe(). Alternatively we can also keep it in a closure or function and only assert! once for the returned Result.

Quoted 4 lines

I guess the same code on the C side would do this in remove or whatever the equivalent thing is there, but would there be the option to report an error? Or is remove an infallible operation? In that case assert! probably is still the best option.

remove() is and has to be infallible, yes.