aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/assertions.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/util/assertions.kt')
-rw-r--r--src/main/kotlin/util/assertions.kt12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/main/kotlin/util/assertions.kt b/src/main/kotlin/util/assertions.kt
index 6f2ed19..86982be 100644
--- a/src/main/kotlin/util/assertions.kt
+++ b/src/main/kotlin/util/assertions.kt
@@ -1,11 +1,18 @@
-
+@file:OptIn(ExperimentalContracts::class)
package moe.nea.firmament.util
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.InvocationKind
+import kotlin.contracts.contract
+
/**
* Less aggressive version of `require(obj != null)`, which fails in devenv but continues at runtime.
*/
inline fun <T : Any> assertNotNullOr(obj: T?, message: String? = null, block: () -> T): T {
+ contract {
+ callsInPlace(block, InvocationKind.AT_MOST_ONCE)
+ }
if (message == null)
assert(obj != null)
else
@@ -18,6 +25,9 @@ inline fun <T : Any> assertNotNullOr(obj: T?, message: String? = null, block: ()
* Less aggressive version of `require(condition)`, which fails in devenv but continues at runtime.
*/
inline fun assertTrueOr(condition: Boolean, block: () -> Unit) {
+ contract {
+ callsInPlace(block, InvocationKind.AT_MOST_ONCE)
+ }
assert(condition)
if (!condition) block()
}