Fix lints and benches.

This commit is contained in:
Sebastian Schmidt 2021-05-28 09:07:26 +03:00
parent 1316ae63d0
commit 6e5ff8615a
6 changed files with 54 additions and 134 deletions

View File

@ -1,4 +1,5 @@
#![feature(test)]
#![feature(bench_black_box)]
extern crate rand;
extern crate rand_pcg;

View File

@ -86,7 +86,7 @@ impl Context for HashMapContext {
}
fn set_function(&mut self, identifier: String, function: Function) -> EvalexprResult<()> {
self.functions.insert(identifier.into(), function);
self.functions.insert(identifier, function);
Ok(())
}
}

View File

@ -20,7 +20,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
} else if let Value::Int(int) = argument {
min_int = min_int.min(int);
} else {
return Err(EvalexprError::expected_number(argument.clone()));
return Err(EvalexprError::expected_number(argument));
}
}
@ -42,7 +42,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
} else if let Value::Int(int) = argument {
max_int = max_int.max(int);
} else {
return Err(EvalexprError::expected_number(argument.clone()));
return Err(EvalexprError::expected_number(argument));
}
}

View File

@ -136,21 +136,14 @@ impl Operator {
// Make this a const fn once #57563 is resolved
pub(crate) fn is_left_to_right(&self) -> bool {
use crate::operator::Operator::*;
match self {
Assign => false,
FunctionIdentifier { identifier: _ } => false,
_ => true,
}
!matches!(self, Assign | FunctionIdentifier { identifier: _ })
}
/// Returns true if chains of this operator should be flattened into one operator with many arguments.
// Make this a const fn once #57563 is resolved
pub(crate) fn is_sequence(&self) -> bool {
use crate::operator::Operator::*;
match self {
Tuple | Chain => true,
_ => false,
}
matches!(self, Tuple | Chain)
}
/// True if this operator is a leaf, meaning it accepts no arguments.
@ -327,20 +320,12 @@ impl Operator {
Eq => {
expect_operator_argument_amount(arguments.len(), 2)?;
if arguments[0] == arguments[1] {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(arguments[0] == arguments[1]))
},
Neq => {
expect_operator_argument_amount(arguments.len(), 2)?;
if arguments[0] != arguments[1] {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(arguments[0] != arguments[1]))
},
Gt => {
expect_operator_argument_amount(arguments.len(), 2)?;
@ -348,23 +333,13 @@ impl Operator {
expect_number_or_string(&arguments[1])?;
if let (Ok(a), Ok(b)) = (arguments[0].as_string(), arguments[1].as_string()) {
if a > b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a > b))
} else if let (Ok(a), Ok(b)) = (arguments[0].as_int(), arguments[1].as_int()) {
if a > b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a > b))
} else {
if arguments[0].as_number()? > arguments[1].as_number()? {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(
arguments[0].as_number()? > arguments[1].as_number()?,
))
}
},
Lt => {
@ -373,23 +348,13 @@ impl Operator {
expect_number_or_string(&arguments[1])?;
if let (Ok(a), Ok(b)) = (arguments[0].as_string(), arguments[1].as_string()) {
if a < b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a < b))
} else if let (Ok(a), Ok(b)) = (arguments[0].as_int(), arguments[1].as_int()) {
if a < b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a < b))
} else {
if arguments[0].as_number()? < arguments[1].as_number()? {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(
arguments[0].as_number()? < arguments[1].as_number()?,
))
}
},
Geq => {
@ -398,23 +363,13 @@ impl Operator {
expect_number_or_string(&arguments[1])?;
if let (Ok(a), Ok(b)) = (arguments[0].as_string(), arguments[1].as_string()) {
if a >= b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a >= b))
} else if let (Ok(a), Ok(b)) = (arguments[0].as_int(), arguments[1].as_int()) {
if a >= b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a >= b))
} else {
if arguments[0].as_number()? >= arguments[1].as_number()? {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(
arguments[0].as_number()? >= arguments[1].as_number()?,
))
}
},
Leq => {
@ -423,23 +378,13 @@ impl Operator {
expect_number_or_string(&arguments[1])?;
if let (Ok(a), Ok(b)) = (arguments[0].as_string(), arguments[1].as_string()) {
if a <= b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a <= b))
} else if let (Ok(a), Ok(b)) = (arguments[0].as_int(), arguments[1].as_int()) {
if a <= b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a <= b))
} else {
if arguments[0].as_number()? <= arguments[1].as_number()? {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(
arguments[0].as_number()? <= arguments[1].as_number()?,
))
}
},
And => {
@ -447,32 +392,20 @@ impl Operator {
let a = arguments[0].as_boolean()?;
let b = arguments[1].as_boolean()?;
if a && b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a && b))
},
Or => {
expect_operator_argument_amount(arguments.len(), 2)?;
let a = arguments[0].as_boolean()?;
let b = arguments[1].as_boolean()?;
if a || b {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(a || b))
},
Not => {
expect_operator_argument_amount(arguments.len(), 1)?;
let a = arguments[0].as_boolean()?;
if !a {
Ok(Value::Boolean(true))
} else {
Ok(Value::Boolean(false))
}
Ok(Value::Boolean(!a))
},
Assign | AddAssign | SubAssign | MulAssign | DivAssign | ModAssign | ExpAssign
| AndAssign | OrAssign => Err(EvalexprError::ContextNotManipulable),
@ -528,7 +461,7 @@ impl Operator {
Assign => {
expect_operator_argument_amount(arguments.len(), 2)?;
let target = arguments[0].as_string()?;
context.set_value(target.into(), arguments[1].clone())?;
context.set_value(target, arguments[1].clone())?;
Ok(Value::Empty)
},
@ -557,7 +490,7 @@ impl Operator {
self
),
}?;
context.set_value(target.into(), result)?;
context.set_value(target, result)?;
Ok(Value::Empty)
},

View File

@ -196,11 +196,18 @@ impl Token {
pub(crate) fn is_assignment(&self) -> bool {
use Token::*;
match self {
Assign | PlusAssign | MinusAssign | StarAssign | SlashAssign | PercentAssign
| HatAssign | AndAssign | OrAssign => true,
_ => false,
}
matches!(
self,
Assign
| PlusAssign
| MinusAssign
| StarAssign
| SlashAssign
| PercentAssign
| HatAssign
| AndAssign
| OrAssign
)
}
}
@ -210,7 +217,7 @@ fn parse_escape_sequence<Iter: Iterator<Item = char>>(iter: &mut Iter) -> Evalex
Some('"') => Ok('"'),
Some('\\') => Ok('\\'),
Some(c) => Err(EvalexprError::IllegalEscapeSequence(format!("\\{}", c))),
None => Err(EvalexprError::IllegalEscapeSequence(format!("\\"))),
None => Err(EvalexprError::IllegalEscapeSequence("\\".to_string())),
}
}
@ -268,7 +275,7 @@ fn str_to_partial_tokens(string: &str) -> EvalexprResult<Vec<PartialToken>> {
/// Resolves all partial tokens by converting them to complex tokens.
fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<Token>> {
let mut result = Vec::new();
while tokens.len() > 0 {
while !tokens.is_empty() {
let first = tokens[0].clone();
let second = tokens.get(1).cloned();
let third = tokens.get(2).cloned();

View File

@ -40,57 +40,36 @@ pub enum Value {
impl Value {
/// Returns true if `self` is a `Value::String`.
pub fn is_string(&self) -> bool {
match self {
Value::String(_) => true,
_ => false,
}
matches!(self, Value::String(_))
}
/// Returns true if `self` is a `Value::Int`.
pub fn is_int(&self) -> bool {
match self {
Value::Int(_) => true,
_ => false,
}
matches!(self, Value::Int(_))
}
/// Returns true if `self` is a `Value::Float`.
pub fn is_float(&self) -> bool {
match self {
Value::Float(_) => true,
_ => false,
}
matches!(self, Value::Float(_))
}
/// Returns true if `self` is a `Value::Int` or `Value::Float`.
pub fn is_number(&self) -> bool {
match self {
Value::Int(_) | Value::Float(_) => true,
_ => false,
}
matches!(self, Value::Int(_) | Value::Float(_))
}
/// Returns true if `self` is a `Value::Boolean`.
pub fn is_boolean(&self) -> bool {
match self {
Value::Boolean(_) => true,
_ => false,
}
matches!(self, Value::Boolean(_))
}
/// Returns true if `self` is a `Value::Tuple`.
pub fn is_tuple(&self) -> bool {
match self {
Value::Tuple(_) => true,
_ => false,
}
matches!(self, Value::Tuple(_))
}
/// Returns true if `self` is a `Value::Empty`.
pub fn is_empty(&self) -> bool {
match self {
Value::Empty => true,
_ => false,
}
matches!(self, Value::Empty)
}
/// Clones the value stored in `self` as `String`, or returns `Err` if `self` is not a `Value::String`.