more sugar & unsigned support

This commit is contained in:
sergeych committed 2024-06-30 12:50:52 +07:00
1 parent 1fd229fdb1
commit 904a91b8de
8 files changed
+247 -1063

No files matched your search

@@ -1,5 +1,6 @@
package net.sergeych.bintools
@Suppress("unused")
interface DataSink {
fun writeByte(data: Byte)
@@ -24,9 +25,9 @@ interface DataSink {
}
fun writeVarUInt(value: UInt) { Varint.encodeUnsigned(value.toULong(), this)}
fun writeVarInt(value: UInt) { Varint.encodeSigned(value.toLong(), this)}
fun writeVarInt(value: Int) { Varint.encodeSigned(value.toLong(), this)}
fun writeSmartUInt(value: UInt) { Smartint.encodeUnsigned(value.toULong(), this)}
fun writeSmartInt(value: UInt) { Smartint.encodeSigned(value.toLong(), this)}
fun writeSmartInt(value: Int) { Smartint.encodeSigned(value.toLong(), this)}
}
inline fun <reified T:Any>DataSink.writeNumber(value: T) {
@@ -3,6 +3,7 @@ package net.sergeych.bipack
import kotlinx.datetime.Instant
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.encoding.AbstractDecoder
@@ -143,7 +144,16 @@ class BipackDecoder(
inline fun <reified T> decode(source: DataSource): T = decode(source, serializer())
inline fun <reified T> decode(source: ByteArray): T =
decode(source.toDataSource(), serializer())
fun <T> decode(serializer: KSerializer<T>, source: ByteArray): T =
decode(source.toDataSource(), serializer)
inline fun <reified T> decode(source: UByteArray): T =
decode(source.toDataSource(), serializer())
fun <T> decode(serializer: KSerializer<T>, source: UByteArray): T =
decode(source.toDataSource(), serializer)
}
}
inline fun <reified T> ByteArray.decodeFromBipack() = BipackDecoder.decode<T>(this)
@Suppress("unused")
inline fun <reified T> UByteArray.decodeFromBipack() = BipackDecoder.decode<T>(this)
@@ -126,3 +126,8 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() {
}
}
@Suppress("unused")
inline fun <reified T> toBipackUByteArray(value: T): UByteArray = BipackEncoder.encode(value).toUByteArray()
@Suppress("unused")
inline fun <reified T> toBipackByteArray(value: T): ByteArray = BipackEncoder.encode(value)