Struct PhantomData
struct PhantomData<T: PointeeSized>
Zero-sized type used to mark things that "act like" they own a T.
Adding a PhantomData<T> field to your type tells the compiler that your
type acts as though it stores a value of type T, even though it doesn't
really. This information is used when computing certain safety properties.
For a more in-depth explanation of how to use PhantomData<T>, please see
the Nomicon.
A ghastly note 👻👻👻
Though they both have scary names, PhantomData and 'phantom types' are
related, but not identical. A phantom type parameter is simply a type
parameter which is never used. In Rust, this often causes the compiler to
complain, and the solution is to add a "dummy" use by way of PhantomData.
Examples
Unused lifetime parameters
Perhaps the most common use case for PhantomData is a struct that has an
unused lifetime parameter, typically as part of some unsafe code. For
example, here is a struct Slice that has two pointers of type *const T,
presumably pointing into an array somewhere:
struct Slice<'a, T> {
start: *const T,
end: *const T,
}
The intention is that the underlying data is only valid for the
lifetime 'a, so Slice should not outlive 'a. However, this
intent is not expressed in the code, since there are no uses of
the lifetime 'a and hence it is not clear what data it applies
to. We can correct this by telling the compiler to act as if the
Slice struct contained a reference &'a T:
use PhantomData;
#
This also in turn infers the lifetime bound T: 'a, indicating
that any references in T are valid over the lifetime 'a.
When initializing a Slice you simply provide the value
PhantomData for the field phantom:
#
# use PhantomData;
#
Unused type parameters
It sometimes happens that you have unused type parameters which
indicate what type of data a struct is "tied" to, even though that
data is not actually found in the struct itself. Here is an
example where this arises with FFI. The foreign interface uses
handles of type *mut () to refer to Rust values of different
types. We track the Rust type using a phantom type parameter on
the struct ExternalResource which wraps a handle.
#
#
# ;
#
#
use PhantomData;
Ownership and the drop check
The exact interaction of PhantomData with drop check may change in the future.
Currently, adding a field of type PhantomData<T> indicates that your type owns data of type
T in very rare circumstances. This in turn has effects on the Rust compiler's drop check
analysis. For the exact rules, see the drop check documentation.
Layout
For all T, the following are guaranteed:
size_of::<PhantomData<T>>() == 0align_of::<PhantomData<T>>() == 1
Implementations
impl<T> Any for PhantomData<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for PhantomData<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for PhantomData<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for PhantomData<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for PhantomData<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for PhantomData<T>
impl<T> Send for PhantomData<T>
impl<T> Sync for PhantomData<T>
impl<T> Unpin for PhantomData<T>
impl<T> UnwindSafe for PhantomData<T>
impl<T, U> Into for PhantomData<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for PhantomData<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for PhantomData<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: ?Sized> CloneFromCell for crate::marker::PhantomData<T>
impl<T: ?Sized> Debug for crate::marker::PhantomData<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T: PointeeSized> Clone for PhantomData<T>
fn clone(self: &Self) -> Self
impl<T: PointeeSized> Copy for PhantomData<T>
impl<T: PointeeSized> Default for PhantomData<T>
fn default() -> Self
impl<T: PointeeSized> Eq for PhantomData<T>
impl<T: PointeeSized> Freeze for PhantomData<T>
impl<T: PointeeSized> Hash for PhantomData<T>
fn hash<H: Hasher>(self: &Self, _: &mut H)
impl<T: PointeeSized> Ord for PhantomData<T>
fn cmp(self: &Self, _other: &PhantomData<T>) -> cmp::Ordering
impl<T: PointeeSized> PartialEq for PhantomData<T>
fn eq(self: &Self, _other: &PhantomData<T>) -> bool
impl<T: PointeeSized> PartialOrd for PhantomData<T>
fn partial_cmp(self: &Self, _other: &PhantomData<T>) -> Option<cmp::Ordering>