Crate const_it

Source
Expand description

§const it!

This crate provides some utilities for use in const evaluation contexts, in particular const slice and error handling.

The slice! and try_slice! macros slice (using any usize or range expression):

const STR: &str = slice!("const slice", ..5); // "const"

The slice_split_at! and slice_try_split_at! macros split a slice in two:

const STR: (&str, &str) = slice_split_at!("const slice", 5); // ("const", " slice")

The slice_cmp! and slice_eq! macros compare slices. slice_starts_with! and slice_strip_prefix! checks for and strips a prefix, respectively, and slice_ends_with! and slice_strip_suffix! do the same for suffixes.

The ok!, expect_ok!, unwrap_ok!, unwrap_ok_or_return!, expect_some!, unwrap_some! and unwrap_some_or_return! macros work with Results and Options.

Macros§

expect_ok
Takes a Result and returns the unwrapped Ok value, or panics if it’s Err. The second argument is the message to use on panic. If the panic message is omitted, the Err value must be of type &str and is used as the panic message.
expect_some
Takes an Option and returns the unwrapped Some value, or panics if it’s None. The second argument is the message to use on panic.
ok
Turn a Result into an Option.
slice
Slice an item in a const context. The first argument is the item to slice, and the second is the slice index, which can be a usize or any usize range type. Panics if the index is out of range or, for strings, if the slice would split a unicode codepoint.
slice_cmp
Compare two slices, returning an Ordering. This only works for slices of primitive integer types and str.
slice_ends_with
Check if a slice ends with another slice. This only works for slices of primitive integer types and str.
slice_eq
Check if two slices are equal. This only works for slices of primitive integer types and str.
slice_partial_cmp
Compare two slices, returning an Option<Ordering>. Currently all supported types always return Some. This only works for slices of primitive integer types and str.
slice_split_at
Split a slice in two at the specified index. Panics on error.
slice_starts_with
Check if a slice starts with another slice. This only works for slices of primitive integer types and str.
slice_strip_prefix
Strip a prefix from a slice, returning an Option with the stripped slice on success. This only works for slices of primitive integer types and str.
slice_strip_suffix
Strip a suffix from a slice, returning an Option with the stripped slice on success. This only works for slices of primitive integer types and str.
slice_try_split_at
Split a slice in two at the specified index. Returns None on error.
try_slice
Slice an item in a const context. The first argument is the item to slice, and the second is the slice index, which can be a usize or any usize range type. Returns Some(sliced), or None if the index is out of range or, for strings, if the slice would split a unicode codepoint.
unwrap_ok
Takes a Result and returns the unwrapped Ok value, or panics if it’s Err.
unwrap_ok_or_return
Takes a Result and evaluates to the unwrapped Ok value, or if it’s Err, returns the Err to the current function’s caller.
unwrap_some
Takes an Option and returns the unwrapped Some value, or panics if it’s None.
unwrap_some_or_return
Takes an Option and evaluates to the unwrapped Some value, or if it’s None, returns the None to the current function’s caller.