Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dtolnay/hackfn
Fake implementation of `std::ops::Fn` for user-defined data types
https://github.com/dtolnay/hackfn
Last synced: 7 days ago
JSON representation
Fake implementation of `std::ops::Fn` for user-defined data types
- Host: GitHub
- URL: https://github.com/dtolnay/hackfn
- Owner: dtolnay
- License: apache-2.0
- Archived: true
- Created: 2018-09-08T08:03:11.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-18T21:26:08.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T11:35:39.813Z (13 days ago)
- Language: Rust
- Homepage:
- Size: 89.8 KB
- Stars: 37
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# \#\[hackfn\]
[](https://github.com/dtolnay/hackfn)
[](https://crates.io/crates/hackfn)
[](https://docs.rs/hackfn)
[](https://github.com/dtolnay/hackfn/actions?query=branch%3Amaster)Fake implementation of `std::ops::Fn` for user-defined data types.
Place a `#[hackfn]` attribute on an impl block containing a single method to use
that method as the implementation of the function call operator.```toml
[dependencies]
hackfn = "0.1"
```*Version requirement: \#\[hackfn\] supports rustc 1.31+*
## Limitations
- The function must receive `&self`. Functions that receive `&mut self` or
`self` are not supported.- The function may not have generic parameters or where-clause.
- The `Self` type must implement `Sized`.
## Examples
```rust
use hackfn::hackfn;/// Function object that adds some number to its input.
struct Plus(u32);#[hackfn]
impl Plus {
fn call(&self, other: u32) -> u32 {
self.0 + other
}
}fn main() {
let plus_one = Plus(1);
let sum = plus_one(2);
assert_eq!(sum, 3);
}
```The next example is somewhat more elaborate:
- Interior mutability can be used to approximate a `FnMut` impl.
- Generic parameters and where-clause are permitted on the impl block (though
not on the function).- The function may take any number of arguments.
```rust
use hackfn::hackfn;use std::cell::Cell;
use std::ops::Add;/// Function object that accumulates a pair of values per call.
#[derive(Default)]
struct AccumulatePairs {
first: Cell,
second: Cell,
}#[hackfn]
impl AccumulatePairs where T: Copy + Add {
fn call(&self, first: T, second: T) {
self.first.set(self.first.get() + first);
self.second.set(self.second.get() + second);
}
}fn main() {
let accumulate = AccumulatePairs::default();
accumulate(30, 1);
accumulate(20, 2);
accumulate(10, 3);
assert_eq!(accumulate.first.get(), 60);
assert_eq!(accumulate.second.get(), 6);
}
```
#### License
Licensed under either of Apache License, Version
2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.