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
Parser
type, andparse_any
macro. -
Functions for comparing many standard library types, with the
const_eq
/const_eq_for
/const_cmp
/const_cmp_for
macros for more conveniently calling them, powered by thepolymorphism
module.
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_macros
dependency, and enables theparse_any
macro. 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 theparsing
module (for parsing from&str
and&[u8]
), theprimitive::parse_*
functions,try_rebind
, andrebind_if_ok
macros. -
alloc"
: Enables items that use types from thealloc
crate, includingVec
andString
.
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_utf8
function (the macro works in all versions),str
indexing 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. Because this crate feature was added before Rust 1.56.0 is released, those unsafe operations might be unstabilized, in which case you'll need to use Rust nightly and the"deref_raw_in_fn"
crate feature. -
"deref_raw_in_fn"
(disabled by default): Requires Rust nightly. Fallback for the case where the"rust_1_56"
feature causes compilation errors because Rust features were unstabilized before the release. -
"constant_time_slice"
(disabled by default): Requires Rust nightly. 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. -
"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"deref_raw_in_fn"
and"rust_1_56"
features. -
"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.