Skip to content

docs: document preferences for Zeroable::zeroed vs pin_init::zeroed#158

Open
nicoan wants to merge 1 commit into
Rust-for-Linux:mainfrom
nicoan:zeroed_docs
Open

docs: document preferences for Zeroable::zeroed vs pin_init::zeroed#158
nicoan wants to merge 1 commit into
Rust-for-Linux:mainfrom
nicoan:zeroed_docs

Conversation

@nicoan

@nicoan nicoan commented Jun 30, 2026

Copy link
Copy Markdown

Modify the comments in pin_init::zeroed and Zeroable::zeroed functions to clarify to the user when one is preferred over the other.

This also adapts the example code in Zeroable::zeroed doc comments to use that function.

Link: https://lore.kernel.org/rust-for-linux/CANiq72m80vip+hz5RngvgH4VxU30amzS-ePr6KVKwKvhf3=Ycg@mail.gmail.com/T/#t
Suggested-by: Miguel Ojeda ojeda@kernel.org

Comment thread src/lib.rs Outdated
/// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
///
/// This function is preferred over [`pin_init::zeroed()`], unless initializing in a
/// `const` context or if the object is pinned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean when "if the object is pinned"?

@nicoan nicoan Jul 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that it refers to an object that is pinned in memory and initialized in-place. I tried not to deviate too much from Alexandre's comment in the mailing list thread.

Comment thread src/lib.rs Outdated
/// Whenever a type implements [`Zeroable`], this function should be preferred over
/// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
///
/// This function should be used in `const` contexts (as it is a `const fn`) or when

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to just gently steer people towards Zeroable::zeroed and not be too explicit. Saying something like "This function is a const version of [Zeroable::zeroed] while const trait is unstable" would be better.

Also, you should add that to init_zeroed, too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the distinction of a const context apply to init_zeroed as well? I don't see a const fn init_zeroed. I looked for usages in the Linux kernel of when pin_init::init_zeroed vs Zeroable::init_zeroed is used, and I saw that pin_init::init_zeroed is usually used within the init! or pin_init! macros along with the <- operator, and Zeroable::init_zeroed in struct update syntax.

Comment aside, can zeroed be inlined? I see the macro for init_zeroed but not for zeroed

Apologies if the questions are not quite right, I am still wrapping my head around the kernel.

Comment thread src/lib.rs Outdated
/// Whenever a type implements [`Zeroable`], this function should be preferred over
/// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
///
/// This function is preferred over [`pin_init::zeroed()`], unless initializing in a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other comment, this just needs to hint to people about existence of another function. Say something like "As const trait is not yet stable, [pin_init::zeroed] can be used in const context" is sufficient. Also need to change init_zeroed.

Modify the comments in `pin_init::zeroed` and `Zeroable::zeroed`
functions to clarify to the user when one is preferred over the other.

This also adapts the example code in `Zeroable::zeroed` doc comments to
use that function.

Link: https://lore.kernel.org/rust-for-linux/CANiq72m80vip+hz5RngvgH4VxU30amzS-ePr6KVKwKvhf3=Ycg@mail.gmail.com/T/#t
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants