pub struct BuiltinFn {
pub name: &'static str,
/* private fields */
}Expand description
A native Rust function that can be called from Swan code.
§Why a newtype instead of a bare Rc<dyn Fn>?
- Debug:
dyn Fnis notDebug; storing anamegives meaningful output in error messages and test output. - Metadata: the
namecan be used when buildingUndefinederror values (“functionmath.sincalled on a Bool”). - Ergonomics:
BuiltinFn::new("name", |arg| ...)is self-documenting.
§Argument convention
The function receives the evaluated right-hand side as a single Value.
Multi-argument calls arrive as Value::Tuple. The function is responsible
for destructuring the tuple when it expects more than one argument.
§Return value
The function must never panic. Return a Value::Undefined (for example
with Value::undef_unary) for any input that is invalid.
§Examples
use swan_core::value::{BuiltinFn, Value};
let negate = BuiltinFn::new("negate", |arg| match arg {
Value::Numb(n) => Value::Numb(-n),
other => Value::undef_unary("negate", other, 0),
});
assert_eq!(negate.call(Value::Numb(2.0)), Value::Numb(-2.0));Fields§
§name: &'static strHuman-readable name, used in Debug output and error messages.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for BuiltinFn
impl !Send for BuiltinFn
impl !Sync for BuiltinFn
impl !UnwindSafe for BuiltinFn
impl Freeze for BuiltinFn
impl Unpin for BuiltinFn
impl UnsafeUnpin for BuiltinFn
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
Mutably borrows from an owned value. Read more