rust-lang / rust-lang/nomicon

The PhantomData section is missing clarification or is wrong!

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

Nobody has claimed this yet.

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

Description

In the book:

In order to tell dropck that we do own values of type T, and therefore may drop some T's when we drop, we must add an extra PhantomData saying exactly that

however:

#[repr(C)]
pub struct Unique<T> {
    ptr         : *mut T,
    _marker     : ::core::marker::PhantomData<T>,
}

impl<T> Unique<T> {
    pub fn new(ptr: *mut T) -> Self { Self { ptr : ptr, _marker: ::core::marker::PhantomData } }
    pub fn getMutPtr(&mut self) -> *mut T { self.ptr }
    pub fn getPtr(&self) -> *const T { self.ptr }
}

#[repr(C)]
pub struct Vec<T> {
    elements    : Unique<T>,
    count       : usize,
    capacity    : usize,
}
...

Will compile and Vec<T> can be used and dropped without any problem. So, what's the point of PhantomData there ? unless I implement Drop on Vec the code with or without PhantomData will compile just fine!

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

Start at the PhantomData section containing the quoted dropck explanation and compare it with the Unique and Vec example in the issue. Determine whether the text should explain the difference made by implementing Drop, then update the section so its claim matches the demonstrated compilation and dropck behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.