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

Documentation of NonNull is difficult to understand #52978

Closed
kornelski opened this issue Aug 2, 2018 · 2 comments
Closed

Documentation of NonNull is difficult to understand #52978

kornelski opened this issue Aug 2, 2018 · 2 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@kornelski
Copy link
Contributor

kornelski commented Aug 2, 2018

https://doc.rust-lang.org/std/ptr/struct.NonNull.html

If you're not sure if you should use NonNull<T>, just use *mut T!

This is a cop out. I am unsure if I should use NonNull, but mainly because the rest of the documentation is hard to digest.

The whole explanation of NonNull doesn't use any terminology or examples that a C programmer can relate to. It feels written from perspective of a language designer, not a language user.

Unlike *mut T, NonNull<T> is covariant over T.

This property is explained entirely using type theory jargon, and advanced usage of PhantomData. The documentation assumes that the reader understands the subtle but crucial difference between PhantomData<Cell<T>> and PhantomData<T>, which by itself requires understanding of covariance — a catch-22.

This documentation doesn't explain covariance, and unfortunately other sources aren't good either. The Wikipedia page requires a lot of mental effort to extract relevant information from (covariance is explained in terms of "that's how OCaml works" or "preserves the ordering of types (≤) [links deeper into jargon-filled type theory article]").

Usually this won't be necessary; covariance is correct for most safe abstractions, such as Box, Rc, Arc, Vec, and LinkedList.

The Wikipedia page has a whole section explaining that Arrays are only safe if they are invariant, and this note seems to be contradicting it, so this is further confusing (probably because the documentation refers to variance of lifetimes, not inheritance? But this is not documented)

Could usage of NonNull be explained without relying on type variance terminology? (and subtle typesystem interactions of PhantomData directly related to it?), e.g. what specific usage would violate the coveriance requirement? What would happen then?

@GuillaumeGomez GuillaumeGomez added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Aug 2, 2018
@frewsxcv
Copy link
Member

frewsxcv commented Sep 3, 2018

Similar issue: #48929

@steveklabnik
Copy link
Member

Yes, closing as a duplicate.

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
Projects
None yet
Development

No branches or pull requests

4 participants