Skip to content

Commit

Permalink
Added contracts to throwIf and throwUnless
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cooper committed Oct 26, 2021
1 parent b07a57d commit 3810567
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.michaelbull.result.coroutines

class RunSuspendCatching {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.michaelbull.result.coroutines

import kotlin.test.Test

class RunSuspendCatchingTest {

@Test
fun propogatesCoroutineCancellation() {
val testDispatcher = TestCoroutineDispatcher()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.michaelbull.result

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* If the [Result] is [Err] and contains a [Throwable], throws the [error][Err.error]
* if the [predicate] returns true.
Expand All @@ -8,8 +11,10 @@ package com.github.michaelbull.result
*/
public inline fun <V : Any?, E : Throwable> Result<V, E>.throwIf(
predicate: (E) -> Boolean
): Result<V, E> =
onFailure { e -> if (predicate(e)) throw e }
): Result<V, E> {
contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) }
return onFailure { e -> if (predicate(e)) throw e }
}

/**
* If the [Result] is [Err] and contains a [Throwable], throws the [error][Err.error]
Expand Down

0 comments on commit 3810567

Please sign in to comment.