{"id":22292641,"url":"https://github.com/rustyyato/vec-option","last_synced_at":"2025-03-25T21:47:03.142Z","repository":{"id":134804688,"uuid":"202449301","full_name":"RustyYato/vec-option","owner":"RustyYato","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-22T15:41:24.000Z","size":28,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T19:16:21.559Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RustyYato.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-15T01:05:10.000Z","updated_at":"2024-07-22T15:41:27.000Z","dependencies_parsed_at":"2024-07-22T18:44:53.115Z","dependency_job_id":null,"html_url":"https://github.com/RustyYato/vec-option","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/RustyYato%2Fvec-option","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RustyYato%2Fvec-option/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RustyYato%2Fvec-option/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RustyYato%2Fvec-option/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RustyYato","download_url":"https://codeload.github.com/RustyYato/vec-option/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245550540,"owners_count":20633872,"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-03T17:23:14.118Z","updated_at":"2025-03-25T21:47:03.122Z","avatar_url":"https://github.com/RustyYato.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vec-option\n\nA space optimized version of `Vec\u003cOption\u003cT\u003e\u003e` that stores the discriminant seperately.\n\n## Feature flags\n\n`nightly` - This turns on a few optimizations (makes `Clone`ing `Copy` elements much cheaper) and extends `try_fold` and `try_for_each` to work with all `Try` types. Finally, this also allows the `iterator.nth_back(n)` methods to be used.\n\n## Pros\n\n* Can have a smaller memory footprint compared to `Vec\u003cOption\u003cT\u003e\u003e` if `Option\u003cT\u003e`'s space optimizations don't take effect\n* More cache-friendly if `Option\u003cT\u003e`'s space optimizations don't take effect\n* Quickly set the entire collection to contain `None`\n* Fast extend with `None`\n\n## Cons\n\n* 2 allocations, instead of a single allocation\n* Cannot remove elements from the middle of the vector\n* Cannot work on the option's directly\n\n## Example\n\nJust like a normal vector, you can push and pop elements from the end of the vector\n\n```rust\nlet mut vec = VecOption::new();\n\nvec.push(10);\n\nassert_eq!(vec, [Some(10)]);\n\nvec.push(20);\nvec.push(None);\nvec.push(Some(30));\n\nassert_eq!(vec, [Some(10), Some(20), None, Some(30)]);\n\nassert_eq!(vec.pop(), Some(Some(30)));\nassert_eq!(vec.pop(), Some(None));\nassert_eq!(vec.pop(), Some(Some(20)));\nassert_eq!(vec.pop(), Some(Some(10)));\nassert_eq!(vec.pop(), None);\nassert_eq!(vec, []);\n```\n\nYou can get elements from the vector\n\n```rust\nlet mut vec = VecOption::from(vec![0, 1, 2, 3, 4]);\n\nassert_eq!(vec.get(2), Some(Some(\u00262)));\nassert_eq!(vec.get_mut(4), Some(Some(\u0026mut 4)));\nassert_eq!(vec.get(5), None);\n```\n\nYou can swap and replace elements\n\n```rust\nvec.swap(2, 1);\n\nassert_eq!(vec, [Some(0), Some(2), Some(1), Some(3), Some(4)]);\n\nassert_eq!(vec.replace(3, None), Some(Some(3)));\nassert_eq!(vec.replace(1, Some(10)), Some(Some(1)));\n\nassert_eq!(vec, [Some(0), Some(10), Some(1), None, Some(4)]);\n```\n\nor if `vec.replace(index, None)` is too much, you can do\n\n```rust\nassert_eq!(vec.take(1), Some(Some(10)));\n\nassert_eq!(vec, [Some(0), None, Some(1), None, Some(4)]);\n```\n\nOf course, you can also truncate or clear the vector\n\n```rust\nlet mut vec = VecOption::from(vec![0, 1, 3, 4]);\n\nassert_eq!(vec.len(), 4);\n\nvec.truncate(2);\n\nassert_eq!(vec, [0, 1]);\n\nvec.clear();\n\nassert!(vec.is_empty());\n```\n\nBut due to the limitations imposed by spliting the representation of the vector, you can't really get a\n`\u0026Option\u003cT\u003e`/`\u0026mut Option\u003cT\u003e` outside of a closure.\nIn fact, you can't get an `\u0026Option\u003cT\u003e` at all, it would be fairly useless, as the only thing you can really do with it is convert it to a `Option\u003c\u0026T\u003e`. But `\u0026mut Option\u003cT\u003e` is usefull, so there are a handful of functions that allow you to operate with them.\n\n```rust\n// This one allows you to edit a single value however you want, and the updates will\n// be reflected once the closure returns. If the closure panics, then it is as if you took the\n// option out of the vector.\nvec.with_mut(index, |element: \u0026mut Option\u003cT\u003e| {\n    ...\n});\n```\n\nThese functions below are like the corrosponding functions in `Iterator`, they iterate over the vector and allow you to do stuff based on which one you call. The only difference is that you get to operate on `\u0026mut Option\u003cT\u003e` directly. Again, if the closure panics, it will be as if you took the value out of the vector.\n\n```rust\nvec.try_fold(...);\n\nvec.fold(...);\n\nvec.try_for_each(...);\n\nvec.for_each(...);\n```\n\nBut because of these limitations, you can very quickly fill up your vector with `None` and set all of the elements in your vector to `None`! This can compile down to just a `memset` if your types don't have drop glue!\n\n```rust\nlet mut vec = VecOption::from(vec![0, 1, 2, 3, 4]);\n\nassert_eq!(vec, [Some(0), Some(2), Some(1), Some(3), Some(4)]);\n\nvec.extend_none(5);\n\nassert_eq!(vec, [Some(0), Some(2), Some(1), Some(3), Some(4), None, None, None, None, None]);\n\nvec.set_all_none();\n\nassert_eq!(vec, [None, None, None, None, None, None, None, None, None, None]);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustyyato%2Fvec-option","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frustyyato%2Fvec-option","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustyyato%2Fvec-option/lists"}