Improve writeup.
This commit is contained in:
@@ -66,8 +66,8 @@ let mut engine = Engine::new();
|
||||
engine.register_type::<TestStruct>();
|
||||
```
|
||||
|
||||
Methods on Custom Type
|
||||
---------------------
|
||||
Methods on The Custom Type
|
||||
-------------------------
|
||||
|
||||
To use native custom types, methods and functions in Rhai scripts, simply register them
|
||||
using one of the `Engine::register_XXX` API.
|
||||
|
@@ -11,8 +11,8 @@ see [fallible functions]({{rootUrl}}/rust/fallible.md)).
|
||||
|
||||
```rust
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString};
|
||||
use rhai::RegisterFn; // use 'RegisterFn' trait for 'register_fn'
|
||||
use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn'
|
||||
use rhai::RegisterFn; // use 'RegisterFn' trait for 'register_fn'
|
||||
use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn'
|
||||
|
||||
// Normal function that returns a standard type
|
||||
// Remember to use 'ImmutableString' and not 'String'
|
||||
@@ -26,7 +26,7 @@ fn add_len_str(x: i64, s: &str) -> i64 {
|
||||
|
||||
// Function that returns a 'Dynamic' value - must return a 'Result'
|
||||
fn get_any_value() -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
Ok((42_i64).into()) // standard types can use 'into()'
|
||||
Ok((42_i64).into()) // standard types can use 'into()'
|
||||
}
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
@@ -30,7 +30,7 @@ impl ModuleResolver for MyModuleResolver {
|
||||
&self,
|
||||
engine: &Engine, // reference to the current 'Engine'
|
||||
path: &str, // the module path
|
||||
pos: Position, // location of the 'import' statement
|
||||
pos: Position, // position of the 'import' statement
|
||||
) -> Result<Module, Box<EvalAltResult>> {
|
||||
// Check module path.
|
||||
if is_valid_module_path(path) {
|
||||
|
@@ -12,7 +12,9 @@ fn to_int(num) {
|
||||
print("Ha! Gotcha! " + num);
|
||||
}
|
||||
|
||||
print(to_int(123)); // what happens?
|
||||
let x = (123).to_int();
|
||||
|
||||
print(x); // what happens?
|
||||
```
|
||||
|
||||
A registered native Rust function, in turn, overrides any built-in function of the
|
||||
|
Reference in New Issue
Block a user