Support freestanding functions in library macro
dtolnay opened this issue · comments
The reflect::library! macro currently only accepts functions as trait functions or associated functions. We'll also need to work with free functions like str::from_utf8 that are not part of a trait or impl block.
reflect::library! {
extern crate std {
mod str {
fn from_utf8(&[u8]) -> Result<&str, Utf8Error>
}
}
}
let result = RUNTIME::std::str::from_utf8.INVOKE(bytes);
Along similar lines it would be useful to be able to create new freestanding functions as well. This could be done by declaring the function upfront in the reflect::library, and calling make_function like this:
reflect::library! {
fn my_new_function(&str)
}
ex.make_function(RUNTIME::my_new_function, my_new_function_impl)
But it would probably be more convenient to just have a make_new_function
method. I'm not exactly sure how it would look like, but maybe something along these lines:
let new_function = ex.make_new_function("new_function_name");
new_function.add_param(Type::primitive_str().reference());
new_function.add_param(Type::from_str("Struct").unwrap());
ex.make_function(new_function, new_function_impl)
fn new_function_impl(/*..*/) {/*..*/}