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

Improve documentation of std::ptr::NonNull #48929

Closed
crlf0710 opened this issue Mar 11, 2018 · 6 comments
Closed

Improve documentation of std::ptr::NonNull #48929

crlf0710 opened this issue Mar 11, 2018 · 6 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-FFI Area: Foreign function interface (FFI) A-raw-pointers Area: raw pointers, MaybeUninit, NonNull C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@crlf0710
Copy link
Member

This type used to be experimental and internal (when it was called Shared), now that it is finally being stablized (see #27730, #47631), people are complaining that the current document texts are not easy to understand.

Current text *mut T but non-zero and covariant. talks about the covariance, which is not mentioned elsewhere in the libstd docs and the Rust books. (I know it's mentioned in Nomicon but beginners seldom have read it.)

I suggest the docs be either supplemented with concrete explanations or reworked upon.

@crlf0710 crlf0710 changed the title Documentation of std::ptr::NonNull Improve documentation of std::ptr::NonNull Mar 11, 2018
@frewsxcv frewsxcv added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Mar 11, 2018
@frewsxcv
Copy link
Member

worth mentioning that the nomicon has a section on covriance: https://doc.rust-lang.org/beta/nomicon/subtyping.html

@mjbshaw
Copy link
Contributor

mjbshaw commented Apr 20, 2018

One question I have that I'd love to be explicitly answered:

Does NonNull<T> have the same ABI as *mut T?

The Nomicon says "If T is an FFI-safe non-nullable pointer type, Option<T> is guaranteed to have the same layout and ABI as T and is therefore also FFI-safe." This documentation and explicit statement gives some guarantees regarding Option, and it's really nice to be able to use Option in FFI code. I would love to have similar guarantees and documentation for NonNull so I could use it in some FFI-related code. But before I can use NonNull I need to make sure that I won't accidentally change my code's ABI.

@XAMPPRocky XAMPPRocky added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label May 14, 2018
@steveklabnik steveklabnik added the E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. label May 28, 2018
@steveklabnik steveklabnik added the P-medium Medium priority label Dec 27, 2018
@steveklabnik
Copy link
Member

Other issues: #52978

@jonas-schievink jonas-schievink added A-FFI Area: Foreign function interface (FFI) T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 6, 2020
@aticu
Copy link
Contributor

aticu commented Jul 13, 2020

The documentation of the as_ref and as_mut methods of NonNull are missing a Safety section. Presumably they require

  • the same safety considerations as std::ptr::read (both) and std::ptr::write (as_mut)
  • that the allocation of the value outlives the self lifetime
  • that there are no aliasing references that violate the immutable XOR mutable scheme

Did I miss anything?

@RalfJung
Copy link
Member

That sounds reasonable, I cannot think of anything else of the top of my head.

I would combine the last two points though:

  • For the duration of the self lifetime, the allocation continues to exist and there are no aliasing references that violate the immutable XOR mutable scheme for the memory pointed to by the returned reference.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 8, 2020
Add safety section to `NonNull::as_*` method docs

This basically adds the safety section of `*mut T::as_{ref,mut}` to the
same methods on `NonNull` with minor modifications to fit the
differences.

Part of rust-lang#48929.
@workingjubilee workingjubilee added the A-raw-pointers Area: raw pointers, MaybeUninit, NonNull label Apr 17, 2021
@Dylan-DPC
Copy link
Member

There have been several changes to this since this issue was created, and this should satisfy this issue. There is scope for more but a newer issue would be better than this one being buried deep in the issue tracker.

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 A-FFI Area: Foreign function interface (FFI) A-raw-pointers Area: raw pointers, MaybeUninit, NonNull C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

10 participants