Struct caldyn::Context
[−]
[src]
pub struct Context<'a> { /* fields omitted */ }
A context hold values for variables, that can be used to evaluate expressions.
Examples
let mut context = Context::new(); context.set("a", 3.0); context.set("b", -2.5); assert_eq!(context.get("a"), Some(3.0)); assert_eq!(context.get("d"), None);
Methods
impl<'a> Context<'a>
[src]
fn new() -> Context<'a>
Create a new empty context
fn set<S: Into<String>>(&mut self, name: S, value: f64)
Set a variable with the given name
and value
. If the variable
already exists, the value is updated.
Examples
let mut context = Context::new(); context.set("a", 3.0); assert_eq!(context.get("a"), Some(3.0)); assert_eq!(context.get("d"), None); context.set("d", 5.0); context.set("a", 8.0); assert_eq!(context.get("a"), Some(8.0)); assert_eq!(context.get("d"), Some(5.0));
fn get(&self, name: &str) -> Option<f64>
Get the value of the variable with name
, or None
if the variable
is not defined.
Examples
let mut context = Context::new(); context.set("a", 3.0); assert_eq!(context.get("a"), Some(3.0)); assert_eq!(context.get("d"), None);
fn set_query<F>(&mut self, function: F) where
F: Fn(&str) -> Option<f64> + 'a,
F: Fn(&str) -> Option<f64> + 'a,
Set a function
to be called when a variable is not found in the
context.
Examples
let mut context = Context::new(); context.set("a", 3.0); assert_eq!(context.get("a"), Some(3.0)); assert_eq!(context.get("b"), None); context.set_query(|name| { if name == "a" || name == "b" { Some(28.0) } else { None } }); // The function is only called for "b", because "a" is already present // in the context. assert_eq!(context.get("a"), Some(3.0)); assert_eq!(context.get("b"), Some(28.0)); // If we set the variable "b", the function will not be called context.set("b", 1.0); assert_eq!(context.get("b"), Some(1.0));