From a2858e0fd31894a5a07f40288aaa6cc55cd3f1ac Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 1 Aug 2020 12:19:27 +0800 Subject: [PATCH] Refine feature gates --- src/fn_call.rs | 96 ++++++++-------- src/packages/arithmetic.rs | 210 +++++++++++++---------------------- src/packages/array_basic.rs | 8 +- src/packages/iter_basic.rs | 18 ++- src/packages/logic.rs | 7 +- src/packages/map_basic.rs | 19 ++-- src/packages/math_basic.rs | 35 +++--- src/packages/string_basic.rs | 7 +- src/packages/string_more.rs | 9 +- 9 files changed, 164 insertions(+), 245 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index da029c78..4b40df64 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1023,30 +1023,30 @@ pub fn run_builtin_binary_op( let x = x.clone().cast::(); let y = y.clone().cast::(); - #[cfg(not(feature = "unchecked"))] - match op { - "+" => return add(x, y).map(Into::into).map(Some), - "-" => return sub(x, y).map(Into::into).map(Some), - "*" => return mul(x, y).map(Into::into).map(Some), - "/" => return div(x, y).map(Into::into).map(Some), - "%" => return modulo(x, y).map(Into::into).map(Some), - "~" => return pow_i_i(x, y).map(Into::into).map(Some), - ">>" => return shr(x, y).map(Into::into).map(Some), - "<<" => return shl(x, y).map(Into::into).map(Some), - _ => (), - } - - #[cfg(feature = "unchecked")] - match op { - "+" => return Ok(Some((x + y).into())), - "-" => return Ok(Some((x - y).into())), - "*" => return Ok(Some((x * y).into())), - "/" => return Ok(Some((x / y).into())), - "%" => return Ok(Some((x % y).into())), - "~" => return pow_i_i_u(x, y).map(Into::into).map(Some), - ">>" => return shr_u(x, y).map(Into::into).map(Some), - "<<" => return shl_u(x, y).map(Into::into).map(Some), - _ => (), + if cfg!(not(feature = "unchecked")) { + match op { + "+" => return add(x, y).map(Into::into).map(Some), + "-" => return sub(x, y).map(Into::into).map(Some), + "*" => return mul(x, y).map(Into::into).map(Some), + "/" => return div(x, y).map(Into::into).map(Some), + "%" => return modulo(x, y).map(Into::into).map(Some), + "~" => return pow_i_i(x, y).map(Into::into).map(Some), + ">>" => return shr(x, y).map(Into::into).map(Some), + "<<" => return shl(x, y).map(Into::into).map(Some), + _ => (), + } + } else { + match op { + "+" => return Ok(Some((x + y).into())), + "-" => return Ok(Some((x - y).into())), + "*" => return Ok(Some((x * y).into())), + "/" => return Ok(Some((x / y).into())), + "%" => return Ok(Some((x % y).into())), + "~" => return pow_i_i_u(x, y).map(Into::into).map(Some), + ">>" => return shr_u(x, y).map(Into::into).map(Some), + "<<" => return shl_u(x, y).map(Into::into).map(Some), + _ => (), + } } match op { @@ -1151,30 +1151,30 @@ pub fn run_builtin_op_assignment( let x = x.downcast_mut::().unwrap(); let y = y.clone().cast::(); - #[cfg(not(feature = "unchecked"))] - match op { - "+=" => return Ok(Some(*x = add(*x, y)?)), - "-=" => return Ok(Some(*x = sub(*x, y)?)), - "*=" => return Ok(Some(*x = mul(*x, y)?)), - "/=" => return Ok(Some(*x = div(*x, y)?)), - "%=" => return Ok(Some(*x = modulo(*x, y)?)), - "~=" => return Ok(Some(*x = pow_i_i(*x, y)?)), - ">>=" => return Ok(Some(*x = shr(*x, y)?)), - "<<=" => return Ok(Some(*x = shl(*x, y)?)), - _ => (), - } - - #[cfg(feature = "unchecked")] - match op { - "+=" => return Ok(Some(*x += y)), - "-=" => return Ok(Some(*x -= y)), - "*=" => return Ok(Some(*x *= y)), - "/=" => return Ok(Some(*x /= y)), - "%=" => return Ok(Some(*x %= y)), - "~=" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), - ">>=" => return Ok(Some(*x = shr_u(*x, y)?)), - "<<=" => return Ok(Some(*x = shl_u(*x, y)?)), - _ => (), + if cfg!(not(feature = "unchecked")) { + match op { + "+=" => return Ok(Some(*x = add(*x, y)?)), + "-=" => return Ok(Some(*x = sub(*x, y)?)), + "*=" => return Ok(Some(*x = mul(*x, y)?)), + "/=" => return Ok(Some(*x = div(*x, y)?)), + "%=" => return Ok(Some(*x = modulo(*x, y)?)), + "~=" => return Ok(Some(*x = pow_i_i(*x, y)?)), + ">>=" => return Ok(Some(*x = shr(*x, y)?)), + "<<=" => return Ok(Some(*x = shl(*x, y)?)), + _ => (), + } + } else { + match op { + "+=" => return Ok(Some(*x += y)), + "-=" => return Ok(Some(*x -= y)), + "*=" => return Ok(Some(*x *= y)), + "/=" => return Ok(Some(*x /= y)), + "%=" => return Ok(Some(*x %= y)), + "~=" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), + ">>=" => return Ok(Some(*x = shr_u(*x, y)?)), + "<<=" => return Ok(Some(*x = shl_u(*x, y)?)), + _ => (), + } } match op { diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index fd95e675..d8e256cc 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -2,38 +2,29 @@ use crate::def_package; use crate::module::FuncReturn; use crate::parser::INT; -#[cfg(not(feature = "unchecked"))] use crate::{result::EvalAltResult, token::Position}; #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; -#[cfg(not(feature = "no_float"))] -#[cfg(feature = "no_std")] -use num_traits::*; - -#[cfg(not(feature = "unchecked"))] use num_traits::{ identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, }; -#[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "only_i64"))] -use crate::stdlib::ops::{BitAnd, BitOr, BitXor}; +#[cfg(feature = "no_std")] +#[cfg(not(feature = "no_float"))] +use num_traits::float::Float; -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] -use crate::stdlib::ops::{Add, Div, Mul, Neg, Rem, Sub}; - -#[cfg(feature = "unchecked")] -use crate::stdlib::ops::{Shl, Shr}; - -#[cfg(not(feature = "unchecked"))] -use crate::stdlib::{boxed::Box, fmt::Display, format}; +use crate::stdlib::{ + boxed::Box, + fmt::Display, + format, + ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub}, +}; // Checked add -#[cfg(not(feature = "unchecked"))] -pub(crate) fn add(x: T, y: T) -> FuncReturn { +pub fn add(x: T, y: T) -> FuncReturn { x.checked_add(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Addition overflow: {} + {}", x, y), @@ -42,8 +33,7 @@ pub(crate) fn add(x: T, y: T) -> FuncReturn { }) } // Checked subtract -#[cfg(not(feature = "unchecked"))] -pub(crate) fn sub(x: T, y: T) -> FuncReturn { +pub fn sub(x: T, y: T) -> FuncReturn { x.checked_sub(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Subtraction underflow: {} - {}", x, y), @@ -52,8 +42,7 @@ pub(crate) fn sub(x: T, y: T) -> FuncReturn { }) } // Checked multiply -#[cfg(not(feature = "unchecked"))] -pub(crate) fn mul(x: T, y: T) -> FuncReturn { +pub fn mul(x: T, y: T) -> FuncReturn { x.checked_mul(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Multiplication overflow: {} * {}", x, y), @@ -62,8 +51,7 @@ pub(crate) fn mul(x: T, y: T) -> FuncReturn { }) } // Checked divide -#[cfg(not(feature = "unchecked"))] -pub(crate) fn div(x: T, y: T) -> FuncReturn +pub fn div(x: T, y: T) -> FuncReturn where T: Display + CheckedDiv + PartialEq + Zero, { @@ -83,8 +71,7 @@ where }) } // Checked negative - e.g. -(i32::MIN) will overflow i32::MAX -#[cfg(not(feature = "unchecked"))] -pub(crate) fn neg(x: T) -> FuncReturn { +pub fn neg(x: T) -> FuncReturn { x.checked_neg().ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Negation overflow: -{}", x), @@ -93,8 +80,7 @@ pub(crate) fn neg(x: T) -> FuncReturn { }) } // Checked absolute -#[cfg(not(feature = "unchecked"))] -pub(crate) fn abs(x: T) -> FuncReturn { +pub fn abs(x: T) -> FuncReturn { // FIX - We don't use Signed::abs() here because, contrary to documentation, it panics // when the number is ::MIN instead of returning ::MIN itself. if x >= ::zero() { @@ -109,32 +95,26 @@ pub(crate) fn abs(x: T) -> FuncRetu } } // Unchecked add - may panic on overflow -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn add_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x + y) } // Unchecked subtract - may panic on underflow -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn sub_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x - y) } // Unchecked multiply - may panic on overflow -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn mul_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x * y) } // Unchecked divide - may panic when dividing by zero -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn div_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x / y) } // Unchecked negative - may panic on overflow -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn neg_u(x: T) -> FuncReturn<::Output> { Ok(-x) } // Unchecked absolute - may panic on overflow -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn abs_u(x: T) -> FuncReturn<::Output> where T: Neg + PartialOrd + Default + Into<::Output>, @@ -147,24 +127,17 @@ where } } // Bit operators -#[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "only_i64"))] fn binary_and(x: T, y: T) -> FuncReturn<::Output> { Ok(x & y) } -#[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "only_i64"))] fn binary_or(x: T, y: T) -> FuncReturn<::Output> { Ok(x | y) } -#[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "only_i64"))] fn binary_xor(x: T, y: T) -> FuncReturn<::Output> { Ok(x ^ y) } // Checked left-shift -#[cfg(not(feature = "unchecked"))] -pub(crate) fn shl(x: T, y: INT) -> FuncReturn { +pub fn shl(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -181,8 +154,7 @@ pub(crate) fn shl(x: T, y: INT) -> FuncReturn { }) } // Checked right-shift -#[cfg(not(feature = "unchecked"))] -pub(crate) fn shr(x: T, y: INT) -> FuncReturn { +pub fn shr(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -199,18 +171,15 @@ pub(crate) fn shr(x: T, y: INT) -> FuncReturn { }) } // Unchecked left-shift - may panic if shifting by a negative number of bits -#[cfg(feature = "unchecked")] -pub(crate) fn shl_u>(x: T, y: T) -> FuncReturn<>::Output> { +pub fn shl_u>(x: T, y: T) -> FuncReturn<>::Output> { Ok(x.shl(y)) } // Unchecked right-shift - may panic if shifting by a negative number of bits -#[cfg(feature = "unchecked")] -pub(crate) fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> { +pub fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> { Ok(x.shr(y)) } // Checked modulo -#[cfg(not(feature = "unchecked"))] -pub(crate) fn modulo(x: T, y: T) -> FuncReturn { +pub fn modulo(x: T, y: T) -> FuncReturn { x.checked_rem(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Modulo division by zero or overflow: {} % {}", x, y), @@ -219,62 +188,58 @@ pub(crate) fn modulo(x: T, y: T) -> FuncReturn { }) } // Unchecked modulo - may panic if dividing by zero -#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn modulo_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x % y) } // Checked power -#[cfg(not(feature = "unchecked"))] -pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn { - #[cfg(not(feature = "only_i32"))] - if y > (u32::MAX as INT) { - Err(Box::new(EvalAltResult::ErrorArithmetic( - format!("Integer raised to too large an index: {} ~ {}", x, y), - Position::none(), - ))) - } else if y < 0 { - Err(Box::new(EvalAltResult::ErrorArithmetic( - format!("Integer raised to a negative index: {} ~ {}", x, y), - Position::none(), - ))) - } else { - x.checked_pow(y as u32).ok_or_else(|| { - Box::new(EvalAltResult::ErrorArithmetic( - format!("Power overflow: {} ~ {}", x, y), +pub fn pow_i_i(x: INT, y: INT) -> FuncReturn { + if cfg!(not(feature = "only_i32")) { + if y > (u32::MAX as INT) { + Err(Box::new(EvalAltResult::ErrorArithmetic( + format!("Integer raised to too large an index: {} ~ {}", x, y), Position::none(), - )) - }) - } - - #[cfg(feature = "only_i32")] - if y < 0 { - Err(Box::new(EvalAltResult::ErrorArithmetic( - format!("Integer raised to a negative index: {} ~ {}", x, y), - Position::none(), - ))) - } else { - x.checked_pow(y as u32).ok_or_else(|| { - Box::new(EvalAltResult::ErrorArithmetic( - format!("Power overflow: {} ~ {}", x, y), + ))) + } else if y < 0 { + Err(Box::new(EvalAltResult::ErrorArithmetic( + format!("Integer raised to a negative index: {} ~ {}", x, y), Position::none(), - )) - }) + ))) + } else { + x.checked_pow(y as u32).ok_or_else(|| { + Box::new(EvalAltResult::ErrorArithmetic( + format!("Power overflow: {} ~ {}", x, y), + Position::none(), + )) + }) + } + } else { + if y < 0 { + Err(Box::new(EvalAltResult::ErrorArithmetic( + format!("Integer raised to a negative index: {} ~ {}", x, y), + Position::none(), + ))) + } else { + x.checked_pow(y as u32).ok_or_else(|| { + Box::new(EvalAltResult::ErrorArithmetic( + format!("Power overflow: {} ~ {}", x, y), + Position::none(), + )) + }) + } } } // Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX) -#[cfg(feature = "unchecked")] -pub(crate) fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { +pub fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { Ok(x.pow(y as u32)) } // Floating-point power - always well-defined #[cfg(not(feature = "no_float"))] -pub(crate) fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { +pub fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { Ok(x.powf(y)) } // Checked power #[cfg(not(feature = "no_float"))] -#[cfg(not(feature = "unchecked"))] -pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { +pub fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -286,9 +251,8 @@ pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { Ok(x.powi(y as i32)) } // Unchecked power - may be incorrect if the power index is too high (> i32::MAX) -#[cfg(feature = "unchecked")] #[cfg(not(feature = "no_float"))] -pub(crate) fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { +pub fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { Ok(x.powi(y as i32)) } @@ -317,11 +281,8 @@ macro_rules! reg_sign { } def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - #[cfg(not(feature = "unchecked"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { + if cfg!(not(feature = "unchecked")) { // Checked basic arithmetic reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64); @@ -332,8 +293,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "+", add, i128, u128); reg_op!(lib, "-", sub, i128, u128); reg_op!(lib, "*", mul, i128, u128); @@ -345,8 +305,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { } } - #[cfg(feature = "unchecked")] - { + if cfg!(feature = "unchecked") { // Unchecked basic arithmetic reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64); @@ -357,8 +316,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "+", add_u, i128, u128); reg_op!(lib, "-", sub_u, i128, u128); reg_op!(lib, "*", mul_u, i128, u128); @@ -372,13 +330,13 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_sign!(lib, "sign", INT, i8, i16, i32, i64); - #[cfg(not(target_arch = "wasm32"))] - reg_sign!(lib, "sign", INT, i128); + if cfg!(not(target_arch = "wasm32")) { + reg_sign!(lib, "sign", INT, i128); + } } // Basic arithmetic for floating-point - no need to check - #[cfg(not(feature = "no_float"))] - { + if cfg!(not(feature = "no_float")) { reg_op!(lib, "+", add_u, f32); reg_op!(lib, "-", sub_u, f32); reg_op!(lib, "*", mul_u, f32); @@ -387,15 +345,12 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_sign!(lib, "sign", f64, f64); } - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "|", binary_or, i128, u128); reg_op!(lib, "&", binary_and, i128, u128); reg_op!(lib, "^", binary_xor, i128, u128); @@ -405,12 +360,11 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { #[cfg(not(feature = "no_float"))] { // Checked power - #[cfg(not(feature = "unchecked"))] - lib.set_fn_2("~", pow_f_i); - - // Unchecked power - #[cfg(feature = "unchecked")] - lib.set_fn_2("~", pow_f_i_u); + if cfg!(not(feature = "unchecked")) { + lib.set_fn_2("~", pow_f_i); + } else { + lib.set_fn_2("~", pow_f_i_u); + } // Floating-point modulo and power reg_op!(lib, "%", modulo_u, f32); @@ -421,19 +375,15 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { } // Checked unary - #[cfg(not(feature = "unchecked"))] - { + if cfg!(not(feature = "unchecked")) { reg_unary!(lib, "-", neg, INT); reg_unary!(lib, "abs", abs, INT); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_unary!(lib, "-", neg, i8, i16, i32, i64); reg_unary!(lib, "abs", abs, i8, i16, i32, i64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_unary!(lib, "-", neg, i128); reg_unary!(lib, "abs", abs, i128); } @@ -441,19 +391,15 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { } // Unchecked unary - #[cfg(feature = "unchecked")] - { + if cfg!(feature = "unchecked") { reg_unary!(lib, "-", neg_u, INT); reg_unary!(lib, "abs", abs_u, INT); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_unary!(lib, "-", neg_u, i8, i16, i32, i64); reg_unary!(lib, "abs", abs_u, i8, i16, i32, i64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_unary!(lib, "-", neg_u, i128); reg_unary!(lib, "abs", abs_u, i128); } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index c0bea7ee..5db525c5 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -82,7 +82,6 @@ macro_rules! reg_pad { }; } -#[cfg(not(feature = "no_index"))] def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { reg_op!(lib, "push", push, INT, bool, char, ImmutableString, Array, ()); reg_pad!(lib, "pad", pad, INT, bool, char, ImmutableString, Array, ()); @@ -104,15 +103,12 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { }, ); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_op!(lib, "push", push, i8, u8, i16, u16, i32, i64, u32, u64); reg_pad!(lib, "pad", pad, i8, u8, i16, u16, i32, u32, i64, u64); reg_tri!(lib, "insert", ins, i8, u8, i16, u16, i32, i64, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "push", push, i128, u128); reg_pad!(lib, "pad", pad, i128, u128); reg_tri!(lib, "insert", ins, i128, u128); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index cccf4b6c..8914c6a5 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -73,9 +73,7 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { reg_range::(lib); lib.set_fn_2("range", get_range::); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { macro_rules! reg_range { ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( @@ -87,16 +85,15 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - reg_range!(lib, "range", i128, u128); + if cfg!(not(target_arch = "wasm32")) { + reg_range!(lib, "range", i128, u128); + } } reg_step::(lib); lib.set_fn_3("range", get_step_range::); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { macro_rules! reg_step { ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( @@ -108,7 +105,8 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - reg_step!(lib, "range", i128, u128); + if cfg!(not(target_arch = "wasm32")) { + reg_step!(lib, "range", i128, u128); + } } }); diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 1cb4b437..6c70a38c 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -33,9 +33,7 @@ macro_rules! reg_op { } def_package!(crate:LogicPackage:"Logical operators.", lib, { - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, u32, u64); @@ -43,8 +41,7 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, u32, u64); reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "<", lt, i128, u128); reg_op!(lib, "<=", lte, i128, u128); reg_op!(lib, ">", gt, i128, u128); diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index 9437bc67..4e2e58ad 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -1,25 +1,20 @@ #![cfg(not(feature = "no_object"))] +use crate::any::Dynamic; use crate::def_package; use crate::engine::Map; +use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; -#[cfg(not(feature = "no_index"))] -use crate::{any::Dynamic, module::FuncReturn}; - -#[cfg(not(feature = "no_index"))] use crate::stdlib::vec::Vec; -#[cfg(not(feature = "no_index"))] fn map_get_keys(map: &mut Map) -> FuncReturn> { Ok(map.iter().map(|(k, _)| k.clone().into()).collect()) } -#[cfg(not(feature = "no_index"))] fn map_get_values(map: &mut Map) -> FuncReturn> { Ok(map.iter().map(|(_, v)| v.clone()).collect()) } -#[cfg(not(feature = "no_object"))] def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { lib.set_fn_2_mut( "has", @@ -74,9 +69,11 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { ); // Register map access functions - #[cfg(not(feature = "no_index"))] - lib.set_fn_1_mut("keys", map_get_keys); + if cfg!(not(feature = "no_index")) { + lib.set_fn_1_mut("keys", map_get_keys); + } - #[cfg(not(feature = "no_index"))] - lib.set_fn_1_mut("values", map_get_values); + if cfg!(not(feature = "no_index")) { + lib.set_fn_1_mut("values", map_get_values); + } }); diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 55bb68a3..f8fd2d33 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -5,22 +5,20 @@ use crate::parser::INT; use crate::parser::FLOAT; #[cfg(not(feature = "no_float"))] -#[cfg(not(feature = "unchecked"))] use crate::{result::EvalAltResult, token::Position}; -#[cfg(not(feature = "no_float"))] #[cfg(feature = "no_std")] -use num_traits::*; +#[cfg(not(feature = "no_float"))] +use num_traits::float::Float; #[cfg(not(feature = "no_float"))] -#[cfg(not(feature = "unchecked"))] use crate::stdlib::{boxed::Box, format}; +#[allow(dead_code)] #[cfg(feature = "only_i32")] -#[cfg(not(feature = "unchecked"))] pub const MAX_INT: INT = i32::MAX; +#[allow(dead_code)] #[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "unchecked"))] pub const MAX_INT: INT = i64::MAX; def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { @@ -69,9 +67,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT)); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { lib.set_fn_1("to_float", |x: i8| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: u8| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: i16| Ok(x as FLOAT)); @@ -81,8 +77,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { lib.set_fn_1("to_float", |x: i64| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: u64| Ok(x as FLOAT)); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { lib.set_fn_1("to_float", |x: i128| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: u128| Ok(x as FLOAT)); } @@ -91,28 +86,25 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { lib.set_fn_1("to_int", |ch: char| Ok(ch as INT)); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { lib.set_fn_1("to_int", |x: i8| Ok(x as INT)); lib.set_fn_1("to_int", |x: u8| Ok(x as INT)); lib.set_fn_1("to_int", |x: i16| Ok(x as INT)); lib.set_fn_1("to_int", |x: u16| Ok(x as INT)); } - #[cfg(not(feature = "only_i32"))] - { + if cfg!(not(feature = "only_i32")) { lib.set_fn_1("to_int", |x: i32| Ok(x as INT)); lib.set_fn_1("to_int", |x: u64| Ok(x as INT)); - #[cfg(feature = "only_i64")] - lib.set_fn_1("to_int", |x: u32| Ok(x as INT)); + if cfg!(feature = "only_i64") { + lib.set_fn_1("to_int", |x: u32| Ok(x as INT)); + } } #[cfg(not(feature = "no_float"))] { - #[cfg(not(feature = "unchecked"))] - { + if cfg!(not(feature = "unchecked")) { lib.set_fn_1( "to_int", |x: f32| { @@ -141,8 +133,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { ); } - #[cfg(feature = "unchecked")] - { + if cfg!(feature = "unchecked") { lib.set_fn_1("to_int", |x: f32| Ok(x as INT)); lib.set_fn_1("to_int", |x: f64| Ok(x as INT)); } diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 2e405004..36f4f333 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -48,9 +48,7 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_op!(lib, KEYWORD_PRINT, to_string, i8, u8, i16, u16, i32, u32); reg_op!(lib, FN_TO_STRING, to_string, i8, u8, i16, u16, i32, u32); reg_op!(lib, KEYWORD_DEBUG, to_debug, i8, u8, i16, u16, i32, u32); @@ -58,8 +56,7 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin reg_op!(lib, FN_TO_STRING, to_string, i64, u64); reg_op!(lib, KEYWORD_DEBUG, to_debug, i64, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, KEYWORD_PRINT, to_string, i128, u128); reg_op!(lib, FN_TO_STRING, to_string, i128, u128); reg_op!(lib, KEYWORD_DEBUG, to_debug, i128, u128); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 6ddc2b0c..35dcab00 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -94,14 +94,11 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str reg_op!(lib, "+", prepend, INT, bool, char); lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y)); - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { + if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { reg_op!(lib, "+", append, i8, u8, i16, u16, i32, i64, u32, u64); reg_op!(lib, "+", prepend, i8, u8, i16, u16, i32, i64, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - { + if cfg!(not(target_arch = "wasm32")) { reg_op!(lib, "+", append, i128, u128); reg_op!(lib, "+", prepend, i128, u128); } @@ -228,7 +225,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str "pad", &[TypeId::of::(), TypeId::of::(), TypeId::of::()], |_engine: &Engine, _: &Module, args: &mut [&mut Dynamic]| { - let len = *args[1].downcast_ref::< INT>().unwrap(); + let len = *args[1].downcast_ref::().unwrap(); // Check if string will be over max size limit #[cfg(not(feature = "unchecked"))]