Make more functions const.

This commit is contained in:
Sebastian Schmidt 2021-07-13 13:55:22 +03:00
parent 20e6e12e7c
commit 9103aa7a26
2 changed files with 8 additions and 14 deletions

View File

@ -102,8 +102,7 @@ impl Operator {
/// Returns the precedence of the operator. /// Returns the precedence of the operator.
/// A high precedence means that the operator has priority to be deeper in the tree. /// A high precedence means that the operator has priority to be deeper in the tree.
// Make this a const fn once #57563 is resolved pub(crate) const fn precedence(&self) -> i32 {
pub(crate) fn precedence(&self) -> i32 {
use crate::operator::Operator::*; use crate::operator::Operator::*;
match self { match self {
RootNode => 200, RootNode => 200,
@ -133,28 +132,25 @@ impl Operator {
/// Returns true if chains of operators with the same precedence as this one should be evaluated left-to-right, /// Returns true if chains of operators with the same precedence as this one should be evaluated left-to-right,
/// and false if they should be evaluated right-to-left. /// and false if they should be evaluated right-to-left.
/// Left-to-right chaining has priority if operators with different order but same precedence are chained. /// Left-to-right chaining has priority if operators with different order but same precedence are chained.
// Make this a const fn once #57563 is resolved pub(crate) const fn is_left_to_right(&self) -> bool {
pub(crate) fn is_left_to_right(&self) -> bool {
use crate::operator::Operator::*; use crate::operator::Operator::*;
!matches!(self, Assign | FunctionIdentifier { identifier: _ }) !matches!(self, Assign | FunctionIdentifier { identifier: _ })
} }
/// Returns true if chains of this operator should be flattened into one operator with many arguments. /// 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) const fn is_sequence(&self) -> bool {
pub(crate) fn is_sequence(&self) -> bool {
use crate::operator::Operator::*; use crate::operator::Operator::*;
matches!(self, Tuple | Chain) matches!(self, Tuple | Chain)
} }
/// True if this operator is a leaf, meaning it accepts no arguments. /// True if this operator is a leaf, meaning it accepts no arguments.
// Make this a const fn once #57563 is resolved // Make this a const fn as soon as whatever is missing gets stable (issue #57563)
pub(crate) fn is_leaf(&self) -> bool { pub(crate) fn is_leaf(&self) -> bool {
self.max_argument_amount() == Some(0) self.max_argument_amount() == Some(0)
} }
/// Returns the maximum amount of arguments required by this operator. /// Returns the maximum amount of arguments required by this operator.
// Make this a const fn once #57563 is resolved pub(crate) const fn max_argument_amount(&self) -> Option<usize> {
pub(crate) fn max_argument_amount(&self) -> Option<usize> {
use crate::operator::Operator::*; use crate::operator::Operator::*;
match self { match self {
Add | Sub | Mul | Div | Mod | Exp | Eq | Neq | Gt | Lt | Geq | Leq | And | Or Add | Sub | Mul | Div | Mod | Exp | Eq | Neq | Gt | Lt | Geq | Leq | And | Or

View File

@ -88,7 +88,7 @@ pub enum PartialToken {
VerticalBar, VerticalBar,
} }
// Make this a const fn as soon as match gets stable (issue #57563) // Make this a const fn as soon as is_whitespace and to_string get stable (issue #57563)
fn char_to_partial_token(c: char) -> PartialToken { fn char_to_partial_token(c: char) -> PartialToken {
match c { match c {
'+' => PartialToken::Plus, '+' => PartialToken::Plus,
@ -122,8 +122,7 @@ fn char_to_partial_token(c: char) -> PartialToken {
} }
impl Token { impl Token {
// Make this a const fn as soon as match gets stable (issue #57563) pub(crate) const fn is_leftsided_value(&self) -> bool {
pub(crate) fn is_leftsided_value(&self) -> bool {
match self { match self {
Token::Plus => false, Token::Plus => false,
Token::Minus => false, Token::Minus => false,
@ -166,8 +165,7 @@ impl Token {
} }
} }
// Make this a const fn as soon as match gets stable (issue #57563) pub(crate) const fn is_rightsided_value(&self) -> bool {
pub(crate) fn is_rightsided_value(&self) -> bool {
match self { match self {
Token::Plus => false, Token::Plus => false,
Token::Minus => false, Token::Minus => false,