Struct scale_info::prelude::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> {
start: *const T,
end: *const T,
phantom: PhantomData<&'a T>,
}
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
:
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
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>>() == 0
align_of::<PhantomData<T>>() == 1
Trait Implementations§
1.0.0 · 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>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more1.0.0 · source§impl<T> Debug for PhantomData<T>where
T: ?Sized,
impl<T> Debug for PhantomData<T>where
T: ?Sized,
source§impl<T> Decode for PhantomData<T>
impl<T> Decode for PhantomData<T>
source§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,
source§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,
1.0.0 · source§impl<T> Default for PhantomData<T>where
T: ?Sized,
impl<T> Default for PhantomData<T>where
T: ?Sized,
source§fn default() -> PhantomData<T>
fn default() -> PhantomData<T>
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.source§impl<T> Encode for PhantomData<T>
impl<T> Encode for PhantomData<T>
source§fn encode_to<W>(&self, _dest: &mut W)
fn encode_to<W>(&self, _dest: &mut W)
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn using_encoded<R, F>(&self, f: F) -> R
fn using_encoded<R, F>(&self, f: F) -> R
source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
1.0.0 · source§impl<T> Hash for PhantomData<T>where
T: ?Sized,
impl<T> Hash for PhantomData<T>where
T: ?Sized,
source§impl<T> MaxEncodedLen for PhantomData<T>
impl<T> MaxEncodedLen for PhantomData<T>
source§fn max_encoded_len() -> usize
fn max_encoded_len() -> usize
1.0.0 · 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,
1.0.0 · source§impl<T> PartialEq for PhantomData<T>where
T: ?Sized,
impl<T> PartialEq 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 ==
.1.0.0 · source§impl<T> PartialOrd for PhantomData<T>where
T: ?Sized,
impl<T> PartialOrd for PhantomData<T>where
T: ?Sized,
source§fn partial_cmp(&self, _other: &PhantomData<T>) -> Option<Ordering>
fn partial_cmp(&self, _other: &PhantomData<T>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§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,
source§impl<T> TypeInfo for PhantomData<T>
impl<T> TypeInfo for PhantomData<T>
impl<T> ConstEncodedLen for PhantomData<T>where
T: ConstEncodedLen,
impl<T> Copy for PhantomData<T>where
T: ?Sized,
impl<T> EncodeLike for PhantomData<T>
impl<T> Eq for PhantomData<T>where
T: ?Sized,
impl<T> Freeze for PhantomData<T>where
T: ?Sized,
impl<T> StructuralPartialEq for PhantomData<T>where
T: ?Sized,
Auto Trait Implementations§
impl<T> RefUnwindSafe for PhantomData<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for PhantomData<T>
impl<T> Sync for PhantomData<T>
impl<T> Unpin for PhantomData<T>
impl<T> UnwindSafe for PhantomData<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.