{"id":16916153,"url":"https://github.com/zesterer/mutation","last_synced_at":"2025-07-07T08:35:21.143Z","repository":{"id":52811314,"uuid":"322374364","full_name":"zesterer/mutation","owner":"zesterer","description":"Unleash the power of nightly Rust to write code that's generic over mutation!","archived":false,"fork":false,"pushed_at":"2020-12-18T15:28:44.000Z","size":7,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T16:23:40.163Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zesterer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-17T18:05:58.000Z","updated_at":"2024-11-06T22:06:35.000Z","dependencies_parsed_at":"2022-08-22T21:11:00.791Z","dependency_job_id":null,"html_url":"https://github.com/zesterer/mutation","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/zesterer%2Fmutation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Fmutation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Fmutation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Fmutation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zesterer","download_url":"https://codeload.github.com/zesterer/mutation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248438687,"owners_count":21103455,"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-13T19:25:15.405Z","updated_at":"2025-04-11T16:23:46.541Z","avatar_url":"https://github.com/zesterer.png","language":"Rust","readme":"# Mutation\n\nUse the power of nightly Rust to write code that's generic over mutation!\n\n# Features\n\n- Zero-cost (at runtime)!\n\n- Highly likely to trigger Internal Compiler Errors (ICE)!\n\n- A horrific type astronomy hack!\n\n- Would work even better if Rust had proper higher-kinded types instead of just GATs!\n\n- Kind of works with iterators\n\n**Highly Experimental**\n\nAre you frequently frustrated by the need to write near-identical functions that\ndiffer only by the occurrence of the `mut` keyword?\n\nDo you wish you that you didn't have to maintain two independent versions of\nwhat effectively amount to the same function?\n\nWell, your dreams probably aren't going to come true. This might be the next\nbest thing though, assuming you're a normal person that thinks that macros are\nsilly.\n\n## Example\n\n```rust\nuse mutation::prelude::*;\n\nstruct MyVec\u003cT\u003e(Vec\u003cT\u003e);\n\nimpl\u003cT\u003e MyVec\u003cT\u003e {\n    /// This function 'glues' our generic-over-mutation API to the underlying\n    /// non-generic API of `Vec`.\n    pub fn get\u003c'a, R: Ref\u003c'a, Self\u003e\u003e(self: R, idx: usize) -\u003e Option\u003cRefMap\u003c'a, R, T\u003e\u003e {\n        /// The underlying `Vec` is not generic over mutation, so here we\n        /// specialize our function with the corresponding function of `Vec`.\n        self.map::\u003cOption\u003cGenRef\u003cT\u003e\u003e, _, _\u003e(\n            |this: \u0026Self| this.0.get(idx),\n            |this: \u0026mut Self| this.0.get_mut(idx),\n        )\n    }\n\n    /// This function 'glues' our generic-over-mutation API to the underlying\n    /// non-generic API of `Vec`.\n    pub fn iter\u003c'a, R: Ref\u003c'a, Self\u003e\u003e(self: R) -\u003e impl Iterator\u003cItem = RefMap\u003c'a, R, T\u003e\u003e where T: 'a {\n        /// The underlying `Vec` is not generic over mutation, so here we\n        /// specialize our function with the corresponding function of `Vec`.\n        self.map::\u003cSliceIter\u003cT\u003e, _, _\u003e(\n            |this: \u0026Self| this.0.iter(),\n            |this: \u0026mut Self| this.0.iter_mut(),\n        )\n    }\n\n    /// And now for some functions that are truly generic over mutation using\n    /// the API above...\n\n    /// This function works for both mutable and immutable references!\n    pub fn get_expect\u003c'a, R: Ref\u003c'a, Self\u003e\u003e(self: R, idx: usize) -\u003e RefMap\u003c'a, R, T\u003e {\n        self.get(idx).unwrap()\n    }\n\n    /// This function works for both mutable and immutable references!\n    pub fn iter_positive\u003c'a, R: Ref\u003c'a, Self\u003e\u003e(self: R) -\u003e impl Iterator\u003cItem = RefMap\u003c'a, R, T\u003e\u003e\n        where T: PartialOrd\u003ci32\u003e + 'a\n    {\n        self.iter().filter(|x| **x \u003e= 0)\n    }\n}\n\n/// Example usage\n\nlet mut my_vec = MyVec(vec![1, 2, 3]);\n\nassert_eq!((\u0026my_vec).iter().copied().sum::\u003ci32\u003e(), 8);\n\n// Iterate elements immutably\nassert_eq!((\u0026my_vec).iter_positive().copied().sum::\u003ci32\u003e(), 9);\n\n// Mutate the second element\n*(\u0026mut my_vec).get_expect(1) = 4;\n\n// Immutably access the second element\nassert_eq!(my_vec.get_expect(1), Some(\u00264));\n\n// Iterate elements mutably, giving each a value of 1\n(\u0026mut my_vec).iter_positive().for_each(|x| *x = 1);\n\nassert_eq!(my_vec.0, vec![-1, 1, 1, 1]);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Fmutation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzesterer%2Fmutation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Fmutation/lists"}