Skip to content

Dubious justification for using NonNull in Arc #323

Open
@samueltardieu

Description

@samueltardieu

In the page Implementing Arc and Mutex / Arc / Layout, the justification for using Arc is the following:

pub struct Arc<T> { ptr: *mut ArcInner<T>, }

This would compile, however it would be incorrect. First of all, the compiler will give us too strict variance. For example, an Arc<&'static str> couldn't be used where an Arc<&'a str> was expected.

To fix the first problem, we can use NonNull. Note that NonNull is a wrapper around a raw pointer that declares that:

  • We are variant over T
  • Our pointer is never null

What bugs me here is that ptr wouldn't need to be *mut ArcInner<T>, being *const ArcInner<T> would be enough and wouldn't have the variance problem. The justification for the pointer never being null is right though, but the one about variance is dubious.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions