Const equivalents of std functions, compile-time comparison, and parsing.
Features
This crate provides:
-
Const fn equivalents of standard library functions and methods.
-
Compile-time parsing through the
Parsertype, andparse_anymacro. -
Functions for comparing many standard library types, with the
const_eq/const_eq_for/const_cmp/const_cmp_formacros for more conveniently calling them, powered by thepolymorphismmodule.
Examples
Parsing an enum
This example demonstrates how you can parse a simple enum from an environment variable, at compile-time.
use eq_str;
use ;
const CHOICE: &str = unwrap_opt_or!;
const DIRECTION: Direction = unwrap_ctx!;
;
use ;
Parsing integers
You can parse integers using the parse_* functions in primitive,
returning an Err(ParseIntError{...}) if the string as a whole isn't a valid integer.
use ;
const N_100: = parse_i128;
assert_eq!;
const N_N3: = parse_i128;
assert_eq!;
// This is how you can unwrap integers parsed from strings, at compile-time.
const N_100_UNW: i128 = unwrap_ctx!;
assert_eq!;
const NONE: = parse_i128;
assert!;
const PAIR: = parse_i128;
assert!;
For parsing an integer inside a larger string,
you can use Parser::parse_u128 method and the other parse_* methods
use ;
const PAIR: = ;
assert_eq!;
assert_eq!;
Parsing a struct
This example demonstrates how you can use Parser to parse a struct at compile-time.
use ;
const PARSED: Struct = ;
pub const
pub const
pub const
pub const
Cargo features
These are the features of these crates:
-
"cmp"(enabled by default): Enables all comparison functions and macros, the string equality and ordering comparison functions don't require this feature. -
"parsing"(enabled by default): Enables the"parsing_no_proc"feature, compiles thekonst_proc_macrosdependency, and enables theparse_anymacro. You can use this feature instead of"parsing_no_proc"if the slightly longer compile times aren't a problem. -
"parsing_no_proc"(enabled by default): Enables theparsingmodule (for parsing from&strand&[u8]), theprimitive::parse_*functions,try_rebind, andrebind_if_okmacros. -
alloc": Enables items that use types from thealloccrate, includingVecandString.
Rust release related
-
"const_generics"(disabled by default): Requires Rust 1.51.0. Enables items that require const generics, and impls for arrays to use const generics instead of only supporting small arrays. -
"rust_1_55": Enables thestring::from_utf8function (the macro works in all versions),strindexing functions, and the"const_generics"feature. -
"rust_1_56": Enables functions that internally use raw pointer dereferences or transmutes, and the"rust_1_55"feature. -
"rust_1_64": Improves the performance of slice functions that split slices, from taking linear time to taking constant time. Note that only functions which mention this feature in their documentation are affected. Enables the"rust_1_56"feature. -
"mut_refs"(disabled by default): Enables const functions that take mutable references. Use this whenever mutable references in const contexts are stabilized. Also enables the"rust_1_64"feature. -
"nightly_mut_refs"(disabled by default): Enables the"mut_refs"feature. Requires Rust nightly.
No-std support
konst is #![no_std], it can be used anywhere Rust can be used.
Minimum Supported Rust Version
konst requires Rust 1.46.0, because it uses looping an branching in const contexts.
Features that require newer versions of Rust, or the nightly compiler, need to be explicitly enabled with cargo features.