rust-lang / rust-lang/nomicon

Dubious justification for using `NonNull` in `Arc`

Open
#323 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
2.3k
Forks
325
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the Implementing Arc and Mutex / Arc / Layout page and compare its explanation of *mut ArcInner<T>, *const ArcInner<T>, and NonNull<T>. Revise the justification so the variance and non-null claims are technically accurate, then verify that the page clearly explains why the chosen representation is used.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.