Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice::fill documentation does not mention usage with uninitialized memory #119423

Closed
FreezyLemon opened this issue Dec 29, 2023 · 4 comments
Closed
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@FreezyLemon
Copy link

FreezyLemon commented Dec 29, 2023

Location

https://doc.rust-lang.org/std/primitive.slice.html#method.fill

Summary

slice::fill has an alias memset which implies it can be used when self's underlying memory is uninitialized.

The description for slice::fill is very short and does not mention this use case, so it seems unclear if this is intended behaviour or not. Some external discussions already mention the function as a safe way to initialize memory with a T: Clone type.

@FreezyLemon FreezyLemon added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 29, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 29, 2023
@saethlin
Copy link
Member

saethlin commented Dec 29, 2023

slice::fill has an alias memset which implies it can be used when self's underlying memory is uninitialized.

That would contradict the documentation for addr_of!, which clearly identifies such usage as UB: https://doc.rust-lang.org/1.75.0/core/ptr/macro.addr_of.html (though that limitation will probably not exist forever, this topic is discussed constantly, for example #119241 (comment))

Some external discussions already mention the function as a safe way to initialize memory

I don't see any mention of uninitialized memory in the linked discussion?

@Noratrieb
Copy link
Member

slice::fill has an alias memset which implies

Documentation aliases don't imply anything. They just help people find stuff they might want. Someone might want to set some bytes in a slice which they'd do with memset in C, so having an alias might help them to find what they need. But doc aliases don't suggest that the function is actually always equivalent to the aliased function.

@the8472
Copy link
Member

the8472 commented Dec 30, 2023

If you have uninitialized memory you can refer to it as &mut [MaybeUninit<T>] and then slice::fill that with a MaybeUninit<T> value. But you shouldn't use that for !Copy values.

@Noratrieb Noratrieb added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 30, 2023
@FreezyLemon
Copy link
Author

Uh, ok. I guess I mixed something up there. I saw the RFC for the somewhat related copy_from_slice which included fill at some point. That did include a mention of using it for safely initializing memory, and I assumed the later implementation still carried this feature.

Based on that assumption I thought this was an oversight in the docs or something, but if that's not the behaviour anyways, the entire issue is invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

5 participants