Skip to main content

bast3st_eval/spec/entities/
entity_id.rs

1use derive_more::{Debug, Deref, Display};
2use serde::{Deserialize, Serialize};
3
4use crate::spec::Entity;
5
6#[derive(Debug, Display, Serialize, Deserialize, Deref)]
7#[serde(from = "u64", into = "u64")]
8#[debug("EntityId<{}>({_0})", std::any::type_name::<T>().split("::").last().unwrap_or_default())]
9#[display("{_0}")]
10pub struct EntityId<T = Entity>(#[deref] pub(super) u64, std::marker::PhantomData<T>);
11
12impl<T> Clone for EntityId<T> {
13    fn clone(&self) -> Self {
14        *self
15    }
16}
17impl<T> Copy for EntityId<T> {}
18impl<T> Eq for EntityId<T> {}
19impl<T> PartialEq for EntityId<T> {
20    fn eq(&self, other: &Self) -> bool {
21        self.0 == other.0
22    }
23}
24impl<T> Ord for EntityId<T> {
25    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
26        self.0.cmp(&other.0)
27    }
28}
29impl<T> PartialOrd for EntityId<T> {
30    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
31        Some(self.cmp(other))
32    }
33}
34
35impl<T> From<u64> for EntityId<T> {
36    fn from(value: u64) -> Self {
37        Self(value, Default::default())
38    }
39}
40
41impl<T> From<EntityId<T>> for u64 {
42    fn from(value: EntityId<T>) -> Self {
43        value.0
44    }
45}
46
47impl<T> EntityId<T> {
48    pub fn new(id: u64) -> Self {
49        Self::from(id)
50    }
51    pub(super) fn _cast_id<U>(self) -> EntityId<U> {
52        EntityId(self.0, Default::default())
53    }
54    pub fn cast_id<U>(self) -> EntityId<U>
55    where
56        U: From<T>,
57    {
58        EntityId(self.0, Default::default())
59    }
60}