Skip to content

docs: link Zeroable::zeroed and pin_init::zeroed in documentation#158

Open
nicoan wants to merge 2 commits into
Rust-for-Linux:mainfrom
nicoan:zeroed_docs
Open

docs: link Zeroable::zeroed and pin_init::zeroed in documentation#158
nicoan wants to merge 2 commits into
Rust-for-Linux:mainfrom
nicoan:zeroed_docs

Conversation

@nicoan

@nicoan nicoan commented Jun 30, 2026

Copy link
Copy Markdown

This PR contains two commits.

docs: link Zeroable::zeroed and pin_init::zeroed in documentation

Modify the comments in the pin_init::zeroed and Zeroable::zeroed
functions to cross-reference each other and make developers aware of
both options.

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

mark pin_init::zeroed as #[inline]

The zeroed function is a trivial wrapper around
unsafe { core::mem::zeroed() }. Mark it as #[inline] to avoid
generating unnecessary symbols for it.

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.

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.

You're correct. I created a topic https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/deprecate.20.60pin_init.3A.3Ainit_zeroed.60.3F/with/609626298 on whether we should keep or soft-deprecate the init_zeroed method. This PR can be purely about zeroed.

Comment thread src/lib.rs Outdated
Comment thread src/lib.rs
/// assert_eq!(point.x, 0);
/// assert_eq!(point.y, 0);
/// ```
#[inline]

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.

The standalone zeroed should also be inlined. Also, could you split this to a separate commit please.

nicoan added 2 commits July 13, 2026 11:47
Modify the comments in the `pin_init::zeroed` and `Zeroable::zeroed`
functions to cross-reference each other and make developers aware of
both options.

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>
The `zeroed` function is a trivial wrapper around
`unsafe { core::mem::zeroed() }`. Mark it as `#[inline]` to avoid
generating unnecessary symbols for it.

Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
@nicoan nicoan changed the title docs: document preferences for Zeroable::zeroed vs pin_init::zeroed docs: link Zeroable::zeroed and pin_init::zeroed in documentation Jul 13, 2026
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