{"id":13595241,"url":"https://github.com/dtolnay/hackfn","last_synced_at":"2025-04-09T10:33:17.149Z","repository":{"id":45478729,"uuid":"147914216","full_name":"dtolnay/hackfn","owner":"dtolnay","description":"Fake implementation of `std::ops::Fn` for user-defined data types","archived":true,"fork":false,"pushed_at":"2022-12-18T21:26:08.000Z","size":92,"stargazers_count":36,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-07T01:19:26.870Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtolnay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-08T08:03:11.000Z","updated_at":"2024-11-16T07:42:58.000Z","dependencies_parsed_at":"2023-01-29T20:00:46.300Z","dependency_job_id":null,"html_url":"https://github.com/dtolnay/hackfn","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fhackfn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fhackfn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fhackfn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fhackfn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtolnay","download_url":"https://codeload.github.com/dtolnay/hackfn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247817537,"owners_count":21001199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T16:01:46.260Z","updated_at":"2025-04-09T10:33:12.138Z","avatar_url":"https://github.com/dtolnay.png","language":"Rust","readme":"# \\#\\[hackfn\\]\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-dtolnay/hackfn-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\" height=\"20\"\u003e](https://github.com/dtolnay/hackfn)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/hackfn.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"20\"\u003e](https://crates.io/crates/hackfn)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-hackfn-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\" height=\"20\"\u003e](https://docs.rs/hackfn)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/dtolnay/hackfn/ci.yml?branch=master\u0026style=for-the-badge\" height=\"20\"\u003e](https://github.com/dtolnay/hackfn/actions?query=branch%3Amaster)\n\nFake implementation of `std::ops::Fn` for user-defined data types.\n\nPlace a `#[hackfn]` attribute on an impl block containing a single method to use\nthat method as the implementation of the function call operator.\n\n```toml\n[dependencies]\nhackfn = \"0.1\"\n```\n\n*Version requirement: \\#\\[hackfn\\] supports rustc 1.31+*\n\n## Limitations\n\n- The function must receive `\u0026self`. Functions that receive `\u0026mut self` or\n  `self` are not supported.\n\n- The function may not have generic parameters or where-clause.\n\n- The `Self` type must implement `Sized`.\n\n## Examples\n\n```rust\nuse hackfn::hackfn;\n\n/// Function object that adds some number to its input.\nstruct Plus(u32);\n\n#[hackfn]\nimpl Plus {\n    fn call(\u0026self, other: u32) -\u003e u32 {\n        self.0 + other\n    }\n}\n\nfn main() {\n    let plus_one = Plus(1);\n    let sum = plus_one(2);\n    assert_eq!(sum, 3);\n}\n```\n\nThe next example is somewhat more elaborate:\n\n- Interior mutability can be used to approximate a `FnMut` impl.\n\n- Generic parameters and where-clause are permitted on the impl block (though\n  not on the function).\n\n- The function may take any number of arguments.\n\n```rust\nuse hackfn::hackfn;\n\nuse std::cell::Cell;\nuse std::ops::Add;\n\n/// Function object that accumulates a pair of values per call.\n#[derive(Default)]\nstruct AccumulatePairs\u003cT\u003e {\n    first: Cell\u003cT\u003e,\n    second: Cell\u003cT\u003e,\n}\n\n#[hackfn]\nimpl\u003cT\u003e AccumulatePairs\u003cT\u003e where T: Copy + Add\u003cOutput = T\u003e {\n    fn call(\u0026self, first: T, second: T) {\n        self.first.set(self.first.get() + first);\n        self.second.set(self.second.get() + second);\n    }\n}\n\nfn main() {\n    let accumulate = AccumulatePairs::default();\n    accumulate(30, 1);\n    accumulate(20, 2);\n    accumulate(10, 3);\n    assert_eq!(accumulate.first.get(), 60);\n    assert_eq!(accumulate.second.get(), 6);\n}\n```\n\n\u003cbr\u003e\n\n#### License\n\n\u003csup\u003e\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\u003c/sup\u003e\n\n\u003cbr\u003e\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n\u003c/sub\u003e\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fhackfn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtolnay%2Fhackfn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fhackfn/lists"}