Struct gecs::OneOf

source ·
pub struct OneOf { /* private fields */ }
Expand description

A special parameter type for ECS query closures to match one of multiple components.

The OneOf<A, B, C, ...> pseudo-type argument to an ECS closure allows a query to match exactly one of the given component types. The component can be accessed as a &T (or &mut T for a &mut OneOf<...>), and is effectively duck-typed within the body of the closure – no traits or where clauses are needed to access elements of the component, so long as the same element or method is available on all potential results of the OneOf binding. If an archetype has more than one of the requested components in a OneOf, this will result in a compilation error. This query will only match archetypes that have one of the requested components.


This is not a real struct and does not exist in any live code, it is a pseudo-type that only has meaning within an ECS query closure when parsed by the operation macro. It is presented here as a standalone struct for documentation purposes only.

Example

use gecs::prelude::*;

pub struct CompA(pub u32);
pub struct CompB(pub u32);
pub struct CompC(pub u32);

ecs_world! {
    ecs_archetype!(ArchFoo, 5, CompA, CompB);
    ecs_archetype!(ArchBar, 5, CompA, CompC);
}

fn main() {
    let mut world = World::default();

    let entity_a = world.archetype_mut::<ArchFoo>().push((CompA(1), CompB(10)));
    let entity_b = world.archetype_mut::<ArchBar>().push((CompA(1), CompC(10)));

    let mut sum_a = 0;
    let mut sum_b = 0;

    // All three of these queries match both ArchFoo and ArchBar:
    ecs_find!(world, entity_a, |v: &mut OneOf<CompB, CompC>| {
        v.0 += 1;
    });

    ecs_find!(world, entity_b, |v: &mut OneOf<CompB, CompC>| {
        v.0 += 1;
    });

    ecs_iter!(world, |u: &CompA, v: &OneOf<CompB, CompC>| {
        sum_a += u.0;
        sum_b += v.0;
    });

    assert_eq!(sum_a, 2);
    assert_eq!(sum_b, 22);
}

Auto Trait Implementations§

§

impl RefUnwindSafe for OneOf

§

impl Send for OneOf

§

impl Sync for OneOf

§

impl Unpin for OneOf

§

impl UnwindSafe for OneOf

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.