Skip to main content

steel_core/entity/
living_reference.rs

1use crate::entity::{LivingEntity, SharedEntity};
2
3/// A living entity capability tied to the same shared allocation used for attribution.
4#[derive(Clone, Copy)]
5pub struct LivingEntityRef<'a> {
6    entity: &'a SharedEntity,
7    living: &'a dyn LivingEntity,
8}
9
10impl<'a> LivingEntityRef<'a> {
11    /// Borrows the living capability, if the entity provides it.
12    #[must_use]
13    pub fn new(entity: &'a SharedEntity) -> Option<Self> {
14        Some(Self {
15            entity,
16            living: entity.as_living_entity()?,
17        })
18    }
19
20    /// The allocation to retain when constructing a damage source.
21    #[must_use]
22    pub const fn entity(self) -> &'a SharedEntity {
23        self.entity
24    }
25
26    /// The living capability of this allocation.
27    #[must_use]
28    pub const fn living(self) -> &'a dyn LivingEntity {
29        self.living
30    }
31}