Skip to main content

WorldBuilder

Struct WorldBuilder 

Source
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

Source

pub fn new() -> Self

Create an empty builder.

Source

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.

Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of registered resources.

Source

pub fn is_empty(&self) -> bool

Returns true if no resources have been registered.

Source

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

Returns true if a resource of type T has been registered.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for WorldBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.