diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt index a12e28029..b532bf618 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt @@ -143,7 +143,7 @@ fun MessageChain.firstOrNull(key: Message.Key): M? = when (key) */ @JvmSynthetic @Suppress("UNCHECKED_CAST") -fun MessageChain.first(key: Message.Key): M = +inline fun MessageChain.first(key: Message.Key): M = firstOrNull(key) ?: throw NoSuchElementException("no such element: $key") /** @@ -151,7 +151,7 @@ fun MessageChain.first(key: Message.Key): M = */ @JvmSynthetic @Suppress("UNCHECKED_CAST") -fun MessageChain.any(key: Message.Key): Boolean = firstOrNull(key) != null +inline fun MessageChain.any(key: Message.Key): Boolean = firstOrNull(key) != null // endregion accessors @@ -238,27 +238,27 @@ fun Message.asMessageChain(): MessageChain = when (this) { * 直接将 [this] 委托为一个 [MessageChain] */ @JvmSynthetic -fun Collection.asMessageChain(): MessageChain = MessageChainImplByCollection(this) +inline fun Collection.asMessageChain(): MessageChain = MessageChainImplByCollection(this) /** * 将 [this] [扁平化后][flatten] 委托为一个 [MessageChain] */ @JvmName("newChain") // @JsName("newChain") -fun Collection.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) +inline fun Collection.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) /** * 直接将 [this] 委托为一个 [MessageChain] */ @JvmSynthetic -fun Iterable.asMessageChain(): MessageChain = MessageChainImplByIterable(this) +inline fun Iterable.asMessageChain(): MessageChain = MessageChainImplByIterable(this) @JvmSynthetic inline fun MessageChain.asMessageChain(): MessageChain = this // 避免套娃 @JvmSynthetic fun CombinedMessage.asMessageChain(): MessageChain { - if (left is SingleMessage && this.element is SingleMessage) { + if (left is SingleMessage && this.tail is SingleMessage) { @Suppress("UNCHECKED_CAST") return (this as Iterable).asMessageChain() } @@ -270,20 +270,20 @@ fun CombinedMessage.asMessageChain(): MessageChain { */ @JvmName("newChain") // @JsName("newChain") -fun Iterable.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) +inline fun Iterable.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) /** * 直接将 [this] 委托为一个 [MessageChain] */ @JvmSynthetic -fun Sequence.asMessageChain(): MessageChain = MessageChainImplBySequence(this) +inline fun Sequence.asMessageChain(): MessageChain = MessageChainImplBySequence(this) /** * 将 [this] [扁平化后][flatten] 委托为一个 [MessageChain] */ @JvmName("newChain") // @JsName("newChain") -fun Sequence.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) +inline fun Sequence.asMessageChain(): MessageChain = MessageChainImplBySequence(this.flatten()) /** * 构造一个 [MessageChain] @@ -317,12 +317,12 @@ fun _____newChain______(messages: String): MessageChain { * A <- B <- C <- D <- E <- F <- G * ``` */ -fun Iterable.flatten(): Sequence = asSequence().flatten() +inline fun Iterable.flatten(): Sequence = asSequence().flatten() // @JsName("flatten1") @JvmName("flatten1")// avoid platform declare clash @JvmSynthetic -fun Iterable.flatten(): Sequence = this.asSequence() // fast path +inline fun Iterable.flatten(): Sequence = this.asSequence() // fast path /** * 扁平化消息序列. @@ -336,12 +336,12 @@ fun Iterable.flatten(): Sequence = this.asSequence * A <- B <- C <- D <- E <- F <- G * ``` */ -fun Sequence.flatten(): Sequence = flatMap { it.flatten() } +inline fun Sequence.flatten(): Sequence = flatMap { it.flatten() } @JsName("flatten1") // avoid platform declare clash @JvmName("flatten1") @JvmSynthetic -fun Sequence.flatten(): Sequence = this // fast path +inline fun Sequence.flatten(): Sequence = this // fast path /** * 扁平化 [Message] @@ -366,7 +366,7 @@ fun CombinedMessage.flatten(): Sequence { } else return this.asSequence().flatten() } -fun MessageChain.flatten(): Sequence = this.asSequence() // fast path +inline fun MessageChain.flatten(): Sequence = this.asSequence() // fast path // endregion converters @@ -387,7 +387,7 @@ object NullMessageChain : MessageChain { override fun toString(): String = "NullMessageChain" override fun equals(other: Any?): Boolean = other === this override fun contains(sub: String): Boolean = error("accessing NullMessageChain") - override fun followedBy(tail: Message): CombinedMessage = CombinedMessage(left = EmptyMessageChain, element = tail) + override fun followedBy(tail: Message): CombinedMessage = CombinedMessage(left = EmptyMessageChain, tail = tail) override fun iterator(): MutableIterator = error("accessing NullMessageChain") }