1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#![feature(fn_traits, unboxed_closures, const_extern_fn)] //! # Overloadf //! //! ** 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,no_run //! #![feature(fn_traits, unboxed_closures)] //! ``` //! //! 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) //! //! ## Examples: //! simple one: //! ```rust //! #![feature(fn_traits, unboxed_closures)] //! //! use overloadf::*; //! #[overload] //! pub fn xdd(number: i32) -> i32 { //! number * 3 //! } //! //! #[overload] //! pub unsafe fn xdd(number: &u64) -> u64 { //! let n = number as *const u64; //! *n * 4 //! } //! //! assert_eq!(xdd(3_i32), 9_i32); //! let c: &u64 = &6_u64; //! assert_eq!(xdd(c), 24_u64); // unsafe function is not supported. //! ``` //! //! with generic and custom type: //! ```rust //! #![feature(fn_traits, unboxed_closures)] //! //! use overloadf::*; //! use std::ops::MulAssign; //! use std::fmt::Debug; //! #[overload] //! pub fn xdd<T: Copy + Debug + MulAssign<i32>>(mut number: T) -> T { //! println!("number {:?}", number); //! number *= 3_i32; //! number //! } //! //! struct ABC; //! impl Debug for ABC { //! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { //! write!(f, "xdddd") //! } //! } //! //! #[overload] //! pub fn xdd(number: ABC) -> () { //! println!("number {:?}", number); //! } //! let aa = 123; //! assert_eq!(xdd(aa), 369); //! assert_eq!(xdd(ABC), ()); //! ``` //! async / await: //! ```rust //! #![feature(fn_traits, unboxed_closures)] //! use overloadf::*; //! //! #[overload] //! pub async fn xdd(number: i32) -> i32 { //! number + 3 //! } //! #[overload] //! pub async fn xdd(number: i64) -> i64 { //! number + 4 //! } //! assert_eq!(futures::executor::block_on(xdd(3_i32)), 6); //! assert_eq!(futures::executor::block_on(xdd(3_i64)), 7); //! ``` //! //! type conflict might happen if generic overlaps with the definition of implemented types: //! ```rust,compile_fail //! #![feature(fn_traits, unboxed_closures)] //! //! use overloadf::*; //! use std::ops::Mul; //! use std::fmt::Debug; //! #[overload] //! pub fn xdd(number: i32) -> i32 { //! number * 2 //! } //! #[overload] //! pub fn xdd<T: Copy + Debug + Mul<i32>>(number: T) -> T { //! number * 3_i32 //! } //! ``` //! //! for trait methods: //! ```rust //! #![feature(fn_traits, unboxed_closures)] //! use overloadf::*; //! #[overload] //! trait Xdd: Sized { //! fn new(input: i32) -> Self; //! fn new(input :u32) -> Self; //! } //! struct Haha { //! a: u32, //! b: i32, //! } //! #[overload] //! 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(12_i32); //! assert_eq!(haha.a, 1_u32); //! assert_eq!(haha.b, 12_i32); //! let haha = Haha::new(9_u32); //! assert_eq!(haha.a, 9_u32); //! assert_eq!(haha.b, 2_i32); //! ``` //! //! non-trait impl: //! ```rust //! #![feature(fn_traits, unboxed_closures)] //! use overloadf::*; //! #[derive(Debug)] //! pub struct Haha { //! a: u32, //! b: i32, //! } //! #[overload] //! impl Haha { //! pub fn new(b: i32) -> Self { //! Self { //! a: 1, //! b, //! } //! } //! pub fn new(a: u32) -> Self { //! Self { //! a, //! b: 2, //! } //! } //! // will do nothing to functions without overloading //! pub fn normal(&self) -> String { //! format!("{:?}", self) //! } //! } //! let haha = Haha::new(12_i32); //! assert_eq!(haha.a, 1_u32); //! assert_eq!(haha.b, 12_i32); //! let haha = Haha::new(9_u32); //! assert_eq!(haha.a, 9_u32); //! assert_eq!(haha.b, 2_i32); //! assert_eq!(haha.normal(), "Haha { a: 9, b: 2 }"); //! ``` pub extern crate overloadf_derive; pub use overloadf_derive::overload;