1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
//! Types for working with [`Component`]s.
use alloc::borrow::Cow;
use alloc::collections::BTreeSet;
use core::alloc::Layout;
use core::any::TypeId;
use core::ops::Index;
pub use evenio_macros::Component;
use crate::archetype::Archetype;
use crate::assert::UnwrapDebugChecked;
use crate::drop::DropFn;
use crate::event::{Event, EventId, EventPtr};
use crate::map::{Entry, TypeIdMap};
use crate::prelude::World;
use crate::slot_map::{Key, SlotMap};
use crate::sparse::SparseIndex;
use crate::system::{Config, InitError, SystemInfo, SystemParam};
use crate::world::UnsafeWorldCell;
/// Contains metadata for all the components in a world.
///
/// This can be obtained in a system by using the `&Components` system
/// parameter.
///
/// ```
/// # use evenio::prelude::*;
/// # use evenio::component::Components;
/// #
/// # #[derive(Event)] struct E;
/// #
/// # let mut world = World::new();
/// world.add_system(|_: Receiver<E>, components: &Components| {});
/// ```
#[derive(Debug)]
pub struct Components {
infos: SlotMap<ComponentInfo>,
by_type_id: TypeIdMap<ComponentId>,
}
impl Components {
pub(crate) fn new() -> Self {
Self {
infos: SlotMap::new(),
by_type_id: TypeIdMap::default(),
}
}
pub(crate) fn add(&mut self, desc: ComponentDescriptor) -> (ComponentId, bool) {
if let Some(type_id) = desc.type_id {
return match self.by_type_id.entry(type_id) {
Entry::Vacant(v) => {
let Some(k) = self.infos.insert_with(|k| ComponentInfo {
name: desc.name,
id: ComponentId(k),
type_id: desc.type_id,
layout: desc.layout,
drop: desc.drop,
is_immutable: desc.is_immutable,
insert_events: BTreeSet::new(),
remove_events: BTreeSet::new(),
}) else {
panic!("too many components")
};
(*v.insert(ComponentId(k)), true)
}
Entry::Occupied(o) => (*o.get(), false),
};
}
let Some(k) = self.infos.insert_with(|k| ComponentInfo {
name: desc.name,
id: ComponentId(k),
type_id: desc.type_id,
layout: desc.layout,
drop: desc.drop,
is_immutable: desc.is_immutable,
insert_events: BTreeSet::new(),
remove_events: BTreeSet::new(),
}) else {
panic!("too many components")
};
(ComponentId(k), true)
}
pub(crate) fn remove(&mut self, component_id: ComponentId) -> Option<ComponentInfo> {
let info = self.infos.remove(component_id.0)?;
if let Some(type_id) = info.type_id {
self.by_type_id.remove(&type_id);
}
Some(info)
}
/// Gets the [`ComponentInfo`] of the given component. Returns `None` if the
/// ID is invalid.
pub fn get(&self, id: ComponentId) -> Option<&ComponentInfo> {
self.infos.get(id.0)
}
/// Gets the [`ComponentInfo`] for a component using its [`ComponentIdx`].
/// Returns `None` if the index is invalid.
pub fn get_by_index(&self, idx: ComponentIdx) -> Option<&ComponentInfo> {
self.infos.get_by_index(idx.0).map(|(_, v)| v)
}
pub(crate) fn get_by_index_mut(&mut self, idx: ComponentIdx) -> Option<&mut ComponentInfo> {
self.infos.get_by_index_mut(idx.0).map(|(_, v)| v)
}
/// Gets the [`ComponentInfo`] for a component using its [`TypeId`]. Returns
/// `None` if the `TypeId` does not map to a component.
pub fn get_by_type_id(&self, type_id: TypeId) -> Option<&ComponentInfo> {
let id = *self.by_type_id.get(&type_id)?;
Some(unsafe { self.get(id).unwrap_debug_checked() })
}
/// Does the given component exist in the world?
pub fn contains(&self, id: ComponentId) -> bool {
self.get(id).is_some()
}
/// Returns an iterator over all component infos.
pub fn iter(&self) -> impl Iterator<Item = &ComponentInfo> {
self.infos.iter().map(|(_, v)| v)
}
}
impl Index<ComponentId> for Components {
type Output = ComponentInfo;
fn index(&self, index: ComponentId) -> &Self::Output {
if let Some(info) = self.get(index) {
info
} else {
panic!("no such component with ID of {index:?} exists")
}
}
}
impl Index<ComponentIdx> for Components {
type Output = ComponentInfo;
fn index(&self, index: ComponentIdx) -> &Self::Output {
if let Some(info) = self.get_by_index(index) {
info
} else {
panic!("no such component with index of {index:?} exists")
}
}
}
impl Index<TypeId> for Components {
type Output = ComponentInfo;
fn index(&self, index: TypeId) -> &Self::Output {
if let Some(info) = self.get_by_type_id(index) {
info
} else {
panic!("no such component with type ID of {index:?} exists")
}
}
}
unsafe impl SystemParam for &'_ Components {
type State = ();
type Item<'a> = &'a Components;
fn init(_world: &mut World, _config: &mut Config) -> Result<Self::State, InitError> {
Ok(())
}
unsafe fn get<'a>(
_state: &'a mut Self::State,
_info: &'a SystemInfo,
_event_ptr: EventPtr<'a>,
world: UnsafeWorldCell<'a>,
) -> Self::Item<'a> {
world.components()
}
fn refresh_archetype(_state: &mut Self::State, _arch: &Archetype) {}
fn remove_archetype(_state: &mut Self::State, _arch: &Archetype) {}
}
/// Metadata for a component.
#[derive(Debug)]
pub struct ComponentInfo {
name: Cow<'static, str>,
id: ComponentId,
type_id: Option<TypeId>,
layout: Layout,
drop: DropFn,
is_immutable: bool,
pub(crate) insert_events: BTreeSet<EventId>,
pub(crate) remove_events: BTreeSet<EventId>,
}
impl ComponentInfo {
/// Gets the name of the component.
///
/// This name is intended for debugging purposes and should not be relied
/// upon for correctness.
pub fn name(&self) -> &str {
&self.name
}
/// Gets the ID of the component.
pub fn id(&self) -> ComponentId {
self.id
}
/// Gets the [`TypeId`] of the component, or `None` if it was not assigned a
/// type ID.
pub fn type_id(&self) -> Option<TypeId> {
self.type_id
}
/// Gets the [`Layout`] of the component.
pub fn layout(&self) -> Layout {
self.layout
}
/// Gets the [`DropFn`] of the component.
pub fn drop(&self) -> DropFn {
self.drop
}
/// Gets the [immutability] of the component.
///
/// [immutability]: Component::IS_IMMUTABLE
pub fn is_immutable(&self) -> bool {
self.is_immutable
}
/// Gets the set of [`Insert`] events for this component.
///
/// [`Insert`]: crate::event::Insert
pub fn insert_events(&self) -> &BTreeSet<EventId> {
&self.insert_events
}
/// Gets the set of [`Remove`] components for this component.
///
/// [`Remove`]: crate::event::Remove
pub fn remove_events(&self) -> &BTreeSet<EventId> {
&self.remove_events
}
}
/// Types which store data on [entities].
///
/// A `Component` is a piece of data which can be attached to an entity. An
/// entity can have any combination of components, but cannot have more than one
/// component of the same type.
///
/// To add a component to an entity, use the [`Insert`] event. To access
/// components from systems, use the [`Fetcher`] system parameter.
///
/// [entities]: crate::entity
/// [`Insert`]: crate::event::Insert
/// [`Fetcher`]: crate::fetch::Fetcher
///
/// # Deriving
///
/// The `Component` trait can be implemented automatically by using the
/// associated derive macro. However, the type must still satisfy the `Send +
/// Sync + 'static` bound to do so.
///
/// ```
/// use evenio::prelude::*;
///
/// // Component with some data.
/// #[derive(Component)]
/// struct Username(String);
///
/// // Component without data, known as a "marker" or "tag" component.
/// struct Invisible;
///
/// // Derive it on structs with named fields.
/// #[derive(Component)]
/// struct Position {
/// x: f32,
/// y: f32,
/// z: f32,
/// }
///
/// // ...and on enums.
/// #[derive(Component)]
/// enum FriendStatus {
/// Friendly,
/// Neutral,
/// Unfriendly,
/// }
///
/// // Components can be immutable, which disallows mutable references
/// // to the component once it's attached to an entity.
/// #[derive(Component)]
/// #[component(immutable)] // Override the default mutability.
/// struct FooCounter(i32);
/// ```
pub trait Component: Send + Sync + 'static {
/// Whether or not this component is immutable.
///
/// Immutable components disallow mutable references, which can be used to
/// ensure components are used in particular ways.
const IS_IMMUTABLE: bool = false;
}
/// Data needed to create a new component.
#[derive(Clone, Debug)]
pub struct ComponentDescriptor {
/// The name of this component.
///
/// This name is intended for debugging purposes and should not be relied
/// upon for correctness.
pub name: Cow<'static, str>,
/// The [`TypeId`] of this component, if any.
pub type_id: Option<TypeId>,
/// The [`Layout`] of the component.
pub layout: Layout,
/// The [`DropFn`] of the component. This is passed a pointer to the
/// component in order to drop it.
pub drop: DropFn,
/// If this component is [immutable](Component::IS_IMMUTABLE).
pub is_immutable: bool,
}
/// Lightweight identifier for a component type.
///
/// component identifiers are implemented using an [index] and a generation
/// count. The generation count ensures that IDs from removed components are
/// not reused by new components.
///
/// A component identifier is only meaningful in the [`World`] it was created
/// from. Attempting to use a component ID in a different world will have
/// unexpected results.
///
/// [index]: ComponentIdx
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct ComponentId(Key);
impl ComponentId {
/// The component ID which never identifies a live component. This is the
/// default value for `ComponentId`.
pub const NULL: Self = Self(Key::NULL);
/// Creates a new component ID from an index and generation count. Returns
/// `None` if a valid ID is not formed.
pub const fn new(index: u32, generation: u32) -> Option<Self> {
match Key::new(index, generation) {
Some(k) => Some(Self(k)),
None => None,
}
}
/// Returns the index of this ID.
pub const fn index(self) -> ComponentIdx {
ComponentIdx(self.0.index())
}
/// Returns the generation count of this ID.
pub const fn generation(self) -> u32 {
self.0.generation().get()
}
}
/// A [`ComponentId`] with the generation count stripped out.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct ComponentIdx(pub u32);
unsafe impl SparseIndex for ComponentIdx {
const MAX: Self = Self(u32::MAX);
fn index(self) -> usize {
self.0.index()
}
fn from_index(idx: usize) -> Self {
Self(u32::from_index(idx))
}
}
/// An event sent immediately after a new component is added to the world.
/// Contains the ID of the added component.
#[derive(Event, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct AddComponent(pub ComponentId);
/// An event sent immediately before a component is removed from the world.
/// Contains the ID of the component to be removed.
#[derive(Event, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct RemoveComponent(pub ComponentId);
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[derive(Event)]
struct E;
#[test]
fn remove_component() {
#[derive(Component)]
struct A(String);
#[derive(Component, PartialEq, Debug)]
struct B(Vec<String>);
let mut world = World::new();
let c1 = world.add_component::<A>();
let e1 = world.spawn();
world.insert(e1, A("hello".into()));
let s1 = world.add_system(|_: Receiver<E>, Single(A(a)): Single<&mut A>| {
a.push_str("hello");
});
world.send(E);
assert!(world.remove_component(c1).is_some());
assert!(!world.systems().contains(s1));
assert!(!world.entities().contains(e1));
assert_eq!(
world.archetypes().len(),
1,
"only the empty archetype should be present"
);
let c2 = world.add_component::<B>();
let e2 = world.spawn();
assert!(world.entities().contains(e2));
world.insert(e2, B(vec![]));
let s2 = world.add_system(|_: Receiver<E>, Single(B(b)): Single<&mut B>| {
b.push("hello".into());
});
world.send(E);
assert_eq!(world.get_component::<B>(e2), Some(&B(vec!["hello".into()])));
assert!(world.remove_component(c2).is_some());
assert!(!world.systems().contains(s2));
assert!(!world.entities().contains(e2));
assert_eq!(world.archetypes().len(), 1);
}
}