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 indexed by dense
ResourceId for O(1) dispatch-time access (~3 cycles per fetch).
§Safe API
resource/resource_mut— cold-path access via HashMap lookup.
§Unsafe API (framework internals)
The low-level get / get_mut methods are unsafe — used by
Param::fetch for ~3-cycle dispatch.
The caller must ensure no mutable aliasing.
Implementations§
Source§impl World
impl World
Sourcepub fn builder() -> WorldBuilder
pub fn builder() -> WorldBuilder
Convenience constructor — returns a new WorldBuilder.
Sourcepub fn registry(&self) -> &Registry
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.
Sourcepub fn registry_mut(&mut self) -> &mut Registry
pub fn registry_mut(&mut self) -> &mut Registry
Returns a mutable reference to the type registry.
Rarely needed — registry() suffices for
construction-time calls. Exists for direct mutation of the
registry if needed.
Sourcepub fn id<T: 'static>(&self) -> ResourceId
pub fn id<T: 'static>(&self) -> ResourceId
Resolve the ResourceId for a type. Cold path — uses HashMap lookup.
§Panics
Panics if the resource type was not registered.
Sourcepub fn try_id<T: 'static>(&self) -> Option<ResourceId>
pub fn try_id<T: 'static>(&self) -> Option<ResourceId>
Try to resolve the ResourceId for a type. Returns None if the
type was not registered.
Sourcepub fn resource<T: 'static>(&self) -> &T
pub fn resource<T: 'static>(&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.
Sourcepub fn resource_mut<T: 'static>(&mut self) -> &mut T
pub fn resource_mut<T: 'static>(&mut self) -> &mut T
Safe exclusive access to a resource. Cold path — resolves via HashMap.
§Panics
Panics if the resource type was not registered.
Sourcepub fn run_startup<F, Params>(&mut self, f: F)where
F: IntoHandler<(), Params>,
pub fn run_startup<F, Params>(&mut self, f: F)where
F: IntoHandler<(), Params>,
Run a handler once with full Param resolution.
Intended for one-shot initialization after build().
The handler receives () as the event — the event parameter is
discarded. 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>,
_event: (),
) {
// wire drivers to IO sources...
}
let mut world = wb.build();
world.run_startup(startup);Sourcepub fn shutdown_handle(&self) -> ShutdownHandle
pub fn shutdown_handle(&self) -> ShutdownHandle
Returns a ShutdownHandle
sharing the same flag as the Shutdown
resource.
The handle is owned by the event loop and checked each iteration.
Handlers trigger shutdown via Res<Shutdown>::shutdown().
Sourcepub fn run(&mut self, f: impl FnMut(&mut World))
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::shutdown or
an external signal flips the flag (see
ShutdownHandle::enable_signals).
The shutdown flag is resolved once before entering the loop — no resource lookup per iteration.
§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);
});Sourcepub fn current_sequence(&self) -> Sequence
pub fn current_sequence(&self) -> Sequence
Returns the current event sequence number.
Sourcepub fn next_sequence(&mut self) -> Sequence
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. Resources
mutated during dispatch will record this sequence in changed_at.
Sourcepub unsafe fn get<T: 'static>(&self, id: ResourceId) -> &T
pub unsafe fn get<T: 'static>(&self, id: ResourceId) -> &T
Fetch a shared reference to a resource by pre-validated index.
§Safety
idmust have been returned byWorldBuilder::registerfor the same builder that produced this container.Tmust be the same type that was registered at thisid.- The caller must ensure no mutable reference to this resource exists.
Sourcepub unsafe fn get_mut<T: 'static>(&self, id: ResourceId) -> &mut T
pub unsafe fn get_mut<T: 'static>(&self, id: ResourceId) -> &mut T
Fetch a mutable reference to a resource by pre-validated index.
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
idmust have been returned byWorldBuilder::registerfor the same builder that produced this container.Tmust be the same type that was registered at thisid.- The caller must ensure no other reference (shared or mutable) to this resource exists.
Sourcepub unsafe fn get_ptr(&self, id: ResourceId) -> *mut u8
pub unsafe fn get_ptr(&self, id: ResourceId) -> *mut u8
Fetch a raw pointer to a resource by pre-validated index.
Intended for macro-generated dispatch code that needs direct pointer access.
§Safety
idmust have been returned byWorldBuilder::registerfor the same builder that produced this container.