Skip to contentSkip to conversation

usb: rust: Use pin_init::zeroed for usb_device_id initialization

PATCH In review Nicolás Antinori 4 participants 4 messages Jun 25 – Jul 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: rust: Use pin_init::zeroed for usb_device_id initialization

All types in bindings implement Zeroable if they can. This enables using pin_init::zeroed() for usb_device_id initialization instead of relying on ..unsafe { MaybeUninit::zeroed().assume_init() }.

This change improves readability and removes unnecessary unsafe blocks.

---
 rust/kernel/usb.rs | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)
rust/kernel/usb.rs +10 -21
index 7aff0c82d0af..bbf85366d2c9 100644
@@ -24,11 +24,8 @@
 };
 use core::{
     marker::PhantomData,
-    mem::{
-        offset_of,
-        MaybeUninit, //
-    },
-    ptr::NonNull,
+    mem::offset_of,
+    ptr::NonNull, //
 };
 
 /// An adapter for the registration of USB drivers.
@@ -130,8 +127,7 @@ pub const fn from_id(vendor: u16, product: u16) -> Self {
             match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE as u16,
             idVendor: vendor,
             idProduct: product,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -143,8 +139,7 @@ pub const fn from_device_ver(vendor: u16, product: u16, bcd_lo: u16, bcd_hi: u16
             idProduct: product,
             bcdDevice_lo: bcd_lo,
             bcdDevice_hi: bcd_hi,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -155,8 +150,7 @@ pub const fn from_device_info(class: u8, subclass: u8, protocol: u8) -> Self {
             bDeviceClass: class,
             bDeviceSubClass: subclass,
             bDeviceProtocol: protocol,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -167,8 +161,7 @@ pub const fn from_interface_info(class: u8, subclass: u8, protocol: u8) -> Self
             bInterfaceClass: class,
             bInterfaceSubClass: subclass,
             bInterfaceProtocol: protocol,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -180,8 +173,7 @@ pub const fn from_device_interface_class(vendor: u16, product: u16, class: u8) -
             idVendor: vendor,
             idProduct: product,
             bInterfaceClass: class,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -193,8 +185,7 @@ pub const fn from_device_interface_protocol(vendor: u16, product: u16, protocol:
             idVendor: vendor,
             idProduct: product,
             bInterfaceProtocol: protocol,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -206,8 +197,7 @@ pub const fn from_device_interface_number(vendor: u16, product: u16, number: u8)
             idVendor: vendor,
             idProduct: product,
             bInterfaceNumber: number,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 
@@ -227,8 +217,7 @@ pub const fn from_device_and_interface_info(
             bInterfaceClass: class,
             bInterfaceSubClass: subclass,
             bInterfaceProtocol: protocol,
-            // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
-            ..unsafe { MaybeUninit::zeroed().assume_init() }
+            ..pin_init::zeroed()
         })
     }
 }

--
2.47.3

reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Nicolás Antinori

All types in bindings implement Zeroable if they can. This enables using pin_init::zeroed() for usb_device_id initialization instead of relying on ..unsafe { MaybeUninit::zeroed().assume_init() }.

This change improves readability and removes unnecessary unsafe blocks.

#2 lore raw
In reply to Nicolás Antinori
Quoted 10 lines · Nicolás Antinori

All types in bindings implement Zeroable if they can. This enables using pin_init::zeroed() for usb_device_id initialization instead of relying on ..unsafe { MaybeUninit::zeroed().assume_init() }.

This change improves readability and removes unnecessary unsafe blocks.

Looks fine -- if needed I can pick it up via rust-next.

Thanks!

Cheers,
Miguel

#3 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Nicolás Antinori

All types in bindings implement Zeroable if they can. This enables using pin_init::zeroed() for usb_device_id initialization instead of relying on ..unsafe { MaybeUninit::zeroed().assume_init() }.

Quoted 7 lines
Quoted 5 lines

This change improves readability and removes unnecessary unsafe blocks.

Looks fine -- if needed I can pick it up via rust-next.

I'll take it, thanks.

greg k-h