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

Dubious justification for using NonNull in Arc #323

Open
samueltardieu opened this issue Nov 10, 2021 · 0 comments · May be fixed by #430
Open

Dubious justification for using NonNull in Arc #323

samueltardieu opened this issue Nov 10, 2021 · 0 comments · May be fixed by #430

Comments

@samueltardieu
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant