1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3use std::sync::atomic::{AtomicU64, Ordering};
4
5#[derive(Serialize, Deserialize)]
6pub(crate) struct EntityFactory {
7 next_entity: AtomicU64,
8}
9
10impl Default for EntityFactory {
11 fn default() -> Self {
12 Self {
13 next_entity: AtomicU64::new(0),
14 }
15 }
16}
17
18impl EntityFactory {
19 pub fn new_entity(&self) -> Entity {
20 Entity(self.next_entity.fetch_add(1, Ordering::SeqCst))
21 }
22}
23
24#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
25pub struct Entity(u64);
26
27impl Display for Entity {
28 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29 self.0.fmt(f)
30 }
31}