1 package kotlinx.coroutines.flow.internal
2 
3 import kotlinx.coroutines.*
4 import kotlinx.coroutines.flow.*
5 
6 /**
7  * This exception is thrown when an operator needs no more elements from the flow.
8  * The operator should never allow this exception to be thrown past its own boundary.
9  * This exception can be safely ignored by non-terminal flow operator if and only if it was caught by its owner
10  * (see usages of [checkOwnership]).
11  * Therefore, the [owner] parameter must be unique for every invocation of every operator.
12  */
13 internal expect class AbortFlowException(owner: Any) : CancellationException {
14     val owner: Any
15 }
16 
checkOwnershipnull17 internal fun AbortFlowException.checkOwnership(owner: Any) {
18     if (this.owner !== owner) throw this
19 }
20 
21 /**
22  * Exception used to cancel child of [scopedFlow] without cancelling the whole scope.
23  */
24 internal expect class ChildCancelledException() : CancellationException
25 
26 @Suppress("NOTHING_TO_INLINE")
27 @PublishedApi
checkIndexOverflownull28 internal inline fun checkIndexOverflow(index: Int): Int {
29     if (index < 0) {
30         throw ArithmeticException("Index overflow has happened")
31     }
32     return index
33 }
34