diff options
| author | Linnea Gräf <nea@nea.moe> | 2024-02-15 01:02:21 +0100 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2024-02-15 01:02:21 +0100 |
| commit | f6d8c3d8599957dc2f5c843d0e1e7243e84d4cb9 (patch) | |
| tree | 118de379d668f078213ddbb770285fff2ce91f47 /src | |
| parent | c3018ca1a64d57dda614a639b04b4e52d2a85616 (diff) | |
| download | SkyHanni-feat/const.tar.gz SkyHanni-feat/const.tar.bz2 SkyHanni-feat/const.zip | |
Turns out value boxing is regretably still a thingfeat/const
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/const/Const.kt | 10 | ||||
| -rw-r--r-- | src/test/java/at/hannibal2/skyhanni/test/const/TestConst.kt | 15 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/const/Const.kt b/src/main/java/at/hannibal2/skyhanni/utils/const/Const.kt index 30d8ef0c6..26e568431 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/const/Const.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/const/Const.kt @@ -80,9 +80,7 @@ inline fun <reified U : T, T> Const<T>.tryCast(): Const<U>? = (unsafeMutable as? * mutable as long. The caller may never cast the returned instance to [MutableList]. */ fun <T> Const<List<T>>.liftList(): List<Const<T>> { - @Suppress("UNCHECKED_CAST") - // This cast is valid since List<T> and List<Const<T>> are always the same at runtime, guaranteed by `@JvmInline` - return unsafeMutable as List<Const<T>> + return unsafeMutable.map(Const.Companion::newUnchecked) } /** @@ -90,8 +88,6 @@ fun <T> Const<List<T>>.liftList(): List<Const<T>> { * either constructed directly as a [List] or if it originally comes from a [MutableList], mutations operations on this * instance are never used. */ -fun <T> List<Const<T>>.unliftList(): Const<List<T>> { - @Suppress("UNCHECKED_CAST") - // This cast is valid since List<T> and List<Const<T>> are always the same at runtime, guaranteed by `@JvmInline` - return Const.newUnchecked(this as List<T>) +fun <T> List<Const<T>>.liftConst(): Const<List<T>> { + return Const.newUnchecked(this.map { it.unsafeMutable }) } diff --git a/src/test/java/at/hannibal2/skyhanni/test/const/TestConst.kt b/src/test/java/at/hannibal2/skyhanni/test/const/TestConst.kt new file mode 100644 index 000000000..3826e09fd --- /dev/null +++ b/src/test/java/at/hannibal2/skyhanni/test/const/TestConst.kt @@ -0,0 +1,15 @@ +package at.hannibal2.skyhanni.test.const + +import at.hannibal2.skyhanni.utils.const.Const +import at.hannibal2.skyhanni.utils.const.liftConst +import at.hannibal2.skyhanni.utils.const.unconst +import org.junit.jupiter.api.Test + +class TestConst { + @Test + fun testConstListLayout() { + val list = listOf(Const.newUnchecked("")) + val liftedList = list.liftConst() + require(liftedList.unsafeMap { it[0] }.unconst == "") + } +} |
