MaybeLocation

Struct MaybeLocation 

Source
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>

Source

pub const fn new(_value: T) -> Self
where 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_with to initialize using a closure.
  • new_with_flattened to initialize using a closure that returns an Option<MaybeLocation<T>>.
Source

pub fn new_with(_f: impl FnOnce() -> T) -> Self

Constructs a new MaybeLocation that wraps the result of the given closure.

§See also
  • new to initialize using a value.
  • new_with_flattened to initialize using a closure that returns an Option<MaybeLocation<T>>.
Source

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.

Source

pub fn zip<U>(self, _other: MaybeLocation<U>) -> MaybeLocation<(T, U)>

Converts a pair of MaybeLocation values to an MaybeLocation of a tuple.

Source

pub fn unwrap_or_default(self) -> T
where 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().

Source

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>>

Source

pub fn new_with_flattened(_f: impl FnOnce() -> Option<MaybeLocation<T>>) -> Self

Constructs a new MaybeLocation that wraps the result of the given closure. If the closure returns Some, it unwraps the inner value.

§See also
  • new to initialize using a value.
  • new_with to initialize using a closure.
Source

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_option to convert to an Option<Option<T>>. When used with Option::flatten, this will have a similar effect, but will return None when the track_location feature is disabled.
Source§

impl<T> MaybeLocation<&T>

Source

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>

Source

pub const fn copied(&self) -> MaybeLocation<T>
where T: Copy,

Maps an MaybeLocation<&mut T> to an MaybeLocation<T> by copying the contents.

Source

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>

Source

pub const fn as_ref(&self) -> MaybeLocation<&T>

Converts from &MaybeLocation<T> to MaybeLocation<&T>.

Source

pub const fn as_mut(&mut self) -> MaybeLocation<&mut T>

Converts from &mut MaybeLocation<T> to MaybeLocation<&mut T>.

Source

pub fn as_deref(&self) -> MaybeLocation<&T::Target>
where T: Deref,

Converts from &MaybeLocation<T> to MaybeLocation<&T::Target>.

Source

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

Source

pub const fn caller() -> Self

Returns the source location of the caller of this function. If that function’s caller is annotated then its call location will be returned, and so on up the stack to the first call within a non-tracked function body.

Trait Implementations§

Source§

impl<T: Clone + ?Sized> Clone for MaybeLocation<T>

Source§

fn clone(&self) -> MaybeLocation<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ?Sized> Debug for MaybeLocation<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Display> Display for MaybeLocation<T>

Source§

fn fmt(&self, _f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Hash + ?Sized> Hash for MaybeLocation<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord + ?Sized> Ord for MaybeLocation<T>

Source§

fn cmp(&self, other: &MaybeLocation<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + ?Sized> PartialEq for MaybeLocation<T>

Source§

fn eq(&self, other: &MaybeLocation<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + ?Sized> PartialOrd for MaybeLocation<T>

Source§

fn partial_cmp(&self, other: &MaybeLocation<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy + ?Sized> Copy for MaybeLocation<T>

Source§

impl<T: Eq + ?Sized> Eq for MaybeLocation<T>

Source§

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>
where T: Send + ?Sized,

§

impl<T> Sync for MaybeLocation<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for MaybeLocation<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for MaybeLocation<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DynEq for T
where T: Any + Eq,

Source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

This method tests for self and other values to be equal. Read more
Source§

impl<T> DynHash for T
where T: DynEq + Hash,

Source§

fn dyn_hash(&self, state: &mut dyn Hasher)

Feeds this value into the given Hasher.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ConditionalSend for T
where T: Send,