generic-upper-bound
This crate allows performing const calculations with the help of a generic const usize
that is a reasonable upper bound of some desired associated const usize.
The API of this crate is structed as follows:
AcceptUpperBoundis the heart of this crate. Implementors use it to specify which generic const they want to be passed to them and what to do with any given upper bound for it. It can be implemented conveniently usingimpl_accept_upper_bound!.eval_with_upper_boundis used to get the result of evaluating an upper bound acceptor with the best-effort upper bound that this crate can offer.
While you cannot use this to write a function with a signature that returns e.g. [T; M + N]
with generic M and N, you can use it to temporarily get an array of size M + N, use it
to do something useful, then return the result of that computation.
For example, you can concatenate two strings at compile time, even if their value is dependent
on generic parameters:
use generic_upper_bound as gub;
;
impl_accept_upper_bound!
let concatenated: &'static str = SOME_STR;
assert_eq!;
Note that this example can be generalized and optimized. For instance, it is possible to accept
any &'a [&'b str] as input and this will also be more efficient (most of the time)
due to the overhead from the inexact upper bound used for each concatenation (which will
likely affect the final binary size).
MSRV
The MSRV is 1.78. This is to allow this crate to be used as a workaround for the breaking change to const promotion that was introduced by that version.
License: MIT