[−][src]Crate overloadable
This crate provides the ability to create overloadable functions in rust through the use of a macro.
Syntax:
use overloadable::overloadable; overloadable!{ #[my_meta] visibility function_name as fn<OptionalTypeArgs>(function_params: function_types) -> optional_return_type where [OptionalTypeArgs: constraints] { code_body } }
What is produced
Here is an example of the output produced by overloadable
:
use overloadable::overloadable; use std::fmt::Debug; overloadable!{ pub my_func as fn(x: usize, y: &str) -> f32 { (x * y.len()) as f32 } fn<T>() where [T: Debug] {} } //Gives #[allow(non_camel_case_types)] pub struct my_func; impl Fn<(usize, &str,)> for my_func { extern "rust-call" fn call(&self, (x, y,): (usize, &str,)) -> f32 { { (x * y.len()) as f32 } } } //The rest of the `Fn*` family impl<T> Fn<()> for my_func where T: Debug { extern "rust-call" fn call(&self, (): ()) -> () { {} } } //The rest of the `Fn*` family.
Limitations
- These functions cannot be exposed for ffi.
- These functions cannot be
const fn
s. - These functions cannot pattern match their arguments.
- These functions cannot be used in place of a
fn()
-style function pointer- But they can be used with the
Fn*
family of traits.
- But they can be used with the
- The
where
clause' contents must be surrounded by square brackets due to a constraint in macros. - Generic lifetime parameters must always be proceeded by a comma, even if they are the only generic parameters.
Macros
overloadable |