Skip to content

Commit f0d9d51

Browse files
committed
fix: add contract for in place invocation to FlowGraphConfigScope
1 parent 08af1a9 commit f0d9d51

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/main/kotlin/dev/silenium/libs/flows/impl/FlowGraphImpl.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package dev.silenium.libs.flows.impl
33
import dev.silenium.libs.flows.api.*
44
import kotlinx.coroutines.*
55
import kotlinx.coroutines.flow.map
6+
import kotlin.contracts.ExperimentalContracts
7+
import kotlin.contracts.InvocationKind
8+
import kotlin.contracts.contract
69
import kotlin.coroutines.CoroutineContext
710
import kotlin.reflect.KClass
811

@@ -147,10 +150,14 @@ internal fun FlowGraph.builder() = FlowGraphConfigScopeImpl(this)
147150
* @see FlowGraph
148151
* @see CoroutineContext
149152
*/
153+
@OptIn(ExperimentalContracts::class)
150154
suspend fun FlowGraph(
151155
coroutineContext: CoroutineContext = Dispatchers.Default,
152156
block: FlowGraphConfigScope.() -> Unit,
153-
): FlowGraph = FlowGraphImpl(coroutineContext).builder().apply(block).configure().getOrThrow()
157+
): FlowGraph {
158+
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
159+
return FlowGraphImpl(coroutineContext).builder().apply(block).configure().getOrThrow()
160+
}
154161

155162
/**
156163
* Creates a new [FlowGraph] with the given [coroutineScope] and [block] configuration.
@@ -163,7 +170,11 @@ suspend fun FlowGraph(
163170
* @see FlowGraph
164171
* @see CoroutineScope
165172
*/
173+
@OptIn(ExperimentalContracts::class)
166174
suspend fun FlowGraph(
167175
coroutineScope: CoroutineScope,
168176
block: FlowGraphConfigScope.() -> Unit,
169-
): FlowGraph = FlowGraphImpl(coroutineScope).builder().apply(block).configure().getOrThrow()
177+
): FlowGraph {
178+
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
179+
return FlowGraphImpl(coroutineScope).builder().apply(block).configure().getOrThrow()
180+
}

src/test/kotlin/dev/silenium/libs/flows/impl/FlowGraphImplTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.silenium.libs.flows.impl
22

3+
import dev.silenium.libs.flows.api.SourceFlowGraphElement
34
import dev.silenium.libs.flows.buffer.BufferSink
45
import dev.silenium.libs.flows.buffer.BufferSource
56
import dev.silenium.libs.flows.test.Base64Buffer
@@ -16,16 +17,16 @@ import kotlinx.coroutines.flow.firstOrNull
1617

1718
class FlowGraphImplTest : FunSpec({
1819
test("FlowGraphBuilder") {
20+
val source: SourceFlowGraphElement<Base64Buffer, DataType, BufferSource<Base64Buffer, DataType>>
1921
val graph = FlowGraph(CoroutineScope(Dispatchers.Default)) {
20-
val source = source(BufferSource<Base64Buffer, DataType>(0u to DataType.BASE64), "buffer-source")
22+
source = source(BufferSource(0u to DataType.BASE64), "buffer-source")
2123
val sink = sink(BufferSink<ByteArray, DataType>(), "buffer-sink")
2224
val decoder = transformer(Base64Decoder(), "base64-decoder")
2325
connect(source to decoder)
2426
connect(decoder to sink) { _, _, sourcePad, _ ->
2527
sourcePad + 1u
2628
}
2729
}
28-
val source = graph.source<BufferSource<Base64Buffer, DataType>>("buffer-source")!!
2930
val sink = graph.sink<BufferSink<ByteArray, DataType>>("buffer-sink")!!
3031

3132
val input = "test"

0 commit comments

Comments
 (0)