LaneContext

Struct LaneContext 

Source
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

Source

pub fn new() -> Self

Creates an empty context.

Source

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.

Source

pub fn get<T: 'static>(&self) -> Option<&T>

Returns a shared reference to a value by type.

Source

pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>

Returns a mutable reference to a value by type.

Source

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

Checks whether a value of the given type is present.

Source

pub fn remove<T: 'static>(&mut self) -> Option<T>

Removes and returns a value by type.

Trait Implementations§

Source§

impl Debug for LaneContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LaneContext

Source§

fn default() -> Self

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

impl Send for LaneContext

Source§

impl Sync for LaneContext

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> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the inner value as &dyn Any.
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.