pub struct ObservedWorld { /* private fields */ }Expand description
A wrapper around SimpleWorld that fires observers on component mutations.
Observers receive read-only access to the world after the mutation completes, allowing them to see the updated state.
§Example
use cougr_core::runtime::{ComponentEvent, ObservedWorld};
use cougr_core::SimpleWorld;
use soroban_sdk::{symbol_short, Bytes, Env};
fn my_observer(_event: &ComponentEvent, _world: &SimpleWorld, _env: &Env) {}
let env = Env::default();
let world = SimpleWorld::new(&env);
let mut observed = ObservedWorld::new(world);
let entity_id = observed.spawn_entity();
observed.observers_mut().on_add(symbol_short!("pos"), my_observer);
observed.add_component(entity_id, symbol_short!("pos"), Bytes::new(&env), &env);
assert!(observed.world().has_component(entity_id, &symbol_short!("pos")));Implementations§
Source§impl ObservedWorld
impl ObservedWorld
Sourcepub fn new(world: SimpleWorld) -> Self
pub fn new(world: SimpleWorld) -> Self
Wrap a SimpleWorld with an empty observer registry.
Sourcepub fn with_observers(world: SimpleWorld, observers: ObserverRegistry) -> Self
pub fn with_observers(world: SimpleWorld, observers: ObserverRegistry) -> Self
Wrap a SimpleWorld with a pre-configured observer 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 observers(&self) -> &ObserverRegistry
pub fn observers(&self) -> &ObserverRegistry
Access the observer registry.
Sourcepub fn observers_mut(&mut self) -> &mut ObserverRegistry
pub fn observers_mut(&mut self) -> &mut ObserverRegistry
Mutably access the observer 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,
env: &Env,
)
pub fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, env: &Env, )
Add a component, firing on_add observers after insertion.
Sourcepub fn remove_component(
&mut self,
entity_id: EntityId,
component_type: &Symbol,
env: &Env,
) -> bool
pub fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, env: &Env, ) -> bool
Remove a component, firing on_remove observers after 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, env: &Env)
pub fn despawn_entity(&mut self, entity_id: EntityId, env: &Env)
Despawn an entity, firing on_remove observers for each component.
Auto Trait Implementations§
impl Freeze for ObservedWorld
impl !RefUnwindSafe for ObservedWorld
impl !Send for ObservedWorld
impl !Sync for ObservedWorld
impl Unpin for ObservedWorld
impl UnsafeUnpin for ObservedWorld
impl !UnwindSafe for ObservedWorld
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