I’d like to be able to optionally enable expensive pre-/postcondition checks in
I define “expensive” as tests that change the algorithmic complexity of the operations. It is hard to know whether a precondition check might do this because most of the algorithms do not have a bound on their complexity, but we can often guess when this happens.
slice.binary_search requires the input slice to be sorted, otherwise the result is unspecified. The complexity of the
binary_search method is not guaranteed by the standard library, but we can expect it to be
O(log N). However, to verify the precondition, we would need to check whether the slice is sorted making the algorithm
O(N). Because we change the complexity from
O(log N) to
O(N), this test is expensive. It also makes
O(N) kind of pointless, so we cannot enable it on debug builds by default. Still, being able to verify these preconditions in some builds can still be useful.
So… I would like to be able to enable these kind of tests on
std. A strawman proposal could be to:
- add a
expensive_assert!macro to std
- allow users to configure std with different features (and make easy to compile different versions of
stdand use them via cargo)
std doesn’t have any algorithms to check whether a sequence is sorted, but there is an issue for that.