Add contracts to cast, safeCast and castOrNull; Add uncheckedCast

This commit is contained in:
Him188 2021-06-25 22:08:49 +08:00
parent f90320bd2b
commit dad2d965ab

View File

@ -13,13 +13,27 @@
package net.mamoe.mirai.utils
import java.util.*
import kotlin.contracts.contract
import kotlin.reflect.KClass
public inline fun <reified T> Any?.cast(): T = this as T
public inline fun <reified T> Any?.cast(): T {
contract { returns() implies (this@cast is T) }
return this as T
}
public inline fun <reified T> Any?.safeCast(): T? = this as? T
public inline fun <reified T> Any?.safeCast(): T? {
contract { returnsNotNull() implies (this@safeCast is T) }
return this as? T
}
public inline fun <reified T> Any?.castOrNull(): T? {
contract { returnsNotNull() implies (this@castOrNull is T) }
return this as? T
}
@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
public inline fun <T> Any?.uncheckedCast(): T = this as T
public inline fun <reified T> Any?.castOrNull(): T? = this as? T
public inline fun <reified R> Iterable<*>.firstIsInstanceOrNull(): R? {
for (it in this) {