[−][src]Function glsp::rfn
pub fn rfn<ArgsWithTag, Ret, F>(f: F) -> Root<RFn>ⓘ where
Wrapper<ArgsWithTag, Ret, F>: WrappedCall,
Wrapper<ArgsWithTag, Ret, F>: 'static,
Creates a GameLisp value which represents a Rust function.
GameLisp will perform automatic conversions for the function's parameters and return value. In particular:
-
The return value may be any type which implements
IntoVal
. -
Parameters may be any type which implements
FromVal
. -
In addition, parameters may be...
-
References to GameLisp primitive types, like
&Arr
and&GFn
. -
An
Option<T>
. These arguments will be set toNone
when the argument list is too short to have a value at that position, or when the caller passes in#n
for that argument. -
References to sized types,
&T
and&mut T
, require the corresponding argument to be anrdata
. Therdata
's payload will be borrowed for the duration of the function call. If you need finer control over the borrow, you could accept aRoot<RData>
or anRRoot<T>
instead.- As a special exception, when
T
implementsRGlobal
, arguments of type&T
and&mut T
will not consume any values from the argument list. Instead, the references will be borrowed from the currentRuntime
's global storage, for the duration of the function call, by callingT::borrow()
orT::borrow_mut()
.
- As a special exception, when
-
References to unsized types will be constructed on the stack and then borrowed.
&[T]
is converted from an array.&str
,&OsStr
,&CStr
and&Path
are converted from strings. -
The special type
Rest
can be used to define a variadic function by capturing any number of arguments.
-
The f
parameter can be a closure, but if so, it must be 'static
. In practice, this
means that if the closure captures any data, you must use the move
keyword to take
ownership.
Due to a rustc bug, the f
parameter must
be passed as a reference or a Box
; it can't be directly passed by value.
fn example(i: i32) -> i32 { i } glsp::rfn(example); //type inference error glsp::rfn(&example); //success glsp::rfn(|i: i32| i); //type inference error glsp::rfn(&|i: i32| i); //success let capture = arr![1, 2, 3]; glsp::rfn(move || capture.shallow_clone()); //type inference error glsp::rfn(Box::new(move || capture.shallow_clone())); //success
When binding a Rust function to a global variable, it's usually more convenient to call
glsp::bind_rfn
.