-
Notifications
You must be signed in to change notification settings - Fork 2
/
InputAllocator.kt
35 lines (28 loc) · 1.22 KB
/
InputAllocator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dev.inmo.micro_utils.common
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
typealias ByteArrayAllocator = () -> ByteArray
typealias SuspendByteArrayAllocator = suspend () -> ByteArray
val ByteArray.asAllocator: ByteArrayAllocator
get() = { this }
val ByteArray.asSuspendAllocator: SuspendByteArrayAllocator
get() = { this }
val ByteArrayAllocator.asSuspendAllocator: SuspendByteArrayAllocator
get() = { this() }
suspend fun SuspendByteArrayAllocator.asAllocator(): ByteArrayAllocator {
return invoke().asAllocator
}
object ByteArrayAllocatorSerializer : KSerializer<ByteArrayAllocator> {
private val realSerializer = ByteArraySerializer()
override val descriptor: SerialDescriptor = realSerializer.descriptor
override fun deserialize(decoder: Decoder): ByteArrayAllocator {
val bytes = realSerializer.deserialize(decoder)
return bytes.asAllocator
}
override fun serialize(encoder: Encoder, value: ByteArrayAllocator) {
realSerializer.serialize(encoder, value())
}
}