mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-13 06:30:13 +08:00
Add contracts to cast
, safeCast
and castOrNull
; Add uncheckedCast
This commit is contained in:
parent
f90320bd2b
commit
dad2d965ab
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user