Use NonZero for limits.

This commit is contained in:
Stephen Chung
2021-01-06 13:46:53 +08:00
parent 3fbcefe0ed
commit a5d6392107
4 changed files with 119 additions and 112 deletions

View File

@@ -1,6 +1,10 @@
//! Configuration settings for [`Engine`].
use crate::stdlib::{format, num::NonZeroU8, string::String};
use crate::stdlib::{
format,
num::{NonZeroU64, NonZeroU8, NonZeroUsize},
string::String,
};
use crate::token::Token;
use crate::Engine;
@@ -62,11 +66,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn set_max_operations(&mut self, operations: u64) -> &mut Self {
self.limits.max_operations = if operations == u64::MAX {
0
} else {
operations
};
self.limits.max_operations = NonZeroU64::new(operations);
self
}
/// The maximum number of operations allowed for a script to run (0 for unlimited).
@@ -75,7 +75,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_operations(&self) -> u64 {
self.limits.max_operations
self.limits.max_operations.map_or(0, NonZeroU64::get)
}
/// Set the maximum number of imported [modules][crate::Module] allowed for a script.
///
@@ -106,18 +106,10 @@ impl Engine {
max_expr_depth: usize,
#[cfg(not(feature = "no_function"))] max_function_expr_depth: usize,
) -> &mut Self {
self.limits.max_expr_depth = if max_expr_depth == usize::MAX {
0
} else {
max_expr_depth
};
self.limits.max_expr_depth = NonZeroUsize::new(max_expr_depth);
#[cfg(not(feature = "no_function"))]
{
self.limits.max_function_expr_depth = if max_function_expr_depth == usize::MAX {
0
} else {
max_function_expr_depth
};
self.limits.max_function_expr_depth = NonZeroUsize::new(max_function_expr_depth);
}
self
}
@@ -127,7 +119,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_expr_depth(&self) -> usize {
self.limits.max_expr_depth
self.limits.max_expr_depth.map_or(0, NonZeroUsize::get)
}
/// The depth limit for expressions in functions (0 for unlimited).
///
@@ -136,7 +128,9 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn max_function_expr_depth(&self) -> usize {
self.limits.max_function_expr_depth
self.limits
.max_function_expr_depth
.map_or(0, NonZeroUsize::get)
}
/// Set the maximum length of [strings][crate::ImmutableString] (0 for unlimited).
///
@@ -144,7 +138,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn set_max_string_size(&mut self, max_size: usize) -> &mut Self {
self.limits.max_string_size = if max_size == usize::MAX { 0 } else { max_size };
self.limits.max_string_size = NonZeroUsize::new(max_size);
self
}
/// The maximum length of [strings][crate::ImmutableString] (0 for unlimited).
@@ -153,7 +147,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_string_size(&self) -> usize {
self.limits.max_string_size
self.limits.max_string_size.map_or(0, NonZeroUsize::get)
}
/// Set the maximum length of [arrays][crate::Array] (0 for unlimited).
///
@@ -162,7 +156,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
#[inline(always)]
pub fn set_max_array_size(&mut self, max_size: usize) -> &mut Self {
self.limits.max_array_size = if max_size == usize::MAX { 0 } else { max_size };
self.limits.max_array_size = NonZeroUsize::new(max_size);
self
}
/// The maximum length of [arrays][crate::Array] (0 for unlimited).
@@ -172,7 +166,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
#[inline(always)]
pub fn max_array_size(&self) -> usize {
self.limits.max_array_size
self.limits.max_array_size.map_or(0, NonZeroUsize::get)
}
/// Set the maximum size of [object maps][crate::Map] (0 for unlimited).
///
@@ -181,7 +175,7 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn set_max_map_size(&mut self, max_size: usize) -> &mut Self {
self.limits.max_map_size = if max_size == usize::MAX { 0 } else { max_size };
self.limits.max_map_size = NonZeroUsize::new(max_size);
self
}
/// The maximum size of [object maps][crate::Map] (0 for unlimited).
@@ -191,7 +185,7 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn max_map_size(&self) -> usize {
self.limits.max_map_size
self.limits.max_map_size.map_or(0, NonZeroUsize::get)
}
/// Set the module resolution service used by the [`Engine`].
///