pub struct Context { /* private fields */ }Expand description
Container for contexts values
§Examples:
use wca::Context;
let ctx = Context::default();
ctx.insert( 42 );
assert_eq!( 42, *ctx.get_ref().unwrap() );let routine = Routine::new_with_ctx
(
| ( args, props ), ctx |
{
let first_arg : i32 = args.get_owned( 0 ).unwrap_or_default();
let ctx_value : &mut i32 = ctx.get_or_default();
*ctx_value += first_arg;
Ok( () )
}
);
let ctx = Context::default();
if let Routine::WithContext( callback ) = routine
{
callback( ( Args( vec![ Value::Number( 1.0 ) ] ), Props( Default::default() ) ), ctx.clone() ).unwrap();
}
assert_eq!( 1, *ctx.get_ref().unwrap() );Implementations§
source§impl Context
impl Context
sourcepub fn former() -> ContextFormer<Context, ReturnContainer>
pub fn former() -> ContextFormer<Context, ReturnContainer>
Make former, variation of builder pattern to form structure defining values of fields step by step.
source§impl Context
impl Context
sourcepub fn insert<T: CloneAny>(&self, value: T)
pub fn insert<T: CloneAny>(&self, value: T)
Insert the T value to the context. If it is already exists - replace it
sourcepub fn get_ref<T: CloneAny>(&self) -> Option<&T>
pub fn get_ref<T: CloneAny>(&self) -> Option<&T>
Return immutable reference on interior object. ! Unsafe !
sourcepub fn get_mut<T: CloneAny>(&self) -> Option<&mut T>
pub fn get_mut<T: CloneAny>(&self) -> Option<&mut T>
Return mutable reference on interior object. ! Unsafe !
sourcepub fn get_or_insert<T: CloneAny>(&self, value: T) -> &mut T
pub fn get_or_insert<T: CloneAny>(&self, value: T) -> &mut T
Insert the value if it doesn’t exists, or take an existing value and return mutable reference to it
sourcepub fn get_or_default<T: CloneAny + Default>(&self) -> &mut T
pub fn get_or_default<T: CloneAny + Default>(&self) -> &mut T
Insert default value if it doesn’t exists, or take an existing value and return mutable reference to it
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
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
source§impl<S> SetWithType for S
impl<S> SetWithType for S
source§fn set_with_type<T, IntoT>(&mut self, component: IntoT)where
IntoT: Into<T>,
S: SetComponent<T, IntoT>,
fn set_with_type<T, IntoT>(&mut self, component: IntoT)where
IntoT: Into<T>,
S: SetComponent<T, IntoT>,
Function to set value of a component by its type.