Module fns

Source
Expand description

Standard functions for the interpreter, and the tools to define new native functions.

§Defining native functions

There are several ways to define new native functions:

  • Implement NativeFn manually. This is the most versatile approach, but it can be overly verbose.
  • Use FnWrapper or the wrap function. This allows specifying arguments / output with custom types (such as bool or a Number), but does not work for non-'static types.
  • Use wrap_fn or wrap_fn_with_context macros. These macros support the same eloquent interface as wrap, and also do not have 'static requirement for args. As a downside, debugging compile-time errors when using macros can be rather painful.

§Why multiple ways to do the same thing?

In the ideal world, FnWrapper would be used for all cases, since it does not involve macro magic. Unfortunately, stable Rust currently does not provide means to describe lifetime restrictions on args / return type of wrapped functions in the general case (this requires generic associated types). As such, the (implicit) 'static requirement is a temporary measure, and macros fill the gaps in their usual clunky manner.

Structs§

Array
Function generating an array by mapping its indexes.
Assert
Assertion function.
AssertEq
Equality assertion function.
Dbg
Acts similarly to the dbg! macro, outputting the argument(s) to stderr and returning them. If a single argument is provided, it’s returned as-is; otherwise, the arguments are wrapped into a tuple.
Filter
Filter function that evaluates the provided function on each item of the tuple and retains only elements for which the function returned true.
FnWrapper
Wrapper of a function containing information about its arguments.
Fold
Reduce (aka fold) function that reduces the provided tuple to a single value.
FromValueError
Error raised when a value cannot be converted to the expected type when using FnWrapper.
If
if function that eagerly evaluates “if” / “else” terms.
Len
Function returning array / object length.
Loop
Loop function that evaluates the provided closure one or more times.
Map
Map function that evaluates the provided function on each item of the tuple.
Merge
Function that merges two tuples.
Push
Function that appends a value onto a tuple.
While
Loop function that evaluates the provided closure while a certain condition is true. Returns the loop state afterwards.

Enums§

Compare
Comparator functions on two primitive arguments. All functions use Arithmetic to determine ordering between the args.
ErrorOutput
Generic error output encompassing all error types supported by wrapped functions.
FromValueErrorKind
Error kinds for FromValueError.
FromValueErrorLocation
Element of the FromValueError location.

Traits§

IntoEvalResult
Converts type into Value or an error. This is used to convert the return type of wrapped functions to the result expected by NativeFn.
TryFromValue
Fallible conversion from Value to a function argument.

Functions§

wrap
Wraps a function enriching it with the information about its arguments. This is a slightly shorter way to create wrappers compared to calling FnWrapper::new().

Type Aliases§

Binary
Binary function wrapper.
Quaternary
Quaternary function wrapper.
Ternary
Ternary function wrapper.
Unary
Unary function wrapper.