{"id":22426613,"url":"https://github.com/theypsilon/arraygen","last_synced_at":"2025-08-01T09:30:52.654Z","repository":{"id":57494003,"uuid":"223795871","full_name":"theypsilon/arraygen","owner":"theypsilon","description":"Derive macro for generating arrays from struct fields.","archived":false,"fork":false,"pushed_at":"2022-10-06T20:18:55.000Z","size":86,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-01T00:08:56.325Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theypsilon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"ko_fi":"theypsilon"}},"created_at":"2019-11-24T19:11:06.000Z","updated_at":"2023-03-10T13:12:52.000Z","dependencies_parsed_at":"2022-09-14T21:51:20.280Z","dependency_job_id":null,"html_url":"https://github.com/theypsilon/arraygen","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/theypsilon%2Farraygen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theypsilon%2Farraygen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theypsilon%2Farraygen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theypsilon%2Farraygen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theypsilon","download_url":"https://codeload.github.com/theypsilon/arraygen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228357931,"owners_count":17907463,"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-12-05T19:22:46.066Z","updated_at":"2024-12-05T19:22:47.098Z","avatar_url":"https://github.com/theypsilon.png","language":"Rust","funding_links":["https://ko-fi.com/theypsilon"],"categories":[],"sub_categories":[],"readme":"# Arraygen\n\n[![Crates.io](https://img.shields.io/crates/v/arraygen.svg)](https://crates.io/crates/arraygen)\n[![Docs](https://docs.rs/arraygen/badge.svg)](https://docs.rs/arraygen)\n[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/josembarroso.svg?style=social\u0026label=Follow%20%40josembarroso)](https://twitter.com/josembarroso)\n\u003cspan class=\"badge-buymeacoffee\"\u003e\u003ca href=\"https://ko-fi.com/theypsilon\" title=\"Buy Me a Coffee at ko-fi.com'\"\u003e\u003cimg src=\"https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg\" alt=\"Buy Me a Coffee at ko-fi.com'\" /\u003e\u003c/a\u003e\u003c/span\u003e\n\nThis crate provides `Arraygen` derive macro for structs, which generates methods returning arrays filled with the selected struct fields.\n\n#### Complete example:\n\n```rust\n#[derive(Arraygen, Debug)]\n#[gen_array(fn get_names: \u0026mut String)]\nstruct Person {\n    #[in_array(get_names)]\n    first_name: String,\n    #[in_array(get_names)]\n    last_name: String,\n}\n\nlet mut person = Person {\n    first_name: \"Ada\".into(),\n    last_name: \"Lovelace\".into()\n};\n\nfor name in person.get_names() {\n    *name = name.to_lowercase();\n}\n\nassert_eq!(\n    format!(\"{:?}\", person),\n    \"Person { first_name: \\\"ada\\\", last_name: \\\"lovelace\\\" }\"\n);\n// PASSES !\n// Notice how it was not lowercased on object creation\n// but now it is.\n```\n\nAs you can see above, the `gen_array` attribute generates a new method returning an array of the indicated type. Additionally, the `in_array` attribute tells which fields are contained within the array returned by that method.\n\nWhat `Arraygen` does under the hood is simply generating the following impl:\n\n```rust\nimpl Person {\n    #[inline(always)]\n    fn get_names(\u0026mut self) -\u003e [\u0026mut String; 2] {\n        [\u0026mut self.first_name, \u0026mut self.last_name]\n    }\n}\n```\n\n#### The attribute `gen_array`:\n\nFor generating an `Arraygen` method you have to use the attribute `gen_array` on top of the struct. There you will indicate the method name and the return type. Optionally, you can also indicate the visibility or an `implicit_select_all` clause. In the following example you'll see how to tweak the visibility:\n\n```rust\n#[derive(Arraygen)]\n#[gen_array(pub(crate) fn get_strings: \u0026String)]\nstruct Foo {...}\n```\n\nIn the code above, the struct `Foo` would have a new method with the following signature:\n\n```rust\npub(crate) fn get_strings(\u0026self) -\u003e [\u0026String; ?] {...}\n```\n\n#### The attribute `in_array`:\n\nTo fill your `Arraygen` methods with struct fields, you have to use the attribute `in_array` in each struct field you want to include.\n\n```rust\n// inside a struct\n#[in_array(get_strings)]\nid: String,\n\n#[in_array(get_strings, get_names)]\nname: String,\n```\n\nYou have to match the method name used in `gen_array` and `in_array` to include these fields in each generated method. So in this example, assuming `gen_strings` and `get_names` are both generated by `gen_array`, the former will get populated with the fields `id` and `name`, and the latter will get populated with the field `name`.\n\nIt is also possible to entirely omit the attribute `in_array` with the use of an `implicit_select_all` clause. Check the [\"implicit_select_all\" section in the documentation](https://docs.rs/arraygen/0.3.2/arraygen/derive.Arraygen.html#implicitly-selection-fields-by-their-types) to learn more about this possibility.\n\n\n#### Generating arrays of Trait Objects:\n\nTrait Objects are fully supported, check the [Trait Objects section in the documentation](https://docs.rs/arraygen/0.3.2/arraygen/derive.Arraygen.html#trait-objects) to see a few working examples.\n\n#### Implicit selection of Fields by their Types\n\nWith the clause `implicit_select_all`, you may select fields without using `in_array`, check [this section in the documentation](https://docs.rs/arraygen/0.3.2/arraygen/derive.Arraygen.html#implicitly-selection-fields-by-their-types) to see an example.\n\n\n## Documentation\n\nFor more information, check the [documentation page](https://docs.rs/arraygen).\n\n## Usage\n\nWith Cargo, you can add this line to your Cargo.toml:\n\n```toml\n[dependencies]\narraygen = \"0.3.2\"\n```\n\n## About the Syntax\n\nI'm open to change the syntax for the 1.0 version. Participate in the issue [Syntax Proposals](https://github.com/theypsilon/arraygen/issues/1) to give your opinion on this matter.\n\n## Known Problems\n\nError messages could be improved in a few cases. And there is no proper support for warnings yet. Arraygen prints standard messages instead of warnings at the moment.\n\n## GettersByType\n\nThis crate is heavily inspired by [GettersByType](https://github.com/theypsilon/getters-by-type-rs) which is another derive that allows you\nto do the same thing. But that one is more opinionated, less flexible, and less powerful, with the only advantage of being less verbose. That's\nwhy I decided to work on this new solution.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheypsilon%2Farraygen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheypsilon%2Farraygen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheypsilon%2Farraygen/lists"}