Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/george-miao/overloading
https://github.com/george-miao/overloading
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/george-miao/overloading
- Owner: George-Miao
- Created: 2023-01-10T14:35:19.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-10T15:13:52.000Z (almost 2 years ago)
- Last Synced: 2024-10-09T22:01:40.975Z (3 months ago)
- Language: Rust
- Size: 10.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Overloading
[](https://github.com/George-Miao/overloading)
[](https://crates.io/crates/overloading)
[](https://docs.rs/overloading)A POC crate that utilizes `Fn*` traits to implement partial overloading. Caveat: only parameters can be overloaded, not return types.
## TLDR
```rust
#![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);
}
```Expanded code:
```rust
#[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
}
}
```