{"id":17939739,"url":"https://github.com/not-elm/auto-delegate","last_synced_at":"2025-03-24T11:32:32.557Z","repository":{"id":166457490,"uuid":"637316555","full_name":"not-elm/auto-delegate","owner":"not-elm","description":"Auto delegate allows you that automatic impl of traits and delegate their handling to child members.","archived":false,"fork":false,"pushed_at":"2024-07-01T15:33:37.000Z","size":157,"stargazers_count":7,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-10T11:04:31.987Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/not-elm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-07T07:10:39.000Z","updated_at":"2024-07-01T15:29:23.000Z","dependencies_parsed_at":"2023-10-16T04:48:26.208Z","dependency_job_id":"b93646a2-d954-4d42-962d-dcebe0bf5cd8","html_url":"https://github.com/not-elm/auto-delegate","commit_stats":null,"previous_names":["elmtw/auto-delegate","elm-register/auto-delegate","mlulm/auto-delegate"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/not-elm%2Fauto-delegate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/not-elm%2Fauto-delegate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/not-elm%2Fauto-delegate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/not-elm%2Fauto-delegate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/not-elm","download_url":"https://codeload.github.com/not-elm/auto-delegate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245261081,"owners_count":20586518,"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-29T00:43:56.940Z","updated_at":"2025-03-24T11:32:30.852Z","avatar_url":"https://github.com/not-elm.png","language":"Rust","readme":"# auto-delegate\n\n## Supports `no_std`\n\nThis Library is Created without std crates.\n\n## Description\n\nAuto delegate allows you that automatic impl of traits and delegate their handling to children.\n\n## Usage\n\n### Struct\n\nGive 'delegate' attribute to the trait which to be delegated,\nand give 'Delegate' to the structure requesting delegation\n\n#### Example1\n\n```rust\nuse auto_delegate::{Delegate, delegate};\n\n#[delegate]\ntrait Calc {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize;\n}\n\n#[derive(Default)]\nstruct CalcAdd;\n\nimpl Calc for CalcAdd {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize {\n        x1 + x2\n    }\n}\n\n#[derive(Delegate, Default)]\nstruct Parent {\n    #[to(Calc)]\n    child: CalcAdd\n}\n\n\nfn main() {\n    let parent = Parent::default();\n\n    assert_eq!(parent.calc(2, 3), 5);\n}\n```\n\n#### Example2: Multiple traits and fields\n\n```rust\nuse auto_delegate::*;\n\n#[delegate]\ntrait Calc {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize;\n}\n\n\n#[derive(Default)]\nstruct CalcAdd;\n\n\nimpl Calc for CalcAdd {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize {\n        x1 + x2\n    }\n}\n\n\n#[delegate]\ntrait Movable {\n    fn move_to(\u0026mut self, pos: (usize, usize));\n\n    fn pos(\u0026self) -\u003e (usize, usize);\n}\n\n\n#[delegate]\ntrait Resizable {\n    fn resize(\u0026mut self, width: usize, height: usize);\n\n    fn size(\u0026self) -\u003e (usize, usize);\n}\n\n\n#[derive(Default)]\nstruct Transform2D {\n    pos: (usize, usize),\n    width: usize,\n    height: usize\n}\n\n\nimpl Movable for Transform2D {\n    fn move_to(\u0026mut self, pos: (usize, usize)) {\n        self.pos = pos;\n    }\n\n    fn pos(\u0026self) -\u003e (usize, usize) {\n        self.pos\n    }\n}\n\n\nimpl Resizable for Transform2D {\n    fn resize(\u0026mut self, width: usize, height: usize) {\n        self.width = width;\n        self.height = height;\n    }\n\n    fn size(\u0026self) -\u003e (usize, usize) {\n        (self.width, self.height)\n    }\n}\n\n\n#[derive(Default, Delegate)]\nstruct Parent\u003cT: Default + Calc\u003e {\n    #[to(Movable, Resizable)]\n    transform: Transform2D,\n\n    #[to(Calc)]\n    calculator: T\n}\n\n\nfn main() {\n    let mut parent = Parent::\u003cCalcAdd\u003e::default();\n\n    assert_eq!(parent.calc(3, 2), 5);\n\n    parent.move_to((10, 11));\n    assert_eq!(parent.pos(), (10, 11));\n\n    parent.resize(100, 120);\n    assert_eq!(parent.size(), (100, 120));\n}\n```\n\n### Enum\n\nLike Struct, Enum can be delegated using Delegate.\n\n#### Example\n\n```rust\nuse auto_delegate::{delegate, Delegate};\n\n#[delegate]\ntrait Calc {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize;\n}\n\n#[derive(Default)]\nstruct CalcAdd;\n\nimpl Calc for CalcAdd {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize {\n        x1 + x2\n    }\n}\n\n#[derive(Default)]\nstruct CalcSub;\n\nimpl Calc for CalcSub {\n    fn calc(\u0026self, x1: usize, x2: usize) -\u003e usize {\n        x1 - x2\n    }\n}\n\n\n#[derive(Delegate)]\n#[to(Calc)]\nenum EnumCalc {\n    Add(CalcAdd),\n    Sub(CalcSub),\n}\n\n\nfn main() {\n    let c = EnumCalc::Add(CalcAdd::default());\n    assert_eq!(c.calc(3, 5), 8);\n\n\n    let c = EnumCalc::Sub(CalcSub::default());\n    assert_eq!(c.calc(3, 2), 1);\n}\n```\n\n## Todo\n\n- [x] support self-receiver(v0.0.8)\n- [x] support method without receiver(v0.1.0)\n- [ ] support trait fn which returns impl type. ","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnot-elm%2Fauto-delegate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnot-elm%2Fauto-delegate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnot-elm%2Fauto-delegate/lists"}