Skip to main content

World

Struct World 

Source
pub struct World { /* private fields */ }
Expand description

Frozen singleton resource storage.

Analogous to Bevy’s World, but restricted to singleton resources (no entities, no components, no archetypes).

Created by WorldBuilder::build(). Resources are accessed via ResourceId direct pointers for O(1) dispatch-time access — a single pointer deref per fetch, zero framework overhead.

§Safe API

§Unsafe API (framework internals)

The low-level get / get_mut methods are unsafe — used by Param::fetch for zero-overhead dispatch. The caller must ensure no mutable aliasing.

Implementations§

Source§

impl World

Source

pub fn builder() -> WorldBuilder

Convenience constructor — returns a new WorldBuilder.

Source

pub fn registry(&self) -> &Registry

Returns a shared reference to the type registry.

Use this for read-only queries (e.g. id, contains) and construction-time calls like into_handler.

Source

pub fn id<T: Resource>(&self) -> ResourceId

Resolve the ResourceId for a type. Cold path — uses HashMap lookup.

§Panics

Panics if the resource type was not registered.

Source

pub fn try_id<T: Resource>(&self) -> Option<ResourceId>

Try to resolve the ResourceId for a type. Returns None if the type was not registered.

Source

pub fn len(&self) -> usize

Returns the number of registered resources.

Source

pub fn is_empty(&self) -> bool

Returns true if no resources are stored.

Source

pub fn contains<T: Resource>(&self) -> bool

Returns true if a resource of type T is stored.

Source

pub fn resource<T: Resource>(&self) -> &T

Safe shared access to a resource. Cold path — resolves via HashMap.

Takes &self — multiple shared references can coexist. The borrow checker prevents mixing with resource_mut (which takes &mut self).

§Panics

Panics if the resource type was not registered.

Source

pub fn resource_mut<T: Resource>(&mut self) -> &mut T

Safe exclusive access to a resource. Cold path — resolves via HashMap.

§Panics

Panics if the resource type was not registered.

Source

pub fn run_startup<F, Params, M>(&mut self, f: F)
where F: IntoSystem<Params, M>,

Run a system once with full Param resolution.

Intended for one-shot initialization after build(). Accepts both void-returning (fn(params...)) and bool-returning (fn(params...) -> bool) functions via IntoSystem. The return value is always ignored — startup has no DAG to propagate through. Named functions only (same closure limitation as IntoHandler).

Can be called multiple times for phased initialization.

§Examples
fn startup(
    mut driver: ResMut<MioDriver>,
    mut listener: ResMut<Listener>,
) {
    // wire drivers to IO sources...
}

let mut world = wb.build();
world.run_startup(startup);
Source

pub fn shutdown_handle(&self) -> ShutdownHandle

Returns a ShutdownHandle sharing the same flag as the world’s shutdown state.

The handle is owned by the event loop and checked each iteration. Handlers trigger shutdown via the Shutdown Param.

Source

pub fn run(&mut self, f: impl FnMut(&mut World))

Run the event loop until shutdown is triggered.

The closure receives &mut World and defines one iteration of the poll loop — which drivers to poll, in what order, with what timeout. The loop exits when a handler calls Shutdown::trigger or an external signal flips the flag (see ShutdownHandle::enable_signals (requires signals feature)).

§Examples
let mut world = wb.build();
world.run_startup(startup);

world.run(|world| {
    let now = Instant::now();
    let timeout = timer.next_deadline(world)
        .map(|d| d.saturating_duration_since(now));
    mio.poll(world, timeout).expect("mio poll");
    timer.poll(world, now);
});
Source

pub fn current_sequence(&self) -> Sequence

Returns the current event sequence number.

Source

pub fn next_sequence(&mut self) -> Sequence

Advance to the next event sequence number and return it.

Drivers call this before dispatching each event. The returned sequence number identifies the event being processed.

Source

pub fn set_sequence(&mut self, seq: Sequence)

Set the current sequence number directly.

Use for recovery / replay — restores the sequence to a known checkpoint so that subsequent next_sequence calls continue from the right point.

Source

pub unsafe fn get<T: 'static>(&self, id: ResourceId) -> &T

Fetch a shared reference to a resource by direct pointer.

§Safety
  • id must have been returned by WorldBuilder::register for the same builder that produced this container.
  • T must be the same type that was registered at this id.
  • The caller must ensure no mutable reference to this resource exists.
Source

pub unsafe fn get_mut<T: 'static>(&self, id: ResourceId) -> &mut T

Fetch a mutable reference to a resource by direct pointer.

Takes &self — the container structure is frozen, but individual resources have interior mutability via raw pointers. Sound because callers (single-threaded sequential dispatch) uphold no-aliasing.

§Safety
  • id must have been returned by WorldBuilder::register for the same builder that produced this container.
  • T must be the same type that was registered at this id.
  • The caller must ensure no other reference (shared or mutable) to this resource exists.
Source

pub fn register_source(&mut self) -> DataSource

Register a new data source for reactor subscriptions.

Convenience for world.resource_mut::<ReactorNotify>().register_source().

Source

pub fn spawn_reactor<C, Params, F: IntoReactor<C, Params>>( &mut self, ctx_fn: impl FnOnce(Token) -> C, step: F, ) -> ReactorRegistration<'_>

Create and register a reactor from a step function + context factory.

The closure receives the assigned Token so the reactor can store it for wire routing or self-deregistration. Params are resolved from the internal registry — single pointer deref at dispatch time.

This is the primary registration API. It handles the borrow juggling internally: allocates the token, resolves params from the registry, and inserts the reactor — all in one call.

§Example
let src = world.register_source();
world.spawn_reactor(
    |id| QuotingCtx { reactor_id: id, instrument: BTC, layer: 1 },
    quoting_step,
).subscribe(src);
Source

pub fn spawn_built_reactor( &mut self, reactor: impl Reactor + 'static, ) -> ReactorRegistration<'_>

Register a pre-built reactor in one step.

For reactors that don’t need their token in the context, or for PipelineReactor instances.

Source

pub fn dispatch_reactors(&mut self) -> bool

Dispatch all woken reactors and process deferred removals.

Call this post-frame after event handlers have called ReactorNotify::mark. Returns true if any reactor ran.

Source

pub fn reactor_count(&self) -> usize

Number of registered reactors.

Trait Implementations§

Auto Trait Implementations§

§

impl !Freeze for World

§

impl !RefUnwindSafe for World

§

impl !Sync for World

§

impl Unpin for World

§

impl UnsafeUnpin for World

§

impl UnwindSafe for World

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.