mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 21:23:55 +08:00
[core] Fix relevant uses of Closeable
This commit is contained in:
parent
d4f4ea30d4
commit
2da0a2d6d3
mirai-core-utils/src
mirai-core/src/commonMain/kotlin
@ -7,14 +7,74 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:JvmName("CloseableKt_common")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import io.ktor.utils.io.core.*
|
||||
import io.ktor.utils.io.errors.*
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.jvm.JvmName
|
||||
|
||||
public expect interface Closeable {
|
||||
@Throws(IOException::class)
|
||||
public fun close()
|
||||
}
|
||||
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public expect inline fun <C : Closeable, R> C.use(block: (C) -> R): R
|
||||
|
||||
// overcome overload resolution ambiguity
|
||||
public inline fun <C : Input, R> C.use(block: (C) -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return this.asMiraiCloseable().use { block(this) }
|
||||
}
|
||||
|
||||
// overcome overload resolution ambiguity
|
||||
public inline fun <C : Output, R> C.use(block: (C) -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return this.asMiraiCloseable().use { block(this) }
|
||||
}
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public inline fun <C : Closeable, R> C.withUse(block: C.() -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use(block)
|
||||
}
|
||||
|
||||
// `Input` does not implement `Closeable` in common
|
||||
public inline fun <C : Input, R> C.withUse(block: C.() -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use(block)
|
||||
}
|
||||
|
||||
// `Output` does not implement `Closeable` in common
|
||||
public inline fun <C : Output, R> C.withUse(block: C.() -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use(block)
|
||||
}
|
||||
|
||||
public inline fun <I : Closeable, O : Closeable, R> I.withOut(output: O, block: I.(output: O) -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use { output.use { block(this, output) } }
|
||||
}
|
||||
|
||||
|
||||
public expect fun Closeable.asKtorCloseable(): io.ktor.utils.io.core.Closeable
|
||||
public expect fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): io.ktor.utils.io.core.Closeable
|
||||
public expect fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): Closeable
|
@ -18,8 +18,6 @@ import io.ktor.utils.io.*
|
||||
import io.ktor.utils.io.charsets.*
|
||||
import io.ktor.utils.io.core.*
|
||||
import io.ktor.utils.io.core.internal.*
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.jvm.JvmMultifileClass
|
||||
import kotlin.jvm.JvmName
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
@ -29,20 +27,6 @@ public val EMPTY_BYTE_ARRAY: ByteArray = ByteArray(0)
|
||||
public val DECRYPTER_16_ZERO: ByteArray = ByteArray(16)
|
||||
public val KEY_16_ZEROS: ByteArray = ByteArray(16)
|
||||
|
||||
public inline fun <C : Closeable, R> C.withUse(block: C.() -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use(block)
|
||||
}
|
||||
|
||||
public inline fun <I : Closeable, O : Closeable, R> I.withOut(output: O, block: I.(output: O) -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return use { output.use { block(this, output) } }
|
||||
}
|
||||
|
||||
/**
|
||||
* It's caller's responsibility to close the input
|
||||
*/
|
||||
|
@ -9,7 +9,13 @@
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import io.ktor.utils.io.core.use as ktorUse
|
||||
|
||||
public actual typealias Closeable = java.io.Closeable
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public actual inline fun <C : Closeable, R> C.use(block: (C) -> R): R = ktorUse(block)
|
||||
|
||||
public actual fun Closeable.asKtorCloseable(): io.ktor.utils.io.core.Closeable = this
|
||||
public actual fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): io.ktor.utils.io.core.Closeable = this
|
||||
public actual fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): Closeable = this
|
@ -9,7 +9,13 @@
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import io.ktor.utils.io.core.use as ktorUse
|
||||
|
||||
public actual typealias Closeable = io.ktor.utils.io.core.Closeable
|
||||
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public actual inline fun <C : Closeable, R> C.use(block: (C) -> R): R = ktorUse(block)
|
||||
|
||||
public actual fun Closeable.asKtorCloseable(): io.ktor.utils.io.core.Closeable = this
|
||||
public actual fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): io.ktor.utils.io.core.Closeable = this
|
||||
public actual fun io.ktor.utils.io.core.Closeable.asMiraiCloseable(): Closeable = this
|
@ -9,7 +9,6 @@
|
||||
|
||||
package net.mamoe.mirai.internal.message.data
|
||||
|
||||
import io.ktor.utils.io.core.*
|
||||
import net.mamoe.mirai.contact.Contact
|
||||
import net.mamoe.mirai.contact.Group
|
||||
import net.mamoe.mirai.internal.contact.groupCode
|
||||
@ -34,6 +33,7 @@ import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
|
||||
import net.mamoe.mirai.utils.concatAsLong
|
||||
import net.mamoe.mirai.utils.gzip
|
||||
import net.mamoe.mirai.utils.toLongUnsigned
|
||||
import net.mamoe.mirai.utils.use
|
||||
import kotlin.math.absoluteValue
|
||||
import kotlin.random.Random
|
||||
|
||||
|
@ -13,6 +13,7 @@ import io.ktor.utils.io.core.*
|
||||
import kotlinx.atomicfu.atomic
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import net.mamoe.mirai.utils.Closeable
|
||||
import net.mamoe.mirai.utils.runBIO
|
||||
import net.mamoe.mirai.utils.toLongUnsigned
|
||||
import net.mamoe.mirai.utils.withUse
|
||||
|
Loading…
Reference in New Issue
Block a user