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]

Create a new empty context

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));

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);

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));

Trait Implementations

impl<'a> Debug for Context<'a>
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<'a> !Send for Context<'a>

impl<'a> !Sync for Context<'a>