rust: miscdevice: use `pin_init::zeroed()` for C type initialization
Subsystems (2)
PATCH 1/1 rust: miscdevice: use `pin_init::zeroed()` for C type initialization
Replace manual zero-initialization using MaybeUninit::zeroed().assume_init() with pin_init::zeroed(). The pin_init helper provides a safer and clearer API for zero-initializing C structs without requiring an unsafe block.
--- rust/kernel/miscdevice.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
rust/kernel/miscdevice.rs +3 -3
@@ -20,7 +20,7 @@ seq_file::SeqFile, types::{ForeignOwnable, Opaque}, }; -use core::{marker::PhantomData , mem::MaybeUninit , pin::Pin }; +use core::{marker::PhantomData , pin::Pin };/// Options for creating a misc device. #[derive( Copy , Clone )]
@@ -33,7 +33,7 @@ impl MiscDeviceOptions {/// Create a raw `struct miscdev` ready for registration. pub const fn into_raw <T :MiscDevice >(self) -> bindings::miscdevice {// SAFETY: All zeros is valid for this C type. -let mut result: bindings::miscdevice =unsafe {MaybeUninit ::zeroed ().assume_init () }; +let mut result: bindings::miscdevice = pin_init::zeroed (); result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int ; result.name =crate ::str::as_char_ptr_in_const_context (self.name); result.fops =MiscdeviceVTable ::<T >::build ();
@@ -421,7 +421,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {None },// SAFETY: All zeros is a valid value for `bindings::file_operations`. - ..unsafe {MaybeUninit ::zeroed ().assume_init () } + ..pin_init::zeroed () };const fn build () -> &'static bindings::file_operations {
Signature · 1 line
2.43.0
Quoted 8 lines · Atharv Dubey
Replace manual zero-initialization using
MaybeUninit::zeroed().assume_init()withpin_init::zeroed(). Thepin_inithelper provides a safer and clearer API for zero-initializing C structs without requiring anunsafeblock.
Welcome! I am glad you ended up sending a patch :)
Minor procedural nit: I think this is missing:
It should be picked up automatically, no need to resend a v2 just for this.
Thanks!
Cheers,
Miguel
Quoted 25 lines · Atharv Dubey
Replace manual zero-initialization using
MaybeUninit::zeroed().assume_init()withpin_init::zeroed(). Thepin_inithelper provides a safer and clearer API for zero-initializing C structs without requiring anunsafeblock.Signed-off-by Atharv Dubey <[email protected]>--- rust/kernel/miscdevice.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)rust/kernel/miscdevice.rs +1
-1@@ -20,7 +20,7 @@ seq_file::SeqFile, types::{ForeignOwnable, Opaque}, }; -use core::{marker::PhantomData , mem::MaybeUninit , pin::Pin }; +use core::{marker::PhantomData , pin::Pin };/// Options for creating a misc device. #[derive( Copy , Clone )]
rust/kernel/miscdevice.rs impl MiscDeviceOptions { · line 36
Quoted 5 lines
rust/kernel/miscdevice.rs +1
-1@@ -33,7 +33,7 @@ impl MiscDeviceOptions {/// Create a raw `struct miscdev` ready for registration. pub const fn into_raw <T :MiscDevice >(self) -> bindings::miscdevice {// SAFETY: All zeros is valid for this C type. -let mut result: bindings::miscdevice =unsafe {MaybeUninit ::zeroed ().assume_init () }; +let mut result: bindings::miscdevice = pin_init::zeroed ();
While getting rid of the unsafewhat's about dropping the SAFETY comment as well? As done by Benno in [1]?
Cheers
Dirk
Quoted 12 lines
rust/kernel/miscdevice.rs +1
-1result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int ; result.name =crate ::str::as_char_ptr_in_const_context (self.name); result.fops =MiscdeviceVTable ::<T >::build ();@@ -421,7 +421,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {None },// SAFETY: All zeros is a valid value for `bindings::file_operations`. - ..unsafe {MaybeUninit ::zeroed ().assume_init () } + ..pin_init::zeroed () };const fn build () -> &'static bindings::file_operations {
Quoted 3 lines · Dirk Behme
While getting rid of the
unsafewhat's about dropping theSAFETYcomment as well? As done by Benno in [1]?
Yeah -- Atharv: please make sure to check your patches with CLIPPY=1 (which should detect that), rustdoc, rusttest... Some notes at:
https://rust-for-linux.com/contributing#submit-checklist-addendum
Thanks!
Cheers,
Miguel
Quoted 8 lines · Miguel Ojeda
Quoted 3 lines · Dirk Behme
While getting rid of the
unsafewhat's about dropping theSAFETYcomment as well? As done by Benno in [1]?Yeah -- Atharv: please make sure to check your patches with
CLIPPY=1(which should detect that),rustdoc,rusttest... Some notes at:
Quoted 8 lines
https://rust-for-linux.com/contributing#submit-checklist-addendum
Thanks!
Cheers,
MiguelSure,will keep this in mind while sending out patches.
-Atharv