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 CSV
This example demonstrates how an CSV environment variable can be parsed into integers.
This requires the "rust_1_64"
and ""parsing_no_proc""
features
(the latter is enabled by default).
use ;
const CSV: &str = env!;
static PARSED: = collect_const!;
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
None of thse features are enabled by default.
-
"rust_1_51"
: 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"rust_1_51"
feature. -
"rust_1_56"
: Enables items that internally use raw pointer dereferences or transmutes, and the"rust_1_55"
feature. -
"rust_1_57"
: Allowskonst
to use thepanic
macro, and enables the"rust_1_56"
feature. -
"rust_1_61"
: Enables const fns that use trait bounds, and the"rust_1_57"
feature. -
"rust_1_64"
: Adds slice and string iterators, string splitting functions([r]split_once
), const equivalents of iterator methods(inkonst::iter
), and makes slicing functions more efficient. Note that only functions which mention this feature in their documentation are affected. Enables the"rust_1_61"
feature. -
"rust_latest_stable"
: enables the latest"rust_1_*"
feature. Only recommendable if you can update the Rust compiler every stable release. -
"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_latest_stable"
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.