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

Introduce #[rune::function] to capture meta from native fns #462

Merged
merged 1 commit into from
Mar 24, 2023

Conversation

udoprog
Copy link
Collaborator

@udoprog udoprog commented Mar 24, 2023

With this change, #[rune::function] can be used to annotate native functions, which will allow rune contexts to capture their metadata such as documentation strings and name of arguments.

The intention is to capture this in the Context so it can be used to generate documentation. But as a side effect, #[rune::function] can statically determine the kind of function to register so this could potentially reduce the number of methods needed for Module in future versions.

use rune::{Any, Module, ContextError};

/// This is a pretty neat function.
#[rune::function]
fn function(string: &str) -> String {
     string.to_string()
}

/// This is a pretty neat async function.
#[rune::function]
async fn async_function(string: &str) -> String {
     string.to_string()
}

#[derive(Any)]
struct Test;

impl Test {
    /// This is a pretty neat async instance function.
    #[rune::function]
    fn function(&self, string: &str) -> String {
         string.to_string()
    }

    /// This is a pretty neat async instance function.
    #[rune::function]
    async fn async_function(&self, string: &str) -> String {
         string.to_string()
    }
}

fn module() -> Result<Module, ContextError> {
     let mut m = Module::new();
     m.function2(function)?;
     m.function2(async_function)?;
     m.ty::<Test>()?;
     m.function2(Test::function)?;
     m.function2(Test::async_function)?;
     Ok(m)
}

@udoprog udoprog added the enhancement New feature or request label Mar 24, 2023
@udoprog udoprog force-pushed the rune-function branch 2 times, most recently from e107fd9 to d9ebc56 Compare March 24, 2023 14:55
@udoprog udoprog merged commit c9013f9 into main Mar 24, 2023
@udoprog udoprog deleted the rune-function branch March 24, 2023 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant