{"id":16831832,"url":"https://github.com/antifuchs/nonzero_ext","last_synced_at":"2025-06-29T21:05:53.770Z","repository":{"id":32989371,"uuid":"148910462","full_name":"antifuchs/nonzero_ext","owner":"antifuchs","description":"Rust traits for nonzero generic integers","archived":false,"fork":false,"pushed_at":"2022-05-24T03:24:12.000Z","size":78,"stargazers_count":15,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-18T09:09:23.285Z","etag":null,"topics":["literals","macro","numbers","rust","trait","traits"],"latest_commit_sha":null,"homepage":null,"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/antifuchs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-15T14:43:07.000Z","updated_at":"2024-09-12T15:32:43.000Z","dependencies_parsed_at":"2022-09-08T15:11:44.480Z","dependency_job_id":null,"html_url":"https://github.com/antifuchs/nonzero_ext","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/antifuchs/nonzero_ext","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antifuchs%2Fnonzero_ext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antifuchs%2Fnonzero_ext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antifuchs%2Fnonzero_ext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antifuchs%2Fnonzero_ext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antifuchs","download_url":"https://codeload.github.com/antifuchs/nonzero_ext/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antifuchs%2Fnonzero_ext/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261710404,"owners_count":23198127,"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":["literals","macro","numbers","rust","trait","traits"],"created_at":"2024-10-13T11:45:31.771Z","updated_at":"2025-06-29T21:05:53.728Z","avatar_url":"https://github.com/antifuchs.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nonzero_ext\n\n## Traits to represent generic nonzero integer types\n![Build status](https://github.com/antifuchs/nonzero_ext/actions/workflows/ci.yml/badge.svg?branch=master) [![Docs](https://docs.rs/nonzero_ext/badge.svg)](https://docs.rs/nonzero_ext)\n\nRust ships with non-zero integer types now, which let programmers\npromise (memory-savingly!) that a number can never be zero. That's\ngreat, but sadly the standard library has not got a whole lot of\ntools to help you use them ergonomically.\n\n### A macro for non-zero constant literals\n\nCreating and handling constant literals is neat, but the standard\nlibrary (and the rust parser at the moment) have no affordances to\neasily create values of `num::NonZeroU*` types from constant\nliterals. This crate ships a `nonzero!` macro that lets you write\n`nonzero!(20u32)`, which checks at compile time that the constant\nbeing converted is non-zero, instead of the cumbersome (and\nruntime-checked!)  `NonZeroU32::new(20).unwrap()`.\n\n### Traits for generic non-zeroness\n\nThe stdlib `num::NonZeroU*` types do not implement any common\ntraits (and neither do their zeroable equivalents).  Where this\nlack of traits in the standard library becomes problematic is if\nyou want to write a function that takes a vector of integers, and\nthat returns a vector of the corresponding non-zero integer types,\nminus any elements that were zero in the original. You can write\nthat with the standard library quite easily for concrete types:\n\n```rust\nfn only_nonzeros(v: Vec\u003cu8\u003e) -\u003e Vec\u003cNonZeroU8\u003e\n{\n    v.into_iter()\n        .filter_map(|n| NonZeroU8::new(n))\n        .collect::\u003cVec\u003cNonZeroU8\u003e\u003e()\n}\nlet expected: Vec\u003cNonZeroU8\u003e = vec![nonzero!(20u8), nonzero!(5u8)];\nassert_eq!(expected, only_nonzeros(vec![0, 20, 5]));\n```\n\nBut what if you want to allow this function to work with any\ninteger type that has a corresponding non-zero type? This crate\ncan help:\n\n```rust\nfn only_nonzeros\u003cI\u003e(v: Vec\u003cI\u003e) -\u003e Vec\u003cI::NonZero\u003e\nwhere\n    I: Sized + NonZeroAble,\n{\n    v.into_iter()\n        .filter_map(|n| n.as_nonzero())\n        .collect::\u003cVec\u003cI::NonZero\u003e\u003e()\n}\n\n// It works for `u8`:\nlet input_u8: Vec\u003cu8\u003e = vec![0, 20, 5];\nlet expected_u8: Vec\u003cNonZeroU8\u003e = vec![nonzero!(20u8), nonzero!(5u8)];\nassert_eq!(expected_u8, only_nonzeros(input_u8));\n\n// And it works for `u32`:\nlet input_u32: Vec\u003cu32\u003e = vec![0, 20, 5];\nlet expected_u32: Vec\u003cNonZeroU32\u003e = vec![nonzero!(20u32), nonzero!(5u32)];\nassert_eq!(expected_u32, only_nonzeros(input_u32));\n```\n\n\nLicense: Apache-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantifuchs%2Fnonzero_ext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantifuchs%2Fnonzero_ext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantifuchs%2Fnonzero_ext/lists"}