pub struct HookedWorld { /* private fields */ }Expand description
A wrapper around SimpleWorld that fires lifecycle hooks on component mutations.
Since SimpleWorld is a #[contracttype] struct and cannot hold function pointers,
this wrapper carries a separate HookRegistry alongside the world.
§Example
use cougr_core::runtime::HookedWorld;
use cougr_core::SimpleWorld;
use soroban_sdk::{symbol_short, Bytes, Env, Symbol};
fn on_add(_entity_id: u32, _ctype: &Symbol, _data: &Bytes) {}
let env = Env::default();
let world = SimpleWorld::new(&env);
let mut hooked = HookedWorld::new(world);
let entity_id = hooked.spawn_entity();
hooked.hooks_mut().on_add(symbol_short!("pos"), on_add);
hooked.add_component(entity_id, symbol_short!("pos"), Bytes::new(&env));
assert!(hooked.has_component(entity_id, &symbol_short!("pos")));Implementations§
Source§impl HookedWorld
impl HookedWorld
Sourcepub fn new(world: SimpleWorld) -> Self
pub fn new(world: SimpleWorld) -> Self
Wrap a SimpleWorld with an empty hook registry.
Sourcepub fn with_hooks(world: SimpleWorld, hooks: HookRegistry) -> Self
pub fn with_hooks(world: SimpleWorld, hooks: HookRegistry) -> Self
Wrap a SimpleWorld with a pre-configured hook registry.
Sourcepub fn world(&self) -> &SimpleWorld
pub fn world(&self) -> &SimpleWorld
Access the underlying SimpleWorld.
Sourcepub fn world_mut(&mut self) -> &mut SimpleWorld
pub fn world_mut(&mut self) -> &mut SimpleWorld
Mutably access the underlying SimpleWorld.
Sourcepub fn hooks(&self) -> &HookRegistry
pub fn hooks(&self) -> &HookRegistry
Access the hook registry.
Sourcepub fn hooks_mut(&mut self) -> &mut HookRegistry
pub fn hooks_mut(&mut self) -> &mut HookRegistry
Mutably access the hook registry.
Sourcepub fn into_inner(self) -> SimpleWorld
pub fn into_inner(self) -> SimpleWorld
Consume the wrapper and return the inner SimpleWorld.
Sourcepub fn spawn_entity(&mut self) -> EntityId
pub fn spawn_entity(&mut self) -> EntityId
Spawn a new entity (delegates to SimpleWorld).
Sourcepub fn add_component(
&mut self,
entity_id: EntityId,
component_type: Symbol,
data: Bytes,
)
pub fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, )
Add a component, firing on_add hooks after insertion.
Sourcepub fn remove_component(
&mut self,
entity_id: EntityId,
component_type: &Symbol,
) -> bool
pub fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, ) -> bool
Remove a component, firing on_remove hooks before removal.
Sourcepub fn get_component(
&self,
entity_id: EntityId,
component_type: &Symbol,
) -> Option<Bytes>
pub fn get_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> Option<Bytes>
Get a component (delegates to SimpleWorld).
Sourcepub fn has_component(
&self,
entity_id: EntityId,
component_type: &Symbol,
) -> bool
pub fn has_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> bool
Check if an entity has a component (delegates to SimpleWorld).
Sourcepub fn despawn_entity(&mut self, entity_id: EntityId)
pub fn despawn_entity(&mut self, entity_id: EntityId)
Despawn an entity, firing on_remove hooks for each component.
Auto Trait Implementations§
impl Freeze for HookedWorld
impl !RefUnwindSafe for HookedWorld
impl !Send for HookedWorld
impl !Sync for HookedWorld
impl Unpin for HookedWorld
impl UnsafeUnpin for HookedWorld
impl !UnwindSafe for HookedWorld
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more