From a5757f6db4670c09334f67f10d10455d8f10f88b Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Wed, 12 Feb 2025 18:33:27 +0100 Subject: [PATCH] Small style fixes --- .../src/io/ktor/utils/io/ByteChannel.kt | 19 ++++++++++--------- .../common/src/io/ktor/utils/io/CloseToken.kt | 6 +++--- .../io/ktor/utils/io/SinkByteWriteChannel.kt | 6 ++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt b/ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt index 8e8da8bc0d6..71f8ccda4b7 100644 --- a/ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt +++ b/ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt @@ -1,16 +1,19 @@ /* - * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.utils.io import io.ktor.utils.io.locks.* -import kotlinx.atomicfu.* -import kotlinx.coroutines.* -import kotlinx.io.* +import kotlinx.atomicfu.AtomicRef +import kotlinx.atomicfu.atomic +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.io.Buffer +import kotlinx.io.Sink +import kotlinx.io.Source import kotlin.concurrent.Volatile -import kotlin.coroutines.* -import kotlin.jvm.* +import kotlin.coroutines.Continuation +import kotlin.jvm.JvmStatic internal expect val DEVELOPMENT_MODE: Boolean internal const val CHANNEL_MAX_SIZE: Int = 1024 * 1024 @@ -171,9 +174,7 @@ public class ByteChannel(public val autoFlush: Boolean = false) : ByteReadChanne private fun closeSlot(cause: Throwable?) { val closeContinuation = if (cause != null) Slot.Closed(cause) else Slot.CLOSED val continuation = suspensionSlot.getAndSet(closeContinuation) - if (continuation !is Slot.Task) return - - continuation.resume(cause) + if (continuation is Slot.Task) continuation.resume(cause) } private inline fun trySuspend( diff --git a/ktor-io/common/src/io/ktor/utils/io/CloseToken.kt b/ktor-io/common/src/io/ktor/utils/io/CloseToken.kt index 37419667a24..9884f05e344 100644 --- a/ktor-io/common/src/io/ktor/utils/io/CloseToken.kt +++ b/ktor-io/common/src/io/ktor/utils/io/CloseToken.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.utils.io @@ -13,7 +13,7 @@ internal val CLOSED = CloseToken(null) @OptIn(ExperimentalCoroutinesApi::class) internal class CloseToken(private val origin: Throwable?) { - fun wrapCause(wrap: (Throwable?) -> Throwable = ::ClosedByteChannelException): Throwable? { + fun wrapCause(wrap: (Throwable) -> Throwable = ::ClosedByteChannelException): Throwable? { return when (origin) { null -> null is CopyableThrowable<*> -> origin.createCopy() @@ -22,6 +22,6 @@ internal class CloseToken(private val origin: Throwable?) { } } - fun throwOrNull(wrap: (Throwable?) -> Throwable): Unit? = + fun throwOrNull(wrap: (Throwable) -> Throwable): Unit? = wrapCause(wrap)?.let { throw it } } diff --git a/ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt b/ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt index c79acbf035e..693814c23c3 100644 --- a/ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt +++ b/ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt @@ -6,7 +6,10 @@ package io.ktor.utils.io import kotlinx.atomicfu.AtomicRef import kotlinx.atomicfu.atomic -import kotlinx.io.* +import kotlinx.io.IOException +import kotlinx.io.RawSink +import kotlinx.io.Sink +import kotlinx.io.buffered /** * Creates a [ByteWriteChannel] that writes to this [Sink]. @@ -58,7 +61,6 @@ internal class SinkByteWriteChannel(origin: RawSink) : ByteWriteChannel { if (!closed.compareAndSet(expect = null, update = CLOSED)) return } - @OptIn(InternalAPI::class) override fun cancel(cause: Throwable?) { val token = if (cause == null) CLOSED else CloseToken(cause) if (!closed.compareAndSet(expect = null, update = token)) return