bevy_ecs_markers 2.0.0

🏷️ Markers for Bevy ECS Entities
Documentation
use std::{
    marker::PhantomData,
    ops::{Deref, Index},
};

use bevy_ecs::system::{Res, SystemParam};

use crate::{entity_marker::EntityMarker, MarkerData, MarkerSlot};

/// A System Param that can read the data from a given [`EntityMarker`]
#[derive(SystemParam)]
pub struct Marker<'s, 'w, M: EntityMarker + 'static> {
    marker_data: Res<'w, MarkerData<M>>,
    #[system_param(ignore)]
    phantom: PhantomData<&'s ()>,
}

impl<M: EntityMarker> Deref for Marker<'_, '_, M>
where
    M::Slots<MarkerSlot>: Deref<Target = MarkerSlot>,
{
    type Target = MarkerSlot;

    fn deref(&self) -> &Self::Target {
        self.marker_data.slots.deref()
    }
}

impl<M: EntityMarker> Index<M> for Marker<'_, '_, M>
where
    M::Slots<MarkerSlot>: Index<usize, Output = MarkerSlot>,
{
    type Output = MarkerSlot;

    fn index(&self, index: M) -> &Self::Output {
        self.marker_data.slots.index(index.slot())
    }
}