Trying to understand the safety of using references in Rhai #961
-
Hi, I have the following Rust fn I'm registering in Rhai. pub fn open(
dir_state: Rc<DirState>,
file_name: &str,
) -> Result<Rc<FileState>, Box<EvalAltResult>> {
match open_helper(&dir_state, file_name) {
Ok(file_state) => Ok(file_state),
Err(e) => Err(format!("{e:#}").into()),
}
}
let mut engine: Engine = Engine::new();
engine.register_fn("open", open); And an implementation in another crate pub fn open_helper(dir_state: &Rc<DirState>, file_name: &str) -> Result<Rc<FileState>> {
....
}
New fn sig: pub fn open(
dir_state: &Rc<DirState>,
file_name: &str,
) -> Result<Rc<FileState>, Box<EvalAltResult>> { And the Rhai script: let dir = open_dir(temp_dir_path);
let file = open_file(&dir, "foo.txt"); Gives this error:
Is that functionality not included intentionally? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
dir_state: &Rc<DirState>, Rhai does not support references as parameters. All parameters must be https://rhai.rs/book/rust/methods.html#admonition-no-support-for-references
This is absolutely OK. The Rust compiler does not stop you, so it is OK. Your function parameters are all Rhai doesn't panic by itself (other than documented) so you can do anything you want with it, as long as it is not |
Beta Was this translation helpful? Give feedback.
Rhai does not support references as parameters. All parameters must be
Clone
and passed in. Except for the first parameter, which may be&mut
.https://rhai.rs/book/rust/methods.html#admonition-no-support-for-references
This is absolutely OK. The Rust compiler does not stop you, so it is OK. Your function parameters are all
Clone
and passed in, inside you can do whatever you want and no references can ever be passed outside of the function.Rhai doesn't panic by itself (other than documented) so you can do anything you want with it, as long as it is not
unsafe
and…