Struct frame_support::dispatch::marker::PhantomData 1.0.0[−][src]
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 std::marker::PhantomData; struct Slice<'a, T: 'a> { start: *const T, end: *const T, phantom: PhantomData<&'a T>, }
This also in turn requires the annotation 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
:
fn borrow_vec<T>(vec: &Vec<T>) -> Slice<'_, T> { let ptr = vec.as_ptr(); Slice { start: ptr, end: unsafe { ptr.add(vec.len()) }, phantom: 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 std::marker::PhantomData; use std::mem; struct ExternalResource<R> { resource_handle: *mut (), resource_type: PhantomData<R>, } impl<R: ResType> ExternalResource<R> { fn new() -> Self { let size_of_res = mem::size_of::<R>(); Self { resource_handle: foreign_lib::new(size_of_res), resource_type: PhantomData, } } fn do_stuff(&self, param: ParamType) { let foreign_params = convert_params(param); foreign_lib::do_stuff(self.resource_handle, foreign_params); } }
Ownership and the drop check
Adding a field of type PhantomData<T>
indicates that your
type owns data of type T
. This in turn implies that when your
type is dropped, it may drop one or more instances of the type
T
. This has bearing on the Rust compiler’s drop check
analysis.
If your struct does not in fact own the data of type T
, it is
better to use a reference type, like PhantomData<&'a T>
(ideally) or PhantomData<*const T>
(if no lifetime applies), so
as not to indicate ownership.
Trait Implementations
impl<T> Clone for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn clone(&self) -> PhantomData<T>
[src]
pub fn clone_from(&mut self, source: &Self)
[src]
impl<T> Copy for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Debug for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Decode for PhantomData<T>
[src]
impl<T> Default for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn default() -> PhantomData<T>
[src]
impl<'de, T> Deserialize<'de> for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn deserialize<D>(
deserializer: D
) -> Result<PhantomData<T>, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
[src]
deserializer: D
) -> Result<PhantomData<T>, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
impl<'de, T> DeserializeSeed<'de> for PhantomData<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
type Value = T
The type produced by using this seed.
pub fn deserialize<D>(
self,
deserializer: D
) -> Result<T, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
[src]
self,
deserializer: D
) -> Result<T, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
impl<T> Encode for PhantomData<T>
[src]
pub fn encode_to<W>(&self, _dest: &mut W) where
W: Output,
[src]
W: Output,
pub fn size_hint(&self) -> usize
[src]
pub fn encode(&self) -> Vec<u8, Global>ⓘ
[src]
pub fn using_encoded<R, F>(&self, f: F) -> R where
F: FnOnce(&[u8]) -> R,
[src]
F: FnOnce(&[u8]) -> R,
impl<T> EncodeLike<PhantomData<T>> for PhantomData<T>
[src]
impl<T> Eq for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Hash for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn hash<H>(&self, &mut H) where
H: Hasher,
[src]
H: Hasher,
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T> MallocSizeOf for PhantomData<T>
[src]
pub fn size_of(&self, &mut MallocSizeOfOps) -> usize
[src]
pub fn constant_size() -> Option<usize>
[src]
impl<T> Ord for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn cmp(&self, _other: &PhantomData<T>) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T> PartialEq<PhantomData<T>> for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn eq(&self, _other: &PhantomData<T>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
[src]
impl<T> PartialOrd<PhantomData<T>> for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn partial_cmp(&self, _other: &PhantomData<T>) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
[src]
impl<T> Serialize for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
[src]
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
impl<T> StructuralEq for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
impl<T> StructuralPartialEq for PhantomData<T> where
T: ?Sized,
[src]
T: ?Sized,
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for PhantomData<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T: ?Sized> Send for PhantomData<T> where
T: Send,
T: Send,
impl<T: ?Sized> Sync for PhantomData<T> where
T: Sync,
T: Sync,
impl<T: ?Sized> Unpin for PhantomData<T> where
T: Unpin,
T: Unpin,
impl<T: ?Sized> UnwindSafe for PhantomData<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> CallHasher for T where
T: Hash,
[src]
T: Hash,
impl<T> CheckedConversion for T
[src]
pub fn checked_from<T>(t: T) -> Option<Self> where
Self: TryFrom<T>,
[src]
Self: TryFrom<T>,
pub fn checked_into<T>(self) -> Option<T> where
Self: TryInto<T>,
[src]
Self: TryInto<T>,
impl<T> Clear for T where
T: PartialEq<T> + Eq + Default,
[src]
T: PartialEq<T> + Eq + Default,
impl<S> Codec for S where
S: Encode + Decode,
[src]
S: Encode + Decode,
impl<T> DecodeAll for T where
T: Decode,
[src]
T: Decode,
impl<T> DecodeLimit for T where
T: Decode,
[src]
T: Decode,
pub fn decode_all_with_depth_limit(limit: u32, input: &[u8]) -> Result<T, Error>
[src]
pub fn decode_with_depth_limit(limit: u32, input: &[u8]) -> Result<T, Error>
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> DynClone for T where
T: Clone,
[src]
T: Clone,
pub fn __clone_box(&self, Private) -> *mut ()
[src]
impl<'_, '_, T> EncodeLike<&'_ &'_ T> for T where
T: Encode,
[src]
T: Encode,
impl<'_, T> EncodeLike<&'_ T> for T where
T: Encode,
[src]
T: Encode,
impl<'_, T> EncodeLike<&'_ mut T> for T where
T: Encode,
[src]
T: Encode,
impl<T> EncodeLike<Arc<T>> for T where
T: Encode,
[src]
T: Encode,
impl<T> EncodeLike<Box<T, Global>> for T where
T: Encode,
[src]
T: Encode,
impl<'a, T> EncodeLike<Cow<'a, T>> for T where
T: Encode + ToOwned,
[src]
T: Encode + ToOwned,
impl<T> EncodeLike<Rc<T>> for T where
T: Encode,
[src]
T: Encode,
impl<T> From<T> for T
[src]
impl<S> FullCodec for S where
S: Decode + FullEncode,
[src]
S: Decode + FullEncode,
impl<S> FullEncode for S where
S: Encode + EncodeLike<S>,
[src]
S: Encode + EncodeLike<S>,
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IsType<T> for T
[src]
pub fn from_ref(&T) -> &T
[src]
pub fn into_ref(&Self) -> &T
[src]
pub fn from_mut(&mut T) -> &mut T
[src]
pub fn into_mut(&mut Self) -> &mut T
[src]
impl<T, Outer> IsWrappedBy<Outer> for T where
T: From<Outer>,
Outer: AsRef<T> + AsMut<T> + From<T>,
[src]
T: From<Outer>,
Outer: AsRef<T> + AsMut<T> + From<T>,
pub fn from_ref(outer: &Outer) -> &T
[src]
Get a reference to the inner from the outer.
pub fn from_mut(outer: &mut Outer) -> &mut T
[src]
Get a mutable reference to the inner from the outer.
impl<T> KeyedVec for T where
T: Codec,
[src]
T: Codec,
impl<T> MallocSizeOfExt for T where
T: MallocSizeOf,
[src]
T: MallocSizeOf,
pub fn malloc_size_of(&self) -> usize
[src]
impl<T> MaybeDebug for T where
T: Debug,
[src]
T: Debug,
impl<T> MaybeDebug for T where
T: Debug,
[src]
T: Debug,
impl<T> MaybeHash for T where
T: Hash,
[src]
T: Hash,
impl<T> MaybeHash for T where
T: Hash,
[src]
T: Hash,
impl<T> MaybeMallocSizeOf for T where
T: MallocSizeOf,
[src]
T: MallocSizeOf,
impl<T> MaybeRefUnwindSafe for T where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> MaybeSerialize for T where
T: Serialize,
[src]
T: Serialize,
impl<T> MaybeSerializeDeserialize for T where
T: DeserializeOwned + Serialize,
[src]
T: DeserializeOwned + Serialize,
impl<T> Member for T where
T: 'static + Clone + PartialEq<T> + Eq + Send + Sync + Debug,
[src]
T: 'static + Clone + PartialEq<T> + Eq + Send + Sync + Debug,
impl<T> Parameter for T where
T: Codec + EncodeLike<T> + Clone + Eq + Debug,
[src]
T: Codec + EncodeLike<T> + Clone + Eq + Debug,
impl<T> Pointable for T
[src]
pub const ALIGN: usize
[src]
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
[src]
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
[src]
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
[src]
pub unsafe fn drop(ptr: usize)
[src]
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T> SaturatedConversion for T
[src]
pub fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
[src]
Self: UniqueSaturatedFrom<T>,
pub fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
[src]
Self: UniqueSaturatedInto<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<S, T> UncheckedInto<T> for S where
T: UncheckedFrom<S>,
[src]
T: UncheckedFrom<S>,
pub fn unchecked_into(self) -> T
[src]
impl<T, S> UniqueSaturatedInto<T> for S where
T: Bounded,
S: TryInto<T>,
[src]
T: Bounded,
S: TryInto<T>,
pub fn unique_saturated_into(self) -> T
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
[src]
V: MultiLane<T>,