pub struct LaneContext { /* private fields */ }Expand description
A type-erased, extensible context for passing data to lanes.
Agents populate a LaneContext with the data their lanes need,
then pass it to Lane::execute, Lane::on_initialize, etc.
Lanes retrieve specific data by type using get.
§Adding data
ⓘ
use khora_core::lane::LaneContext;
let mut ctx = LaneContext::new();
ctx.insert(42u32);
ctx.insert(String::from("hello"));
assert_eq!(ctx.get::<u32>(), Some(&42));
assert_eq!(ctx.get::<String>().unwrap(), "hello");§Mutable references
For data that is borrowed (not owned), use Slot (mutable) or
Ref (shared) wrappers:
ⓘ
use khora_core::lane::{LaneContext, Slot};
let mut value = 10u32;
let mut ctx = LaneContext::new();
ctx.insert(Slot::new(&mut value));
let slot = ctx.get::<Slot<u32>>().unwrap();
*slot.get() = 20;§Safety
LaneContext uses unsafe impl Send + Sync because it may hold
Slot / Ref wrappers containing raw pointers. This is safe
because the context is stack-scoped: created by the agent, passed to
one lane at a time, and dropped before the next frame.
Implementations§
Source§impl LaneContext
impl LaneContext
Sourcepub fn insert<T: 'static + Send + Sync>(&mut self, value: T)
pub fn insert<T: 'static + Send + Sync>(&mut self, value: T)
Inserts a value, keyed by its concrete type.
If a value of the same type was already present, it is replaced.
Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
Returns a mutable reference to a value by type.
Trait Implementations§
Source§impl Debug for LaneContext
impl Debug for LaneContext
Source§impl Default for LaneContext
impl Default for LaneContext
impl Send for LaneContext
impl Sync for LaneContext
Auto Trait Implementations§
impl Freeze for LaneContext
impl !RefUnwindSafe for LaneContext
impl Unpin for LaneContext
impl !UnwindSafe for LaneContext
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