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.
/// 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) fn precedence(&self) -> i32 {
pub(crate) const fn precedence(&self) -> i32 {
use crate::operator::Operator::*;
match self {
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,
/// 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.
// Make this a const fn once #57563 is resolved
pub(crate) fn is_left_to_right(&self) -> bool {
pub(crate) const fn is_left_to_right(&self) -> bool {
use crate::operator::Operator::*;
!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 {
pub(crate) const fn is_sequence(&self) -> bool {
use crate::operator::Operator::*;
matches!(self, Tuple | Chain)
}
/// 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 {
self.max_argument_amount() == Some(0)
}
/// Returns the maximum amount of arguments required by this operator.
// Make this a const fn once #57563 is resolved
pub(crate) fn max_argument_amount(&self) -> Option<usize> {
pub(crate) const fn max_argument_amount(&self) -> Option<usize> {
use crate::operator::Operator::*;
match self {
Add | Sub | Mul | Div | Mod | Exp | Eq | Neq | Gt | Lt | Geq | Leq | And | Or

View File

@ -88,7 +88,7 @@ pub enum PartialToken {
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 {
match c {
'+' => PartialToken::Plus,
@ -122,8 +122,7 @@ fn char_to_partial_token(c: char) -> PartialToken {
}
impl Token {
// Make this a const fn as soon as match gets stable (issue #57563)
pub(crate) fn is_leftsided_value(&self) -> bool {
pub(crate) const fn is_leftsided_value(&self) -> bool {
match self {
Token::Plus => 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) fn is_rightsided_value(&self) -> bool {
pub(crate) const fn is_rightsided_value(&self) -> bool {
match self {
Token::Plus => false,
Token::Minus => false,