hello, I come in a understanding of PhantomData which I know I am wrong, but I can't find a proof to show where is the error: I get a conclusion: PhantomData has no effect in practice. Anybody can explain why PhantomData is useful?
Let me introduce the theory:
At first, let's talk without #[may_dangle], It is unstable and may been moved in the future. Since PhantomData is stable, It should have meanings without it.
Ok, there is a struct like this:
pub struct Box<T: fmt::Debug> {
ptr: *mut T,
_marker: marker::PhantomData<T>
}
then what is the effect with _marker?
In general, it is said the effect is making rustc believe Box owning the T.
But that effect does not force rustc to call drop_in_place(ptr) automatically, author of Box still has to call it by hand --- same as without _marker. If the author forget the call, rustc will not report, even with _marker being present.
If it is not for the author to force correctness, then it is for the user of Box?
While, if it has some effect, the effect should be disallow the user dropping T because the author will dropping it.
Ok, let's examine how the _marker is initialized. Obviously, it initialized via ptr, while, ptr is initialized with either borrow or move.
If it is from borrow, then the ownership is still held in user side; and Box can't get the ownership from a borrow. If boxes author want to get the ownership for _marker, he will get a compiling error: it is just the general rules taking effect.
If it is from moved, then the user side does not hold the ownership anymore, then the only drop take place in box, e.g. user side will not try to drop T, and _marker is useless because there is never a place for it to show itself useful.