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 indexed by dense ResourceId for O(1) dispatch-time access (~3 cycles per fetch).

§Safe API

§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

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 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.

Source

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.

Source

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.

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: 'static>(&self) -> bool

Returns true if a resource of type T is stored.

Source

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.

Source

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.

Source

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);
Source

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().

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::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);
});
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. Resources mutated during dispatch will record this sequence in changed_at.

Source

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

Fetch a shared reference to a resource by pre-validated index.

§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 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
  • 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 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

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.