Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark block in corresponding encodeStructure/decodeStructure extension… #1917

Merged
merged 1 commit into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 4 additions & 11 deletions core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt
Expand Up @@ -562,19 +562,12 @@ public interface CompositeDecoder {
*/
public inline fun <T> Decoder.decodeStructure(
descriptor: SerialDescriptor,
block: CompositeDecoder.() -> T
crossinline block: CompositeDecoder.() -> T
): T {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
try {
return composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
val result = composite.block()
composite.endStructure(descriptor)
return result
}

private const val decodeMethodDeprecated = "Please migrate to decodeElement method which accepts old value." +
Expand Down
24 changes: 9 additions & 15 deletions core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt
Expand Up @@ -473,18 +473,13 @@ public interface CompositeEncoder {
/**
* Begins a structure, encodes it using the given [block] and ends it.
*/
public inline fun Encoder.encodeStructure(descriptor: SerialDescriptor, block: CompositeEncoder.() -> Unit) {
public inline fun Encoder.encodeStructure(
descriptor: SerialDescriptor,
crossinline block: CompositeEncoder.() -> Unit
) {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
try {
composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
composite.block()
composite.endStructure(descriptor)
}

/**
Expand All @@ -495,10 +490,9 @@ public inline fun Encoder.encodeCollection(
collectionSize: Int,
crossinline block: CompositeEncoder.() -> Unit
) {
with(beginCollection(descriptor, collectionSize)) {
block()
endStructure(descriptor)
}
val composite = beginCollection(descriptor, collectionSize)
composite.block()
composite.endStructure(descriptor)
}

/**
Expand Down
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractPolymorphicSerializer<T : Any> internal constructo
var klassName: String? = null
var value: Any? = null
if (decodeSequentially()) {
return decodeSequentially(this)
return@decodeStructure decodeSequentially(this)
}

mainLoop@ while (true) {
Expand Down