diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt index 95e45b76d..3495a9c31 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt @@ -111,9 +111,13 @@ open class LockFreeLinkedList { } } - open fun peekFirst(): E = head.nextNode.letValueIfValid { return it } ?: throw NoSuchElementException() - - open fun peekLast(): E = head.iterateBeforeFirst { it === tail }.letValueIfValid { return it } ?: throw NoSuchElementException() + open fun peekFirst(): E { + return head + .iterateBeforeFirst { it === tail } + .takeUnless { it.isTail() } + ?.nodeValue + ?: throw NoSuchElementException() + } open fun removeLast(): E { while (true) { @@ -770,11 +774,7 @@ open class LockFreeLinkedListNode( internal val nextNodeRef: AtomicRef> = atomic(nextNode ?: this) inline fun letValueIfValid(block: (E) -> R): R? { - if (!this.isValidElementNode()) { - return null - } - val value = this.nodeValue - return if (value !== null) block(value) else null + return this.takeIf { isValidElementNode() }?.nodeValue?.let(block) } /**