Skip to main content

WorldCtx

Struct WorldCtx 

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

Copy handle for synchronous World access from async tasks.

§Safety Contract

  • Single-threaded only. No concurrent with_world calls.
  • World outlives tasks. The World must not be dropped while any task holds a WorldCtx.

Both invariants are enforced structurally by the single-threaded executor: only one task polls at a time, and the user owns the World alongside the executor in the same scope.

§Examples

use nexus_async_rt::{Executor, WorldCtx};
use nexus_rt::{WorldBuilder, Res, ResMut, IntoHandler, Handler};

let mut world = builder.build();
let ctx = WorldCtx::new(&mut world);

// Pre-resolve at setup — single HashMap lookup per type
let mut on_quote = (|mut books: ResMut<Books>, q: Quote| {
    books.update(q);
}).into_handler(world.registry());

let mut executor = Executor::new(64);
executor.spawn_boxed(async move {
    let data = read_socket().await;
    // Single deref per resource at dispatch time
    ctx.with_world(|world| on_quote.run(world, data));
});

executor.drain();

Implementations§

Source§

impl WorldCtx

Source

pub fn new(world: &mut World) -> Self

Create a context handle from a mutable World reference.

§Safety Contract (enforced by caller, not by the type system)
  • The World must outlive all tasks using this handle.
  • The caller must not use &mut World directly while tasks hold a WorldCtx — all World access must go through with_world.
  • Single-threaded use only (no concurrent with_world calls).

These invariants are structurally enforced by crate::Runtime: the World is created before the runtime, block_on takes &mut self preventing direct World access during execution, and the single-threaded executor prevents concurrent polls.

Source

pub fn with_world<R>(&self, f: impl FnOnce(&mut World) -> R) -> R

Source

pub fn with_world_ref<R>(&self, f: impl FnOnce(&World) -> R) -> R

Run a closure with shared World access.

Use when you only need to read resources.

Trait Implementations§

Source§

impl Clone for WorldCtx

Source§

fn clone(&self) -> WorldCtx

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for WorldCtx

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.