function_group

Crates.iofunction_group
lib.rsfunction_group
version0.2.4
sourcesrc
created_at2019-04-02 01:46:36.383878
updated_at2019-04-09 00:54:06.173952
descriptionA macro to hack in function overloading to rust
homepage
repositoryhttps://github.com/Jon-Davis/function_group
max_upload_size
id125247
size17,828
Jonathon Davis (Jon-Davis)

documentation

README

Crates.io Documentation

Function_Group

Function Group is a Function Overloading macro for the rust programing language. The macro allows you to define multiple functions that take a variable number of arguments! Actually the functions still only take one argument, but they accept multiple types of tuples.

Function groups can take multiple types of arguments and even be recursive.

function_group! {
    fn add -> usize {
        (one : usize, two : usize) {
            one + two
        }
        (one : usize, two : usize, three: usize) {
            add((one, two)) + three
        }
    }
}

assert!(add((5, 5)) == 10);
assert!(add((5, 5, 5)) == 15);

The arguments can be mutable or immutable refrences.

function_group! {
    fn add_to {
        (one : &mut usize, two : usize) {
            *one += two;
        }
        (one : &mut usize, two : usize, three : usize) {
            *one += two + three;
        }
    }
}

let mut x = 10;
add_to((&mut x, 5));
add_to((&mut x, 5, 5));
assert!(x == 25);

Function Groups can even be associated with a Type. In the example below, each sub function will be passed a mutable refrence to self, and these functions will be usable by the TestStruct type.

struct TestStruct(usize);
function_group! {
    fn add_to_struct(&mut self : TestStruct) {
        (one : usize) {
            self.0 += one;
        }
        (one : usize, two : usize){
            self.0 += one + two; 
        }
    }
}

let mut x = TestStruct(10);
x.add_to_struct((1,2));
assert!(x.0 == 13);

Possible future features

  1. Generics should be feasble on a per sub-function basis
  2. having a function group in a trait is unlikely
Commit count: 30

cargo fmt