Skip to content

Reexplain NonNull's covariance details more concisely #141933

@workingjubilee

Description

@workingjubilee

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 want NonNull<T>.

A more precise summarization would be more useful and less intimidating.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions