overfn

Crates.iooverfn
lib.rsoverfn
version0.1.2
sourcesrc
created_at2023-07-23 23:03:19.859071
updated_at2023-07-23 23:09:18.939625
descriptionSimple overloading system for functions with different arguments
homepagehttps://github.com/ferranSanchezLlado/overfn.git
repositoryhttps://github.com/ferranSanchezLlado/overfn.git
max_upload_size
id924063
size26,199
Ferran Sanchez Llado (ferranSanchezLlado)

documentation

https://docs.rs/overfn

README

Overload functions (overfn)

This crate allows you to overload functions with the same name but with different number of arguments through the overload macro. After overloading all the functions, you need to use the macros!() to genarate the macros to invoke the overloaded functions.

Example

use overfn::*;

#[overload]
fn test(item: usize) -> usize {
    item
}

#[overload]
fn test(left: usize, right: usize) -> usize {
    left + right
}

struct Test(usize);

impl Test {
    #[overload(Test)]
    fn new() -> Self {
        Self(0)
    }

    #[overload(Test)]
    fn new(item: usize) -> Self {
        Self(item)
    }

    #[overload(Test)]
    fn test(&self) -> usize {
        self.0
    }

    #[overload(Test)]
    fn test(&self, other: usize) -> usize {
        self.0 + other
    }
}

macros!();

assert_eq!(test!(2), 2);
assert_eq!(test!(2, 2), 4);

let test = Test_new!();
assert_eq!(test.0, 0);

let test = Test_new!(2);
assert_eq!(test.0, 2);

assert_eq!(Test_test!(test), 2);
assert_eq!(Test_test!(test, 2), 4);

Documentation

You can find the documentation here.

Limitations

  • Curretly, you can't overload a function with the same number of arguments with different types.
  • You need to use the macros!() macro to generate the macros to call the overloaded functions.
  • If you overload a class method or instance method, you need to pass the class name in the attribute.

License

This project is licensed under the MIT license or Apache License, Version 2.0 at your option.

Commit count: 3

cargo fmt