3个版本
0.1.2 | 2023年1月10日 |
---|---|
0.1.1 | 2023年1月10日 |
0.1.0 | 2023年1月10日 |
#903在 过程宏
7KB
109 代码行
Overloading
一个使用Fn*
特质实现部分重载的POCcrate。注意:只能重载参数,不能重载返回类型。
TLDR
#![feature(unboxed_closures)]
#![feature(fn_traits)]
use overloading::overloading;
#[overloading]
fn overloaded(abc: String) -> i32 {
abc.parse().unwrap()
}
#[overloading(overloaded)]
fn overloaded() -> i32 {
114514
}
#[test]
fn test() {
let res = overloaded("123".to_owned());
assert_eq!(res, 123);
let res = overloaded();
assert_eq!(res, 114514);
}
展开代码
#[allow(non_camel_case_types)]
struct overloaded;
impl std::ops::FnOnce<(String,)> for overloaded {
type Output = i32;
extern "rust-call" fn call_once(self, (abc,): (String,)) -> Self::Output {
abc.parse().unwrap()
}
}
impl std::ops::FnMut<(String,)> for overloaded {
extern "rust-call" fn call_mut(&mut self, (abc,): (String,)) -> Self::Output {
abc.parse().unwrap()
}
}
impl std::ops::Fn<(String,)> for overloaded {
extern "rust-call" fn call(&self, (abc,): (String,)) -> Self::Output {
abc.parse().unwrap()
}
}
impl std::ops::FnOnce<()> for overloaded {
type Output = i32;
extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
114514
}
}
impl std::ops::FnMut<()> for overloaded {
extern "rust-call" fn call_mut(&mut self, _: ()) -> Self::Output {
114514
}
}
impl std::ops::Fn<()> for overloaded {
extern "rust-call" fn call(&self, _: ()) -> Self::Output {
114514
}
}
依赖
~1.5MB
~36K SLoC