Reexplain NonNull
's covariance details more concisely
#141933
Labels
A-docs
Area: Documentation for any part of the project, including the compiler, standard library, and tools
A-raw-pointers
Area: raw pointers, MaybeUninit, NonNull
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Location
std::ptr::NonNull
https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html
Summary
This page spills a lot of digital ink on the matter of covariance versus invariance. The fact of the matter is it is usually correct to use, which is why it was chosen to be like that. There are abstractions that require invariance, but they must be designed carefully to begin with, and the current format of the description is very repetitive. In practice, we have not seen an explosion of bugs from people using
NonNull
incorrectly over the past 60 versions, and I do not believe it is primarily because of the documented cautions. I do, however, see people incorrectly using&mut T
,&T
when they wantNonNull<T>
.A more precise summarization would be more useful and less intimidating.
The text was updated successfully, but these errors were encountered: