{"id":16154248,"url":"https://github.com/thomaseizinger/rust-impl-template","last_synced_at":"2025-04-06T23:44:24.415Z","repository":{"id":62440801,"uuid":"239242359","full_name":"thomaseizinger/rust-impl-template","owner":"thomaseizinger","description":null,"archived":false,"fork":false,"pushed_at":"2020-02-09T03:50:38.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T13:19:42.600Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomaseizinger.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":"2020-02-09T03:48:05.000Z","updated_at":"2020-05-05T07:05:25.000Z","dependencies_parsed_at":"2022-11-01T22:01:00.934Z","dependency_job_id":null,"html_url":"https://github.com/thomaseizinger/rust-impl-template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaseizinger%2Frust-impl-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaseizinger%2Frust-impl-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaseizinger%2Frust-impl-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaseizinger%2Frust-impl-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomaseizinger","download_url":"https://codeload.github.com/thomaseizinger/rust-impl-template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247569130,"owners_count":20959758,"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-10-10T01:15:48.424Z","updated_at":"2025-04-06T23:44:24.399Z","avatar_url":"https://github.com/thomaseizinger.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# impl-template\n\n`impl-template` is a procedural macro for the Rust programming language that allows you to define templates for \"impl\"-items and have them expanded to several instances depending on the configuration.\n\nIn a way, you can think of it as compile-time blanket impls if you know all types you want to implement it for upfront.\n\n## Usage\n\n```rust\ntrait Foo {}\n\nstruct Bar;\nstruct Baz;\n\n#[impl_template]\nimpl Foo for ((Bar, Baz)) {\n\n}\n```\n\nThis will generate the following code:\n\n```rust\nimpl Foo for Bar {\n\n}\n\nimpl Foo for Baz {\n\n}\n```\n\n## Advanced usage\n\n\n`impl-template` looks for patterns of double tuples.\nThose are syntactically valid Rust code but AFAIK fairly useless and should thus not appear in day-to-day Rust-code.\n\n### Several double tuple patterns\n\nYou can have as many of those double-tuples as you want.\n`impl-template` will create a cartesian product out of all of them and generate the impl-blocks accordingly.\n\n```rust\ntrait GenericFoo\u003cT, S\u003e { }\n\nstruct Bar;\nstruct Baz;\n\nstruct One;\nstruct Two;\nstruct Three;\n\nstruct Alpha;\nstruct Beta;\n\n#[impl_template]\nimpl GenericFoo\u003c((Bar, Baz)), ((Alpha, Beta))\u003e for ((One, Two, Three)) { }\n```\nThe above snippet will expand to 12 impl blocks (2 * 3 * 2).\n\n### Referring to types\n\n`impl-template` allows you to refer to types within the template block.\nIt generates a dummy identifier for every double-tuple in the scheme of `__TYPE{}__` with `{}` being replaced with a 0-based index.\n\nWe can extend `GenericFoo` with a method where we have to name the type parameters:\n\n```rust\ntrait GenericFoo\u003cT, S\u003e {\n    fn my_fn(arg1: T, arg2: S) -\u003e Self;\n}\n\nstruct Bar;\nstruct Baz;\n\nstruct One;\nstruct Two;\nstruct Three;\n\nstruct Alpha;\nstruct Beta;\n\n#[impl_template]\nimpl GenericFoo\u003c((Bar, Baz)), ((Alpha, Beta))\u003e for ((One, Two, Three)) {\n    fn my_fn(_arg1: __TYPE0__, _arg2: __TYPE1__) -\u003e __TYPE2__ {\n        unimplemented!()\n    }\n}\n```\n\nThe above code expands to the following (non-exhaustive list):\n\n```rust\nimpl GenericFoo\u003cBar, Alpha\u003e for One {\n    fn my_fn(_arg1: Bar, _arg2: Alpha) -\u003e One {\n        unimplemented!()\n    }\n}\n\nimpl GenericFoo\u003cBar, Beta\u003e for One {\n    fn my_fn(_arg1: Bar, _arg2: Beta) -\u003e One {\n        unimplemented!()\n    }\n}\n```\n\nIn other words, `__TYPE0__` is an iterator-like placeholder for the first double-tuple `((Bar, Baz))`, `__TYPE1__` for the second one, etc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaseizinger%2Frust-impl-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomaseizinger%2Frust-impl-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaseizinger%2Frust-impl-template/lists"}