{"id":18406998,"url":"https://github.com/nvzqz/condtype","last_synced_at":"2025-04-09T10:06:40.602Z","repository":{"id":153509503,"uuid":"629672834","full_name":"nvzqz/condtype","owner":"nvzqz","description":"Choose Rust types at compile-time via constants","archived":false,"fork":false,"pushed_at":"2024-01-23T15:00:50.000Z","size":48,"stargazers_count":58,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-09T15:28:58.192Z","etag":null,"topics":["conditional","rust","types"],"latest_commit_sha":null,"homepage":"https://docs.rs/condtype","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/nvzqz.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}},"created_at":"2023-04-18T19:45:18.000Z","updated_at":"2024-04-17T03:20:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"3fc4f192-da8e-4aee-8896-e3e508c5d888","html_url":"https://github.com/nvzqz/condtype","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2Fcondtype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2Fcondtype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2Fcondtype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2Fcondtype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nvzqz","download_url":"https://codeload.github.com/nvzqz/condtype/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018060,"owners_count":21034048,"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":["conditional","rust","types"],"created_at":"2024-11-06T03:11:50.044Z","updated_at":"2025-04-09T10:06:40.566Z","avatar_url":"https://github.com/nvzqz.png","language":"Rust","funding_links":["https://github.com/sponsors/nvzqz","https://paypal.me/nvzqz"],"categories":[],"sub_categories":[],"readme":"# `condtype`\n\n[![docs.rs](https://img.shields.io/crates/v/condtype.svg?style=flat-square\u0026label=docs\u0026color=blue\u0026logo=rust)](https://docs.rs/condtype) [![crates.io](https://img.shields.io/crates/d/condtype.svg?style=flat-square)](https://crates.io/crates/condtype) [![github](https://img.shields.io/github/stars/nvzqz/condtype.svg?style=flat-square\u0026color=black)][github]\n\nChoose Rust types at compile-time via boolean constants, brought to you by\n[Nikolai Vazquez](https://hachyderm.io/@nikolai).\n\nIf you find this library useful, consider\n[starring it][github] as well as\n[sponsoring](https://github.com/sponsors/nvzqz) or\n[donating once](https://paypal.me/nvzqz). 💖\n\n[github]: https://github.com/nvzqz/condtype\n\n## Conditional Typing\n\nThe [`CondType`] type and [`condval!`] macro choose types at compile-time using\n[`bool`] constants, just like [`std::conditional_t` in C++](https://en.cppreference.com/w/cpp/types/conditional).\nUnlike the [`Either`] type, the type chosen by [`CondType`]/[`condval!`] is\ndirectly used, rather than wrapped with an [`enum`] type. This may be considered\na form of [dependent typing](https://en.wikipedia.org/wiki/Dependent_type), but\nit is limited in ability and is restricted to compile-time constants rather than\nruntime values.\n\n## `CondType`\n\nIn the following example, [`CondType`] aliases either [`\u0026str`] or [`i32`],\ndepending on the boolean [generic constant][const-generics]:\n\n```rust\nuse condtype::CondType;\n\nlet str: CondType\u003ctrue,  \u0026str, i32\u003e = \"hello\";\nlet int: CondType\u003cfalse, \u0026str, i32\u003e = 42;\n\n// Unsized types are also supported:\nlet str: \u0026CondType\u003ctrue, str, [u8]\u003e = \"world\";\n```\n\n## `condval!`\n\n[`condval!`] enables choosing differently-typed values without specifying types.\nIn the following example, `val` is inferred to be either [`\u0026str`] or [`i32`],\ndepending on `COND`.\n\n```rust\nuse condtype::condval;\n\nconst COND: bool = true;\n\nlet val = condval!(if COND {\n    \"hello\"\n} else {\n    42\n});\n\nassert_eq!(val, \"hello\");\n```\n\n`if let` pattern matching is also supported:\n\n```rust\nuse condtype::condval;\n\nconst STR: Option\u003c\u0026str\u003e = Some(\"hello\");\n\nlet val = condval!(if let Some(str) = STR {\n    str.to_uppercase()\n} else {\n    42\n});\n\nassert_eq!(val, \"HELLO\");\n```\n\n### Platform-Specific Types\n\nThis library can make code for some platforms more efficient by using\nsmaller-sized types, depending on platform-specific constants.\n\nIn the following example, the `RlimOption` type can be either\n\u003ccode\u003e[Option]\\\u003c[rlim_t][resource.h]\u003e\u003c/code\u003e or [`rlim_t`][resource.h] itself,\nwhere [`rlim_t::MAX`] can be treated as a sentinel value for\n[`Option::None`][None] if it is not equal to [`RLIM_INFINITY`][resource.h].\n\n```rust\nuse condtype::{condval, CondType};\nuse libc::{rlim_t, RLIM_INFINITY};\n\nconst RLIM_INFINITY_IS_MAX: bool = RLIM_INFINITY == rlim_t::MAX;\n\ntype RlimOption = CondType\u003cRLIM_INFINITY_IS_MAX, Option\u003crlim_t\u003e, rlim_t\u003e;\n\nconst RLIM_NONE: RlimOption = condval!(if RLIM_INFINITY_IS_MAX {\n    None::\u003crlim_t\u003e\n} else {\n    rlim_t::MAX\n});\n\n// Convert from either `RlimOption` type to `Option` via the `Into` trait:\nlet rlim_none: Option\u003crlim_t\u003e = RLIM_NONE.into();\n```\n\nWithout this library, one could otherwise use [`cfg_if!`] to achieve the same\ngoal. However, using [`#[cfg]`][cfg] requires maintaining a list of platforms\nand being more fine-grained if [`RLIM_INFINITY`][resource.h] is dependent on CPU\narchitecture.\n\n```rust\nuse cfg_if::cfg_if;\nuse libc::rlim_t;\n\ncfg_if! {\n    // Platforms where `RLIM_INFINITY != rlim_t::MAX`:\n    if #[cfg(any(\n        target_os = \"macos\",\n        target_os = \"freebsd\",\n        target_os = \"solaris\",\n        // ad nauseam...\n    ))] {\n        type RlimOption = rlim_t;\n        const RLIM_NONE: RlimOption = rlim_t::MAX;\n    } else {\n        type RlimOption = Option\u003crlim_t\u003e;\n        const RLIM_NONE: RlimOption = None;\n    }\n}\n```\n\n## Limitations\n\nIt is currently not possible to use [`CondType`] or [`condval!`] with a\n[generic constant][const-generics] because [Rust does not yet consider trait\nimplementations based on booleans to be exhaustive](https://github.com/rust-lang/project-const-generics/issues/26).\nOnce that issue is resolved, all versions of this library should _just work_\nwith generic constants.\n\n```rust,ignore\nfn generic\u003cconst B: bool\u003e() {\n    let val: CondType\u003cB, \u0026str, i32\u003e = condval!(if B {\n        \"hello\"\n    } else {\n        42\n    });\n}\n```\n\n## Install\n\nThis library is [available on crates.io](https://crates.io/crates/condtype) and\ncan be used by running the following `cargo` command in your project directory:\n\n```sh\ncargo add condtype\n```\n\nor by manually adding the following to your project's [`Cargo.toml`](https://doc.rust-lang.org/cargo/reference/manifest.html):\n\n```toml\n[dependencies]\ncondtype = \"1.3.0\"\n```\n\n## License\n\nLike the Rust project, this library may be used under either the\n[MIT License](https://github.com/nvzqz/condtype/blob/main/LICENSE-MIT) or\n[Apache License (Version 2.0)](https://github.com/nvzqz/condtype/blob/main/LICENSE-APACHE).\n\n[`CondType`]: https://docs.rs/condtype/latest/condtype/type.CondType.html\n[`condval!`]: https://docs.rs/condtype/latest/condtype/macro.condval.html\n[`Either`]:   https://docs.rs/either/latest/either/enum.Either.html\n[`cfg_if!`]:  https://docs.rs/cfg-if/latest/cfg_if/macro.cfg_if.html\n\n[`const`]: https://doc.rust-lang.org/std/keyword.const.html\n[`enum`]:  https://doc.rust-lang.org/std/keyword.enum.html\n[`bool`]:  https://doc.rust-lang.org/std/primitive.bool.html\n[`i32`]:   https://doc.rust-lang.org/std/primitive.i32.html\n[`\u0026str`]:  https://doc.rust-lang.org/std/primitive.str.html\n[Option]:  https://doc.rust-lang.org/std/option/enum.Option.html\n[None]:    https://doc.rust-lang.org/std/option/enum.Option.html#variant.None\n[cfg]:     https://doc.rust-lang.org/rust-by-example/attribute/cfg.html\n\n[`rlim_t::MAX`]: https://doc.rust-lang.org/std/primitive.u64.html#associatedconstant.MAX\n\n[const-generics]: https://doc.rust-lang.org/reference/items/generics.html#const-generics\n\n[resource.h]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_resource.h.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnvzqz%2Fcondtype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnvzqz%2Fcondtype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnvzqz%2Fcondtype/lists"}