Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support freestanding functions in library macro #17

Open
dtolnay opened this issue Sep 10, 2019 · 1 comment
Open

Support freestanding functions in library macro #17

dtolnay opened this issue Sep 10, 2019 · 1 comment

Comments

@dtolnay
Copy link
Owner

dtolnay commented Sep 10, 2019

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);
@8BitMate
Copy link
Contributor

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(/*..*/) {/*..*/}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants