Struct frame_support::dispatch::marker::PhantomData
1.0.0 · source · pub struct PhantomData<T>
where
T: ?Sized;
Expand description
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.
Layout
For all T
, the following are guaranteed:
size_of::<PhantomData<T>>() == 0
align_of::<PhantomData<T>>() == 1
Trait Implementations§
§impl<T> CanonicalDeserialize for PhantomData<T>where
T: Send + Sync,
impl<T> CanonicalDeserialize for PhantomData<T>where T: Send + Sync,
§fn deserialize_with_mode<R>(
_reader: R,
_compress: Compress,
_validate: Validate
) -> Result<PhantomData<T>, SerializationError>where
R: Read,
fn deserialize_with_mode<R>( _reader: R, _compress: Compress, _validate: Validate ) -> Result<PhantomData<T>, SerializationError>where R: Read,
fn deserialize_compressed<R>(reader: R) -> Result<Self, SerializationError>where R: Read,
fn deserialize_compressed_unchecked<R>( reader: R ) -> Result<Self, SerializationError>where R: Read,
fn deserialize_uncompressed<R>(reader: R) -> Result<Self, SerializationError>where R: Read,
fn deserialize_uncompressed_unchecked<R>( reader: R ) -> Result<Self, SerializationError>where R: Read,
§impl<T> CanonicalSerialize for PhantomData<T>
impl<T> CanonicalSerialize for PhantomData<T>
§fn serialize_with_mode<W>(
&self,
_writer: W,
_compress: Compress
) -> Result<(), SerializationError>where
W: Write,
fn serialize_with_mode<W>( &self, _writer: W, _compress: Compress ) -> Result<(), SerializationError>where W: Write,
fn serialized_size(&self, _compress: Compress) -> usize
fn serialize_compressed<W>(&self, writer: W) -> Result<(), SerializationError>where W: Write,
fn compressed_size(&self) -> usize
fn serialize_uncompressed<W>(&self, writer: W) -> Result<(), SerializationError>where W: Write,
fn uncompressed_size(&self) -> usize
source§impl<T> Clone for PhantomData<T>where
T: ?Sized,
impl<T> Clone for PhantomData<T>where T: ?Sized,
source§fn clone(&self) -> PhantomData<T>
fn clone(&self) -> PhantomData<T>
source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T> Debug for PhantomData<T>where
T: ?Sized,
impl<T> Debug for PhantomData<T>where T: ?Sized,
§impl<'a, T> Decode<'a> for PhantomData<T>where
T: ?Sized,
impl<'a, T> Decode<'a> for PhantomData<T>where T: ?Sized,
Dummy implementation for PhantomData
which allows deriving
implementations on structs with phantom fields.
§impl<T> Decode for PhantomData<T>
impl<T> Decode for PhantomData<T>
§fn decode<I>(_input: &mut I) -> Result<PhantomData<T>, Error>where
I: Input,
fn decode<I>(_input: &mut I) -> Result<PhantomData<T>, Error>where I: Input,
§fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>
) -> Result<DecodeFinished, Error>where
I: Input,
fn decode_into<I>( input: &mut I, dst: &mut MaybeUninit<Self> ) -> Result<DecodeFinished, Error>where I: Input,
§fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
fn skip<I>(input: &mut I) -> Result<(), Error>where I: Input,
§fn encoded_fixed_size() -> Option<usize>
fn encoded_fixed_size() -> Option<usize>
§impl<T> DerOrd for PhantomData<T>
impl<T> DerOrd for PhantomData<T>
Provide a no-op implementation for PhantomData
source§impl<'de, T> Deserialize<'de> for PhantomData<T>where
T: ?Sized,
impl<'de, T> Deserialize<'de> for PhantomData<T>where T: ?Sized,
source§fn deserialize<D>(
deserializer: D
) -> Result<PhantomData<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<PhantomData<T>, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
source§impl<'de, T> DeserializeSeed<'de> for PhantomData<T>where
T: Deserialize<'de>,
impl<'de, T> DeserializeSeed<'de> for PhantomData<T>where T: Deserialize<'de>,
source§fn deserialize<D>(
self,
deserializer: D
) -> Result<T, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( self, deserializer: D ) -> Result<T, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
Deserialize::deserialize
method, except
with some initial piece of data (the seed) passed in.§impl<T> Encode for PhantomData<T>
impl<T> Encode for PhantomData<T>
§fn encode_to<W>(&self, _dest: &mut W)where
W: Output + ?Sized,
fn encode_to<W>(&self, _dest: &mut W)where W: Output + ?Sized,
§fn using_encoded<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&[u8]) -> R,
fn using_encoded<R, F>(&self, f: F) -> Rwhere F: FnOnce(&[u8]) -> R,
§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
§impl<T> Encode for PhantomData<T>where
T: ?Sized,
impl<T> Encode for PhantomData<T>where T: ?Sized,
Dummy implementation for PhantomData
which allows deriving
implementations on structs with phantom fields.
§fn encoded_len(&self) -> Result<Length, Error>
fn encoded_len(&self) -> Result<Length, Error>
§fn encode(&self, _writer: &mut impl Writer) -> Result<(), Error>
fn encode(&self, _writer: &mut impl Writer) -> Result<(), Error>
Writer
].§fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>
source§impl<T> Hash for PhantomData<T>where
T: ?Sized,
impl<T> Hash for PhantomData<T>where T: ?Sized,
§impl<T> MaxEncodedLen for PhantomData<T>
impl<T> MaxEncodedLen for PhantomData<T>
§fn max_encoded_len() -> usize
fn max_encoded_len() -> usize
source§impl<T> Ord for PhantomData<T>where
T: ?Sized,
impl<T> Ord for PhantomData<T>where T: ?Sized,
source§fn cmp(&self, _other: &PhantomData<T>) -> Ordering
fn cmp(&self, _other: &PhantomData<T>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T> PalletError for PhantomData<T>
impl<T> PalletError for PhantomData<T>
source§const MAX_ENCODED_SIZE: usize = 0usize
const MAX_ENCODED_SIZE: usize = 0usize
source§impl<T> PartialEq<PhantomData<T>> for PhantomData<T>where
T: ?Sized,
impl<T> PartialEq<PhantomData<T>> for PhantomData<T>where T: ?Sized,
source§fn eq(&self, _other: &PhantomData<T>) -> bool
fn eq(&self, _other: &PhantomData<T>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T> PartialOrd<PhantomData<T>> for PhantomData<T>where
T: ?Sized,
impl<T> PartialOrd<PhantomData<T>> for PhantomData<T>where T: ?Sized,
source§impl<T> Serialize for PhantomData<T>where
T: ?Sized,
impl<T> Serialize for PhantomData<T>where T: ?Sized,
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
§impl<T> TypeInfo for PhantomData<T>
impl<T> TypeInfo for PhantomData<T>
§impl<T> Valid for PhantomData<T>where
T: Sync,
impl<T> Valid for PhantomData<T>where T: Sync,
§impl<T> ValueOrd for PhantomData<T>
impl<T> ValueOrd for PhantomData<T>
Provide a no-op implementation for PhantomData
§impl<Z> Zeroize for PhantomData<Z>
impl<Z> Zeroize for PhantomData<Z>
PhantomData
is always zero sized so provide a [Zeroize
] implementation.
impl<T> ConstEncodedLen for PhantomData<T>where T: ConstEncodedLen,
impl<T> Copy for PhantomData<T>where T: ?Sized,
impl<T> EncodeLike<PhantomData<T>> for PhantomData<T>
impl<T> Eq for PhantomData<T>where T: ?Sized,
impl<T> StructuralEq for PhantomData<T>where T: ?Sized,
impl<T> StructuralPartialEq for PhantomData<T>where T: ?Sized,
impl<Z> ZeroizeOnDrop for PhantomData<Z>
[PhantomData
is always zero sized so provide a ZeroizeOnDrop implementation.
Auto Trait Implementations§
impl<T: ?Sized> RefUnwindSafe for PhantomData<T>where T: RefUnwindSafe,
impl<T: ?Sized> Send for PhantomData<T>where T: Send,
impl<T: ?Sized> Sync for PhantomData<T>where T: Sync,
impl<T: ?Sized> Unpin for PhantomData<T>where T: Unpin,
impl<T: ?Sized> UnwindSafe for PhantomData<T>where T: UnwindSafe,
Blanket Implementations§
§impl<T> CanonicalSerializeHashExt for Twhere
T: CanonicalSerialize,
impl<T> CanonicalSerializeHashExt for Twhere T: CanonicalSerialize,
source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
§impl<T> DecodeAll for Twhere
T: Decode,
impl<T> DecodeAll for Twhere T: Decode,
§fn decode_all(input: &mut &[u8]) -> Result<T, Error>
fn decode_all(input: &mut &[u8]) -> Result<T, Error>
Self
and consume all of the given input data. Read more§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere T: Decode,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T, Outer> IsWrappedBy<Outer> for Twhere
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T, Outer> IsWrappedBy<Outer> for Twhere Outer: AsRef<T> + AsMut<T> + From<T>, T: From<Outer>,
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere Self: UniqueSaturatedFrom<T>,
source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere Self: UniqueSaturatedInto<T>,
T
. Read moresource§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere T: UncheckedFrom<S>,
source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.source§impl<T, S> UniqueSaturatedInto<T> for Swhere
T: Bounded,
S: TryInto<T>,
impl<T, S> UniqueSaturatedInto<T> for Swhere T: Bounded, S: TryInto<T>,
source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.