pub struct WorldBuilder { /* private fields */ }Expand description
Builder for registering resources before freezing into a World container.
Each resource type can only be registered once. Registration assigns a
direct ResourceId pointer.
The registry() method exposes the type-to-pointer mapping
so that drivers can resolve handlers against the builder before build().
§Examples
use nexus_rt::{WorldBuilder, Resource};
#[derive(Resource)]
struct Counter(u64);
#[derive(Resource)]
struct Flag(bool);
let mut builder = WorldBuilder::new();
let id = builder.register(Counter(42));
builder.register(Flag(true));
let world = builder.build();
unsafe {
assert_eq!(world.get::<Counter>(id).0, 42);
}Implementations§
Source§impl WorldBuilder
impl WorldBuilder
Sourcepub fn register<T: Resource>(&mut self, value: T) -> ResourceId
pub fn register<T: Resource>(&mut self, value: T) -> ResourceId
Register a resource and return its ResourceId.
The value is heap-allocated inside a ResourceCell<T> and ownership
is transferred to the container. The pointer is stable for the
lifetime of the resulting World.
§Panics
Panics if a resource of the same type is already registered.
Sourcepub fn register_default<T: Default + Resource>(&mut self) -> ResourceId
pub fn register_default<T: Default + Resource>(&mut self) -> ResourceId
Register a resource using its Default value and return its
ResourceId.
Equivalent to self.register::<T>(T::default()).
Sourcepub fn ensure<T: Resource>(&mut self, value: T) -> ResourceId
pub fn ensure<T: Resource>(&mut self, value: T) -> ResourceId
Ensure a resource is registered, returning its ResourceId.
If the type is already registered, returns the existing ID and
drops value. If not, registers it and returns the new ID.
Use register when duplicate registration is a
bug that should panic. Use ensure when multiple plugins or
drivers may independently need the same resource type.
Sourcepub fn ensure_default<T: Default + Resource>(&mut self) -> ResourceId
pub fn ensure_default<T: Default + Resource>(&mut self) -> ResourceId
Ensure a resource is registered using its Default value,
returning its ResourceId.
If the type is already registered, returns the existing ID.
If not, registers T::default() and returns the new ID.
Sourcepub fn registry(&self) -> &Registry
pub fn registry(&self) -> &Registry
Returns a shared reference to the type registry.
Use this for construction-time calls like
into_handler,
into_callback, and
into_step.
Sourcepub fn contains<T: Resource>(&self) -> bool
pub fn contains<T: Resource>(&self) -> bool
Returns true if a resource of type T has been registered.
Sourcepub fn install_plugin(&mut self, plugin: impl Plugin) -> &mut Self
pub fn install_plugin(&mut self, plugin: impl Plugin) -> &mut Self
Install a plugin. The plugin is consumed and registers its resources into this builder.
Sourcepub fn install_driver<D: Installer>(&mut self, driver: D) -> D::Poller
pub fn install_driver<D: Installer>(&mut self, driver: D) -> D::Poller
Install a driver. The installer is consumed, registers its resources into this builder, and returns a concrete poller for dispatch-time polling.
Sourcepub fn build(self) -> World
pub fn build(self) -> World
Freeze the builder into an immutable World container.
After this call, no more resources can be registered. All
ResourceId values remain valid for the lifetime of the
returned World.
When the reactors feature is enabled, ReactorNotify,
DeferredRemovals, and
SourceRegistry are automatically registered
if not already present.