diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 6ece57c61..028dc252c 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -31,61 +31,6 @@ //! # Ok(()) //! # } //! ``` -//! -//! # Register a Rust Function with a Rhai `Module` -//! -//! ``` -//! use rhai::{EvalAltResult, FLOAT, Module}; -//! use rhai::plugin::*; -//! use rhai::module_resolvers::*; -//! -//! #[export_fn] -//! fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT { -//! ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt() -//! } -//! -//! # fn main() -> Result<(), Box> { -//! let mut engine = Engine::new(); -//! engine.register_fn("get_mystic_number", || 42.0 as FLOAT); -//! let mut m = Module::new(); -//! set_exported_fn!(m, "euclidean_distance", distance_function); -//! let mut r = StaticModuleResolver::new(); -//! r.insert("Math::Advanced", m); -//! engine.set_module_resolver(r); -//! -//! assert_eq!(engine.eval::( -//! r#" -//! import "Math::Advanced" as math; -//! math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()) -//! "#)?, 41.0); -//! # Ok(()) -//! # } -//! ``` -//! -//! # Register a Plugin Function with an `Engine` -//! -//! ``` -//! use rhai::{EvalAltResult, FLOAT, Module}; -//! use rhai::plugin::*; -//! use rhai::module_resolvers::*; -//! -//! #[export_fn] -//! pub fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT { -//! ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt() -//! } -//! -//! # fn main() -> Result<(), Box> { -//! let mut engine = Engine::new(); -//! engine.register_fn("get_mystic_number", || { 42 as FLOAT }); -//! register_exported_fn!(engine, "euclidean_distance", distance_function); -//! -//! assert_eq!(engine.eval::( -//! "euclidean_distance(0.0, 1.0, 0.0, get_mystic_number())" -//! )?, 41.0); -//! # Ok(()) -//! # } -//! ``` -//! use quote::quote; use syn::{parse_macro_input, spanned::Spanned, DeriveInput}; @@ -100,61 +45,6 @@ mod rhai_module; #[cfg(test)] mod test; -/// Attribute, when put on a Rust function, turns it into a _plugin function_. -/// -/// # Usage -/// -/// ``` -/// # use rhai::{Engine, EvalAltResult}; -/// use rhai::plugin::*; -/// -/// #[export_fn] -/// fn my_plugin_function(x: i64) -> i64 { -/// x * 2 -/// } -/// -/// # fn main() -> Result<(), Box> { -/// let mut engine = Engine::new(); -/// -/// register_exported_fn!(engine, "func", my_plugin_function); -/// -/// assert_eq!(engine.eval::("func(21)")?, 42); -/// # Ok(()) -/// # } -/// ``` -#[proc_macro_attribute] -pub fn export_fn( - args: proc_macro::TokenStream, - input: proc_macro::TokenStream, -) -> proc_macro::TokenStream { - let mut output = quote! { - #[allow(clippy::needless_pass_by_value)] - }; - output.extend(proc_macro2::TokenStream::from(input.clone())); - - let parsed_params = match crate::attrs::outer_item_attributes(args.into(), "export_fn") { - Ok(args) => args, - Err(err) => return err.to_compile_error().into(), - }; - let mut function_def = parse_macro_input!(input as function::ExportedFn); - - if !function_def.cfg_attrs().is_empty() { - return syn::Error::new( - function_def.cfg_attrs()[0].span(), - "`cfg` attributes are not allowed for `export_fn`", - ) - .to_compile_error() - .into(); - } - - if let Err(e) = function_def.set_params(parsed_params) { - return e.to_compile_error().into(); - } - - output.extend(function_def.generate()); - proc_macro::TokenStream::from(output) -} - /// Attribute, when put on a Rust module, turns it into a _plugin module_. /// /// # Usage @@ -279,6 +169,47 @@ pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro } } +/// Attribute, when put on a Rust function, turns it into a _plugin function_. +/// +/// # Deprecated +/// +/// This macro is deprecated as it performs no additional value. +/// +/// This method will be removed in the next major version. +#[deprecated(since = "1.18.0")] +#[proc_macro_attribute] +pub fn export_fn( + args: proc_macro::TokenStream, + input: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + let mut output = quote! { + #[allow(clippy::needless_pass_by_value)] + }; + output.extend(proc_macro2::TokenStream::from(input.clone())); + + let parsed_params = match crate::attrs::outer_item_attributes(args.into(), "export_fn") { + Ok(args) => args, + Err(err) => return err.to_compile_error().into(), + }; + let mut function_def = parse_macro_input!(input as function::ExportedFn); + + if !function_def.cfg_attrs().is_empty() { + return syn::Error::new( + function_def.cfg_attrs()[0].span(), + "`cfg` attributes are not allowed for `export_fn`", + ) + .to_compile_error() + .into(); + } + + if let Err(e) = function_def.set_params(parsed_params) { + return e.to_compile_error().into(); + } + + output.extend(function_def.generate()); + proc_macro::TokenStream::from(output) +} + /// Macro to register a _plugin function_ (defined via [`#[export_fn]`][macro@export_fn]) into an `Engine`. /// /// # Deprecated