* Let function overloading possible in rust *
With a single attribute on top of the function, you can overload the function with different parameters. Current implementation still has some flaws and todo items, so use at your own risk.
This library is based on some unstable features. To use this library, please put the following lines in crate root and the beginning of test files: ```rust
```
There are some features that cannot be achieved until now: - unsafe function overloading - const function overloading - different privacy setting on function overloading (will pickup the privacy setting in first function and apply to all) - function overloading inside traits (for limited cases)
simple one: ```rust
use overloadf::*;
pub fn xdd() -> i32 { 5_i32 }
pub fn xdd(number: i32) -> i32 { number * 3 }
pub fn xdd(number: u8) { println!("{}", number); }
pub unsafe fn xdd(number: &u64) -> u64 { let n = number as *const u64; *n * 4 }
asserteq!(xdd(3i32), 9i32); let c: &u64 = &6u64; asserteq!(xdd(c), 24u64); // unsafe function is not supported. asserteq!(xdd(), 5i32); ```
with generic and custom type: ```rust
use overloadf::*; use std::ops::MulAssign; use std::fmt::Debug;
pub fn xdd
struct ABC; impl Debug for ABC { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "xdddd") } }
pub fn xdd(number: ABC) -> () {
println!("number {:?}", number);
}
let aa = 123;
asserteq!(xdd(aa), 369);
asserteq!(xdd(ABC), ());
async / await:
rust
use overloadf::*;
pub async fn xdd(number: i32) -> i32 { number + 3 }
pub async fn xdd(number: i64) -> i64 { number + 4 } asserteq!(futures::executor::blockon(xdd(3i32)), 6); asserteq!(futures::executor::blockon(xdd(3i64)), 7); ```
type conflict might happen if generic overlaps with the definition of implemented types: ```rust,compile_fail
use overloadf::*; use std::ops::Mul; use std::fmt::Debug;
pub fn xdd(number: i32) -> i32 { number * 2 }
pub fn xdd
for trait methods (notice that trait for overload must inherit Sized): ```rust
use overloadf::*;
trait Xdd: Sized { fn new(input: i32) -> Self; fn new(input :u32) -> Self; } struct Haha { a: u32, b: i32, }
impl Xdd for Haha { fn new(b: i32) -> Self { Self { a: 1, b, } } fn new(a: u32) -> Self { Self { a, b: 2, } } } let haha = Haha::new(12i32); asserteq!(haha.a, 1u32); asserteq!(haha.b, 12i32); let haha = Haha::new(9u32); asserteq!(haha.a, 9u32); asserteq!(haha.b, 2i32); ```
dynamic trait object implementation: ```rust
use overloadf::*; trait Xdd: 'static {}
impl dyn Xdd { fn abc(&self, c: i32) -> i32 { c } fn abc(&self, c: u32) -> u32 { c } }
impl
trait with generics: ```rust
use overloadf::*;
trait Xdd
struct Haha { a: u32, b: i32, }
impl Xdd
non-trait impl: ```rust
use overloadf::*;
pub struct Haha { a: u32, b: i32, }
impl Haha { pub fn new(b: i32) -> Self { Self { a: 1, b, } } pub fn new(a: u32) -> Self { Self { a, b: 2, } } // self in function parameter will be converted to associated type // that means no more syntax sugar like haha.normal() pub fn normal(&self) -> String { format!("{:?}", self) } pub fn normal(&self, prefix: &str) -> String { format!("{} {:?}", prefix, self) } // function without overloading is not influenced pub fn display(&self) -> String { format!("{:?}", self) } } let haha = Haha::new(12i32); asserteq!(haha.a, 1u32); asserteq!(haha.b, 12i32); let haha = Haha::new(9u32); asserteq!(haha.a, 9u32); asserteq!(haha.b, 2i32); asserteq!(Haha::normal(&haha), "Haha { a: 9, b: 2 }"); asserteq!(Haha::normal(&haha, "abc"), "abc Haha { a: 9, b: 2 }"); assert_eq!(haha.display(), "Haha { a: 9, b: 2 }"); ```
default attribute:
in functions(not yet for traits), now you could decorate parameters with default values.
the reason why we don't support parameter = value
syntax directly is that in derived
TokenStream
, values inside will go through compiler first for syntax check.
The default value syntax will be treated as a compile error and forbid us from parsing
and generating valid Tokens.
example: ```rust
use overloadf::*;
fn xdd(#[default(= 5i32)] a: i32, #[default(= 32u64)] b: u64) -> u64 { b - (a as u64) } asserteq!(xdd(3i32), 29u64); asserteq!(xdd(4i32, 4u64), 0); asserteq!(xdd(), 27u64); // compile error: expect i32 found u64 // asserteq!(xdd(6u64), 1_u64); ```
```rust
use overloadf::*;
fn xdd(#[default(= 5i32)] a: i32, b: u8, #[default(= 32u64)] c: u64) -> u64 { c + (b as u64) - (a as u64) } asserteq!(xdd(4i32, 7u8), 35u64); asserteq!(xdd(3u8), 30_u64); ```
Licensed under
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.