pub struct MaybeLocation<T: ?Sized = &'static Location<'static>> { /* private fields */ }Expand description
A value that contains a T if the track_location feature is enabled,
and is a ZST if it is not.
The overall API is similar to Option, but whether the value is Some or None is set at compile
time and is the same for all values.
If the track_location feature is disabled, then all functions on this type that return
an MaybeLocation will have an empty body and should be removed by the optimizer.
This allows code to be written that will be checked by the compiler even when the feature is disabled, but that will be entirely removed during compilation.
Implementations§
Source§impl<T> MaybeLocation<T>
impl<T> MaybeLocation<T>
Sourcepub const fn new(_value: T) -> Selfwhere
T: Copy,
pub const fn new(_value: T) -> Selfwhere
T: Copy,
Constructs a new MaybeLocation that wraps the given value.
This may only accept Copy types,
since it needs to drop the value if the track_location feature is disabled,
and non-Copy types cannot be dropped in const context.
Use new_with if you need to construct a non-Copy value.
§See also
new_withto initialize using a closure.new_with_flattenedto initialize using a closure that returns anOption<MaybeLocation<T>>.
Sourcepub fn new_with(_f: impl FnOnce() -> T) -> Self
pub fn new_with(_f: impl FnOnce() -> T) -> Self
Constructs a new MaybeLocation that wraps the result of the given closure.
§See also
newto initialize using a value.new_with_flattenedto initialize using a closure that returns anOption<MaybeLocation<T>>.
Sourcepub fn map<U>(self, _f: impl FnOnce(T) -> U) -> MaybeLocation<U>
pub fn map<U>(self, _f: impl FnOnce(T) -> U) -> MaybeLocation<U>
Maps an MaybeLocation<T> to MaybeLocation<U> by applying a function to a contained value.
Sourcepub fn zip<U>(self, _other: MaybeLocation<U>) -> MaybeLocation<(T, U)>
pub fn zip<U>(self, _other: MaybeLocation<U>) -> MaybeLocation<(T, U)>
Converts a pair of MaybeLocation values to an MaybeLocation of a tuple.
Sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
Returns the contained value or a default.
If the track_location feature is enabled, this always returns the contained value.
If it is disabled, this always returns T::Default().
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Converts an MaybeLocation to an Option to allow run-time branching.
If the track_location feature is enabled, this always returns Some.
If it is disabled, this always returns None.
Source§impl<T> MaybeLocation<Option<T>>
impl<T> MaybeLocation<Option<T>>
Sourcepub fn new_with_flattened(_f: impl FnOnce() -> Option<MaybeLocation<T>>) -> Self
pub fn new_with_flattened(_f: impl FnOnce() -> Option<MaybeLocation<T>>) -> Self
Sourcepub fn transpose(self) -> Option<MaybeLocation<T>>
pub fn transpose(self) -> Option<MaybeLocation<T>>
Transposes a MaybeLocation of an Option into an Option of a MaybeLocation.
This can be useful if you want to use the ? operator to exit early
if the track_location feature is enabled but the value is not found.
If the track_location feature is enabled,
this returns Some if the inner value is Some
and None if the inner value is None.
If it is disabled, this always returns Some.
§Example
let mut world = World::new();
let entity = world.spawn(()).id();
let location: MaybeLocation<Option<&'static Location<'static>>> =
world.entities().entity_get_spawned_or_despawned_by(entity);
let location: MaybeLocation<&'static Location<'static>> = location.transpose()?;§See also
into_optionto convert to anOption<Option<T>>. When used withOption::flatten, this will have a similar effect, but will returnNonewhen thetrack_locationfeature is disabled.
Source§impl<T> MaybeLocation<&T>
impl<T> MaybeLocation<&T>
Sourcepub const fn copied(&self) -> MaybeLocation<T>where
T: Copy,
pub const fn copied(&self) -> MaybeLocation<T>where
T: Copy,
Maps an MaybeLocation<&T> to an MaybeLocation<T> by copying the contents.
Source§impl<T> MaybeLocation<&mut T>
impl<T> MaybeLocation<&mut T>
Sourcepub const fn copied(&self) -> MaybeLocation<T>where
T: Copy,
pub const fn copied(&self) -> MaybeLocation<T>where
T: Copy,
Maps an MaybeLocation<&mut T> to an MaybeLocation<T> by copying the contents.
Sourcepub fn assign(&mut self, _value: MaybeLocation<T>)
pub fn assign(&mut self, _value: MaybeLocation<T>)
Assigns the contents of an MaybeLocation<T> to an MaybeLocation<&mut T>.
Source§impl<T: ?Sized> MaybeLocation<T>
impl<T: ?Sized> MaybeLocation<T>
Sourcepub const fn as_ref(&self) -> MaybeLocation<&T>
pub const fn as_ref(&self) -> MaybeLocation<&T>
Converts from &MaybeLocation<T> to MaybeLocation<&T>.
Sourcepub const fn as_mut(&mut self) -> MaybeLocation<&mut T>
pub const fn as_mut(&mut self) -> MaybeLocation<&mut T>
Converts from &mut MaybeLocation<T> to MaybeLocation<&mut T>.
Sourcepub fn as_deref(&self) -> MaybeLocation<&T::Target>where
T: Deref,
pub fn as_deref(&self) -> MaybeLocation<&T::Target>where
T: Deref,
Converts from &MaybeLocation<T> to MaybeLocation<&T::Target>.
Sourcepub fn as_deref_mut(&mut self) -> MaybeLocation<&mut T::Target>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> MaybeLocation<&mut T::Target>where
T: DerefMut,
Converts from &mut MaybeLocation<T> to MaybeLocation<&mut T::Target>.
Source§impl MaybeLocation
impl MaybeLocation
Trait Implementations§
Source§impl<T: Clone + ?Sized> Clone for MaybeLocation<T>
impl<T: Clone + ?Sized> Clone for MaybeLocation<T>
Source§fn clone(&self) -> MaybeLocation<T>
fn clone(&self) -> MaybeLocation<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Display> Display for MaybeLocation<T>
impl<T: Display> Display for MaybeLocation<T>
Source§impl<T: Ord + ?Sized> Ord for MaybeLocation<T>
impl<T: Ord + ?Sized> Ord for MaybeLocation<T>
Source§fn cmp(&self, other: &MaybeLocation<T>) -> Ordering
fn cmp(&self, other: &MaybeLocation<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: PartialOrd + ?Sized> PartialOrd for MaybeLocation<T>
impl<T: PartialOrd + ?Sized> PartialOrd for MaybeLocation<T>
impl<T: Copy + ?Sized> Copy for MaybeLocation<T>
impl<T: Eq + ?Sized> Eq for MaybeLocation<T>
impl<T: ?Sized> StructuralPartialEq for MaybeLocation<T>
Auto Trait Implementations§
impl<T> Freeze for MaybeLocation<T>where
T: ?Sized,
impl<T> RefUnwindSafe for MaybeLocation<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for MaybeLocation<T>
impl<T> Sync for MaybeLocation<T>
impl<T> Unpin for MaybeLocation<T>
impl<T> UnwindSafe for MaybeLocation<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: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.