Skip to content

Commit 0dc8319

Browse files
Rename CBF to Cbf, Add Any constraint to BinarySerializer T
1 parent e9cd6f7 commit 0dc8319

21 files changed

+144
-139
lines changed

cosmic-binary-format/src/jmh/kotlin/benchmark/MapSerializerBenchmark.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.openjdk.jmh.annotations.OutputTimeUnit
77
import org.openjdk.jmh.annotations.Scope
88
import org.openjdk.jmh.annotations.Setup
99
import org.openjdk.jmh.annotations.State
10-
import xyz.xenondevs.cbf.CBF
10+
import xyz.xenondevs.cbf.Cbf
1111
import java.util.concurrent.TimeUnit
1212
import kotlin.random.Random
1313

@@ -58,27 +58,27 @@ open class MapSerializerBenchmark {
5858

5959
@Benchmark
6060
fun serializeMap10() {
61-
CBF.write(map10)
61+
Cbf.write(map10)
6262
}
6363

6464
@Benchmark
6565
fun serializeMap1k() {
66-
CBF.write(map1k)
66+
Cbf.write(map1k)
6767
}
6868

6969
@Benchmark
7070
fun serializeMap1m() {
71-
CBF.write(map1m)
71+
Cbf.write(map1m)
7272
}
7373

7474
@Benchmark
7575
fun serializeNestedMap10() {
76-
CBF.write(nestedMap10)
76+
Cbf.write(nestedMap10)
7777
}
7878

7979
@Benchmark
8080
fun serializeNestedMap1k() {
81-
CBF.write(nestedMap1k)
81+
Cbf.write(nestedMap1k)
8282
}
8383

8484
}

cosmic-binary-format/src/main/kotlin/xyz/xenondevs/cbf/CBFSecurityManager.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import kotlin.reflect.KType
55

66
/**
77
* The security manager is responsible for controlling the creation of serializers.
8-
*
8+
*
99
* For example, in environments with a plugin system, the security manager can be used to prevent plugins from registering serializers
1010
* for third-party types, preventing multiple serializers with different formats from existing.
1111
*/
12-
interface CBFSecurityManager {
12+
interface CbfSecurityManager {
1313

1414
/**
1515
* Checks whether the given [serializer] for [type] is allowed to be registered.
1616
*/
17-
fun <T> isAllowed(type: KType, serializer: BinarySerializer<T>): Boolean
17+
fun <T : Any> isAllowed(type: KType, serializer: BinarySerializer<T>): Boolean
1818

1919
}
2020

2121
/**
22-
* An exception that is thrown when an action is prevented by the [CBFSecurityManager].
22+
* An exception that is thrown when an action is prevented by the [CbfSecurityManager].
2323
*/
24-
class CBFSecurityException internal constructor(type: KType, any: Any) : RuntimeException(
25-
"Registration of $any for $type prevented by CBFSecurityManager"
24+
class CbfSecurityException internal constructor(type: KType, any: Any) : RuntimeException(
25+
"Registration of $any for $type prevented by CbfSecurityManager"
2626
)

cosmic-binary-format/src/main/kotlin/xyz/xenondevs/cbf/Compound.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlin.concurrent.withLock
1515
import kotlin.reflect.KType
1616
import kotlin.reflect.full.isSubtypeOf
1717
import kotlin.reflect.full.isSupertypeOf
18+
import kotlin.reflect.full.withNullability
1819
import kotlin.reflect.typeOf
1920

2021
/**
@@ -96,7 +97,7 @@ private class DirectCompoundEntry<T : Any> private constructor(
9697
return ProviderCompoundEntry(type, this.cachedValue as R, default)
9798
} else {
9899
assert(bin != null)
99-
val value: R = CBF.read(type, bin!!)
100+
val value: R = Cbf.read(type, bin!!)
100101
?: throw AssertionError("Serialized value is null, but $type was expected")
101102
return ProviderCompoundEntry(type, value, default)
102103
}
@@ -116,17 +117,20 @@ private class DirectCompoundEntry<T : Any> private constructor(
116117

117118
@OptIn(UncheckedApi::class)
118119
override fun get(type: KType): T {
120+
val type = type.withNullability(false)
121+
119122
if (this.type != null && this.cachedValue != null) {
120123
if (!type.isSupertypeOf(this.type!!))
121124
throw IllegalArgumentException("$type (return type) is not a supertype of ${this.type} (entry type)")
122125
return this.cachedValue as T
123126
} else {
124127
assert(bin != null)
125128

126-
this.type = type
127-
this.cachedValue = CBF.read(type, bin!!)
128-
?: throw AssertionError("Serialized value is null, but $type was expected")
129-
this.bin = null
129+
set(
130+
type.withNullability(false),
131+
Cbf.read(type, bin!!)
132+
?: throw AssertionError("Serialized value is null, but $type was expected")
133+
)
130134

131135
return this.cachedValue!!
132136
}
@@ -135,7 +139,7 @@ private class DirectCompoundEntry<T : Any> private constructor(
135139
@OptIn(UncheckedApi::class)
136140
override fun serialize(): ByteArray? {
137141
if (type != null && cachedValue != null) {
138-
return CBF.write(type!!, cachedValue)
142+
return Cbf.write(type!!, cachedValue)
139143
} else {
140144
return bin
141145
}
@@ -156,7 +160,7 @@ private class DirectCompoundEntry<T : Any> private constructor(
156160
} else {
157161
// no type check possible
158162
assert(bin != null)
159-
val value = CBF.read<T>(entry.type, bin!!)
163+
val value = Cbf.read<T>(entry.type, bin!!)
160164
?: throw AssertionError("Serialized value is null, but $type was expected")
161165
entry.set(entry.type, value)
162166
}
@@ -201,7 +205,7 @@ private class ProviderCompoundEntry<T>(
201205

202206
@OptIn(UncheckedApi::class)
203207
override fun serialize(): ByteArray? {
204-
return cachedValue?.let { CBF.write(type, it) }
208+
return cachedValue?.let { Cbf.write(type, it) }
205209
}
206210

207211
override fun transferTo(entry: CompoundEntry<T>) {
@@ -221,7 +225,7 @@ private class ProviderCompoundEntry<T>(
221225
}
222226

223227
/**
224-
* A compound is a key-value storage intended for serialization via [CBF].
228+
* A compound is a key-value storage intended for serialization via [Cbf].
225229
*
226230
* After deserialization, a [Compound] contains only binary data of values of unknown types
227231
* (and the keys under which they're stored), which is then lazily deserialized
@@ -264,7 +268,7 @@ class Compound private constructor(
264268
when (entry) {
265269
is DirectCompoundEntry<T> -> {
266270
if (value != null) {
267-
entry.set(type, value)
271+
entry.set(type.withNullability(false), value)
268272
} else {
269273
entryMap -= key
270274
}
@@ -273,7 +277,7 @@ class Compound private constructor(
273277
is ProviderCompoundEntry<T> -> entry.set(type, value)
274278
null -> {
275279
if (value != null) {
276-
entryMap[key] = DirectCompoundEntry(type, value)
280+
entryMap[key] = DirectCompoundEntry(type.withNullability(false), value)
277281
}
278282
}
279283
}
@@ -434,11 +438,11 @@ class Compound private constructor(
434438
}
435439

436440
/**
437-
* Creates a deep copy of this compound, copying all deserialized values using [CBF.copy].
441+
* Creates a deep copy of this compound, copying all deserialized values using [Cbf.copy].
438442
*/
439443
@OptIn(UncheckedApi::class)
440444
fun copy(): Compound = lock.withLock {
441-
copy { type, obj -> CBF.copy(type, obj) }
445+
copy { type, obj -> Cbf.copy(type, obj)!! }
442446
}
443447

444448
/**

0 commit comments

Comments
 (0)