pub struct BindContext<'a> { /* private fields */ }Expand description
Bindings context for a cel evaluation.
This struct houses all of the bindings (dynamic values) for a cel evaluation.
Currently the types of identifiers that can be bound to are variables, functions
and macros. This context is separate from the contents of the CelContext to allow
for multiple runs with different bound values on the same programs without the need
to maintain multiple copies of the programs.
Implementations§
Source§impl<'a> BindContext<'a>
impl<'a> BindContext<'a>
Sourcepub fn new() -> BindContext<'a>
pub fn new() -> BindContext<'a>
Create a new bind context contain default functions and macros.
Examples found in repository?
examples/bench.rs (line 30)
28fn bench_run_one_nobindings() {
29 let mut cel = CelContext::new();
30 let exec = BindContext::new();
31
32 cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
33
34 cel.exec("entry", &exec).unwrap();
35}
36
37fn bench_run_many_no_bindings() {
38 let mut cel = CelContext::new();
39 let exec = BindContext::new();
40
41 cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
42
43 for _ in 0..10_000 {
44 cel.exec("entry", &exec).unwrap();
45 }
46}
47
48fn bench_run_one_with_binding() {
49 let mut cel = CelContext::new();
50 let mut exec = BindContext::new();
51
52 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
53 exec.bind_param("foo", 6.into());
54
55 cel.exec("entry", &exec).unwrap();
56}
57
58fn bench_run_one_with_many_bindings() {
59 let mut cel = CelContext::new();
60 let mut exec = BindContext::new();
61
62 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
63
64 for o in 0..10_000 {
65 exec.bind_param("foo", o.into());
66
67 cel.exec("entry", &exec).unwrap();
68 }
69}
70
71fn bench_build_many() {
72 let mut cel = CelContext::new();
73
74 for o in 0..1_000 {
75 cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
76 .unwrap();
77 }
78}
79
80fn bench_construct_many_with_bindings() {
81 for o in 0..10_000 {
82 let mut cel = CelContext::new();
83 let mut exec = BindContext::new();
84
85 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
86 exec.bind_param("foo", o.into());
87
88 cel.exec("entry", &exec).unwrap();
89 }
90}More examples
examples/eval.rs (line 7)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let mut context = CelContext::new();
7 let mut exec = BindContext::new();
8
9 context.add_program_str("prog", &args[1]).unwrap();
10
11 if args.len() > 2 {
12 exec.bind_params_from_json_obj(args[2].parse().unwrap())
13 .unwrap();
14 }
15
16 let res = context.exec("prog", &exec).unwrap();
17 println!("{}", res);
18}pub fn for_compile() -> BindContext<'a>
Sourcepub fn bind_param(&mut self, name: &str, value: CelValue)
pub fn bind_param(&mut self, name: &str, value: CelValue)
Bind a param with the given name and value.
Examples found in repository?
examples/bench.rs (line 53)
48fn bench_run_one_with_binding() {
49 let mut cel = CelContext::new();
50 let mut exec = BindContext::new();
51
52 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
53 exec.bind_param("foo", 6.into());
54
55 cel.exec("entry", &exec).unwrap();
56}
57
58fn bench_run_one_with_many_bindings() {
59 let mut cel = CelContext::new();
60 let mut exec = BindContext::new();
61
62 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
63
64 for o in 0..10_000 {
65 exec.bind_param("foo", o.into());
66
67 cel.exec("entry", &exec).unwrap();
68 }
69}
70
71fn bench_build_many() {
72 let mut cel = CelContext::new();
73
74 for o in 0..1_000 {
75 cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
76 .unwrap();
77 }
78}
79
80fn bench_construct_many_with_bindings() {
81 for o in 0..10_000 {
82 let mut cel = CelContext::new();
83 let mut exec = BindContext::new();
84
85 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
86 exec.bind_param("foo", o.into());
87
88 cel.exec("entry", &exec).unwrap();
89 }
90}Sourcepub fn bind_params_from_json_obj(&mut self, values: Value) -> CelResult<()>
pub fn bind_params_from_json_obj(&mut self, values: Value) -> CelResult<()>
Cheater function to bind the keys of an JSON object with its values
Examples found in repository?
More examples
examples/eval.rs (line 12)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let mut context = CelContext::new();
7 let mut exec = BindContext::new();
8
9 context.add_program_str("prog", &args[1]).unwrap();
10
11 if args.len() > 2 {
12 exec.bind_params_from_json_obj(args[2].parse().unwrap())
13 .unwrap();
14 }
15
16 let res = context.exec("prog", &exec).unwrap();
17 println!("{}", res);
18}pub fn bind_param_proto_msg(&mut self, name: &str, msg: Box<dyn MessageDyn>)
Sourcepub fn bind_func(&mut self, name: &str, func: &'a RsCelFunction)
pub fn bind_func(&mut self, name: &str, func: &'a RsCelFunction)
Bind a function to the bind context, can be new or overwrite an existing (including default)
Sourcepub fn bind_macro(&mut self, name: &str, macro_: &'a RsCelMacro)
pub fn bind_macro(&mut self, name: &str, macro_: &'a RsCelMacro)
Bind a macro to the bind context.
Sourcepub fn get_func(&self, name: &str) -> Option<&'a RsCelFunction>
pub fn get_func(&self, name: &str) -> Option<&'a RsCelFunction>
Get a function by name.
Sourcepub fn get_macro(&self, name: &str) -> Option<&'a RsCelMacro>
pub fn get_macro(&self, name: &str) -> Option<&'a RsCelMacro>
Get a macro by name.
pub fn is_bound(&self, name: &str) -> bool
Trait Implementations§
Source§impl<'a> Clone for BindContext<'a>
impl<'a> Clone for BindContext<'a>
Source§fn clone(&self) -> BindContext<'a>
fn clone(&self) -> BindContext<'a>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<'a> Freeze for BindContext<'a>
impl<'a> !RefUnwindSafe for BindContext<'a>
impl<'a> !Send for BindContext<'a>
impl<'a> !Sync for BindContext<'a>
impl<'a> Unpin for BindContext<'a>
impl<'a> !UnwindSafe for BindContext<'a>
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