generic-upper-bound
Provides functionality to get a const generic usize that is that is a reasonable
upper bound for a specified associated const usize for the purpose of intermediate
const calculations, as a workaround for generic_const_exprs.
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 and what to do with any given upper bound for it.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 paramters:
use generic_upper_bound as gub;
;
;
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] where 'b: 'a 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
MSRV is 1.78. This is to allow this crate to be used as a workaround the breaking change to const promotion that was introduced by that version.