pub struct WorldCtx { /* private fields */ }Expand description
Copy handle for synchronous World access from async tasks.
§Safety Contract
- Single-threaded only. No concurrent
with_worldcalls. - World outlives tasks. The
Worldmust not be dropped while any task holds aWorldCtx.
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
impl WorldCtx
Sourcepub fn new(world: &mut World) -> Self
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
Worldmust outlive all tasks using this handle. - The caller must not use
&mut Worlddirectly while tasks hold aWorldCtx— all World access must go throughwith_world. - Single-threaded use only (no concurrent
with_worldcalls).
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.
pub fn with_world<R>(&self, f: impl FnOnce(&mut World) -> R) -> R
Sourcepub fn with_world_ref<R>(&self, f: impl FnOnce(&World) -> R) -> R
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§
Auto Trait Implementations§
impl Freeze for WorldCtx
impl !RefUnwindSafe for WorldCtx
impl !Send for WorldCtx
impl !Sync for WorldCtx
impl Unpin for WorldCtx
impl UnsafeUnpin for WorldCtx
impl !UnwindSafe for WorldCtx
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more