diff --git a/mirai-core-utils/src/commonMain/kotlin/TypeSafeMap.kt b/mirai-core-utils/src/commonMain/kotlin/TypeSafeMap.kt index ea1f0bdf4..bf14b4b9b 100644 --- a/mirai-core-utils/src/commonMain/kotlin/TypeSafeMap.kt +++ b/mirai-core-utils/src/commonMain/kotlin/TypeSafeMap.kt @@ -14,10 +14,10 @@ package net.mamoe.mirai.utils import kotlinx.serialization.Serializable import kotlin.contracts.InvocationKind import kotlin.contracts.contract +import kotlin.jvm.JvmInline import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -import kotlin.jvm.JvmInline @Serializable @JvmInline @@ -175,13 +175,13 @@ internal class MutableTypeSafeMapImpl( } } -public fun TypeSafeMap.toMutableTypeSafeMap(): MutableTypeSafeMap = MutableTypeSafeMap(this.toMap()) +public fun TypeSafeMap.toMutableTypeSafeMap(): MutableTypeSafeMap = createMutableTypeSafeMap(this.toMap()) -public inline fun MutableTypeSafeMap(): MutableTypeSafeMap = MutableTypeSafeMapImpl() -public inline fun MutableTypeSafeMap(map: Map<String, Any>): MutableTypeSafeMap = +public inline fun createMutableTypeSafeMap(): MutableTypeSafeMap = MutableTypeSafeMapImpl() +public inline fun createMutableTypeSafeMap(map: Map<String, Any>): MutableTypeSafeMap = MutableTypeSafeMapImpl().also { it.map.putAll(map) } -public inline fun TypeSafeMap(): TypeSafeMap = TypeSafeMap.EMPTY +public inline fun createTypeSafeMap(): TypeSafeMap = TypeSafeMap.EMPTY public inline fun buildTypeSafeMap(block: MutableTypeSafeMap.() -> Unit): MutableTypeSafeMap { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } diff --git a/mirai-core-utils/src/commonTest/kotlin/net/mamoe/mirai/utils/TypeSafeMapTest.kt b/mirai-core-utils/src/commonTest/kotlin/net/mamoe/mirai/utils/TypeSafeMapTest.kt index bc8f8ddce..049ee39de 100644 --- a/mirai-core-utils/src/commonTest/kotlin/net/mamoe/mirai/utils/TypeSafeMapTest.kt +++ b/mirai-core-utils/src/commonTest/kotlin/net/mamoe/mirai/utils/TypeSafeMapTest.kt @@ -23,7 +23,7 @@ internal class TypeSafeMapTest { @Test fun `can set get`() { - val map = MutableTypeSafeMap() + val map = createMutableTypeSafeMap() map[myKey] = "str" map[myKey2] = "str2" assertEquals(2, map.size) @@ -34,7 +34,7 @@ internal class TypeSafeMapTest { @Test fun `test nulls`() { - val map = MutableTypeSafeMap() + val map = createMutableTypeSafeMap() map[myNullableKey] = null map[myNullableKey2] = "str2" assertEquals(2, map.size) @@ -44,7 +44,7 @@ internal class TypeSafeMapTest { @Test fun `key is inlined`() { - val map = MutableTypeSafeMap() + val map = createMutableTypeSafeMap() map[TypeKey<String>("test")] = "str" map[TypeKey<String>("test")] = "str2" assertEquals(1, map.size) @@ -53,7 +53,7 @@ internal class TypeSafeMapTest { @Test fun `can toMap`() { - val map = MutableTypeSafeMap() + val map = createMutableTypeSafeMap() map[myKey] = "str" map[myKey2] = "str2" assertEquals(2, map.size) @@ -67,7 +67,7 @@ internal class TypeSafeMapTest { @Test fun `test serialization`() { - val map = MutableTypeSafeMap() + val map = createMutableTypeSafeMap() map[myKey] = "str" map[myKey2] = "str2" assertEquals(2, map.size) @@ -86,7 +86,7 @@ internal class TypeSafeMapTest { val string = yaml.encodeToString(map1) println(string) // { "test2": "str2" ,"test": "str" } - val result = MutableTypeSafeMap(Yaml.decodeMapFromString(string).cast()) + val result = createMutableTypeSafeMap(Yaml.decodeMapFromString(string).cast()) assertEquals(map, result) } } \ No newline at end of file diff --git a/mirai-core/src/commonTest/kotlin/notice/processors/AbstractNoticeProcessorTest.kt b/mirai-core/src/commonTest/kotlin/notice/processors/AbstractNoticeProcessorTest.kt index cf4e71fe9..592c10e2f 100644 --- a/mirai-core/src/commonTest/kotlin/notice/processors/AbstractNoticeProcessorTest.kt +++ b/mirai-core/src/commonTest/kotlin/notice/processors/AbstractNoticeProcessorTest.kt @@ -58,7 +58,7 @@ internal abstract class AbstractNoticeProcessorTest : AbstractCommonNHTest(), Gr } protected suspend inline fun use( - attributes: TypeSafeMap = TypeSafeMap(), + attributes: TypeSafeMap = createTypeSafeMap(), pipeline: NoticeProcessorPipeline = bot.components.noticeProcessorPipeline, block: UseTestContext.() -> ProtocolStruct ): Collection<Packet> { @@ -73,7 +73,7 @@ internal abstract class AbstractNoticeProcessorTest : AbstractCommonNHTest(), Gr } protected suspend inline fun use( - attributes: TypeSafeMap = TypeSafeMap(), + attributes: TypeSafeMap = createTypeSafeMap(), crossinline createContext: NoticeProcessorPipelineImpl.(attributes: TypeSafeMap) -> NoticeProcessorPipelineImpl.ContextImpl, block: UseTestContext.() -> ProtocolStruct ): Collection<Packet> =