pub struct Evaluator {
pub function: Function,
pub environment: HashMap<usize, i32>,
}Expand description
A dice expression evaluator. The evaluator uses a client-supplied pseudo-random number to generate the results of dice rolls.
Underflows and overflows saturate to the minimum and maximum values of the
i32 type, and special arithmetic rules for division, remainder, and
exponentiation ensure that no undefined behavior occurs. In particular:
- Division by zero is treated as zero,
- Zero to the power of zero is treated as one,
- Bases raised to negative exponents are treated as zero.
Fields§
§function: FunctionThe function to evaluate.
environment: HashMap<usize, i32>The environment in which to evaluate the function, as a map from external variable indices to values. Missing bindings default to zero.
Implementations§
Source§impl Evaluator
impl Evaluator
Sourcepub fn bind<'s>(
&mut self,
name: &'s str,
value: i32,
) -> Result<(), EvaluationError<'s>>
pub fn bind<'s>( &mut self, name: &'s str, value: i32, ) -> Result<(), EvaluationError<'s>>
Bind an external variable to a value. The external variable must be declared in the function’s signature.
§Parameters
name: The name of the target external variable.value: The value to bind to the external variable.
§Errors
UnrecognizedExternal if the
alleged external variable is unrecognized.
Sourcepub fn evaluate<R>(
&mut self,
args: impl IntoIterator<Item = i32>,
rng: &mut R,
) -> Result<Evaluation, EvaluationError<'static>>
pub fn evaluate<R>( &mut self, args: impl IntoIterator<Item = i32>, rng: &mut R, ) -> Result<Evaluation, EvaluationError<'static>>
Evaluate the function using the given arguments and pseudo-random number generator (pRNG). Be sure to bind all external variables, and seed the pRNG if desired, before calling this method. Missing external variables default to zero during evaluation, but all parameters must be bound.
§Parameters
args: The arguments to the function.rng: The pseudo-random number generator to use for range and dice rolls.
§Returns
The result of the evaluation.
§Errors
BadArity if the number of arguments
provided disagrees with the number of formal parameters in the function
signature.
Source§impl Evaluator
impl Evaluator
Sourcepub fn bounds(
&self,
args: impl IntoIterator<Item = i32>,
) -> Result<Bounds, EvaluationError<'_>>
pub fn bounds( &self, args: impl IntoIterator<Item = i32>, ) -> Result<Bounds, EvaluationError<'_>>
Compute the bounds of the function for the specified arguments and external variables.
§Parameters
args: The arguments to the function.
§Returns
The bounds of the function, covering both the value and the number of
possible outcomes. The possible outcome count is None if the function
contains dynamic range or roll expressions, i.e., range or roll
expressions whose count or faces are themselves determined by range or
roll expressions. In such cases, the outcome count cannot be determined
without generating a complete histogram of the function, which is too
expensive to do without a specific request.
§Errors
BadArity if the number of arguments
provided disagrees with the number of formal parameters in the function
signature.
Trait Implementations§
impl Eq for Evaluator
impl StructuralPartialEq for Evaluator
Auto Trait Implementations§
impl Freeze for Evaluator
impl RefUnwindSafe for Evaluator
impl Send for Evaluator
impl Sync for Evaluator
impl Unpin for Evaluator
impl UnwindSafe for Evaluator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more