Skip to content

Commit dc1c1b3

Browse files
committed
optimize and/or functions to exit early
1 parent b6c082c commit dc1c1b3

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

dsc_lib/src/functions/and.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ impl Function for And {
3434

3535
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
3636
debug!("{}", t!("functions.and.invoked"));
37-
let mut result = true;
3837
for arg in args {
3938
if let Some(value) = arg.as_bool() {
40-
result &= value;
39+
if !value {
40+
return Ok(Value::Bool(false));
41+
}
4142
} else {
4243
return Err(DscError::Parser(t!("functions.invalidArguments").to_string()));
4344
}
4445
}
45-
Ok(Value::Bool(result))
46+
Ok(Value::Bool(true))
4647
}
4748
}
4849

dsc_lib/src/functions/or.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ impl Function for Or {
3434

3535
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
3636
debug!("{}", t!("functions.or.invoked"));
37-
let mut result = false;
3837
for arg in args {
3938
if let Some(value) = arg.as_bool() {
40-
result |= value;
39+
if value {
40+
return Ok(Value::Bool(true));
41+
}
4142
} else {
4243
return Err(DscError::Parser(t!("functions.invalidArguments").to_string()));
4344
}
4445
}
45-
Ok(Value::Bool(result))
46+
Ok(Value::Bool(false))
4647
}
4748
}
4849

0 commit comments

Comments
 (0)