-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add image caches; Fix deserialized image not rendering in `ForwardMes…
- Loading branch information
Showing
9 changed files
with
415 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
mirai-core-utils/src/commonMain/kotlin/UnsafeMutableNonNullProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2019-2021 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/dev/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.utils | ||
|
||
public fun <T : Any> unsafeMutableNonNullPropertyOf( | ||
name: String = "<unknown>" | ||
): UnsafeMutableNonNullProperty<T> { | ||
return UnsafeMutableNonNullProperty(name) | ||
} | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
public class UnsafeMutableNonNullProperty<T : Any>( | ||
private val propertyName: String = "<unknown>" | ||
) { | ||
@JvmField | ||
public var value0: T? = null | ||
|
||
public val isInitialized: Boolean get() = value0 !== null | ||
public var value: T | ||
get() = value0 ?: throw UninitializedPropertyAccessException("Property `$propertyName` not initialized") | ||
set(value) { | ||
value0 = value | ||
} | ||
|
||
public fun clear() { | ||
value0 = null | ||
} | ||
|
||
public inline operator fun getValue(thiz: Any?, property: Any?): T = value | ||
public inline operator fun setValue(thiz: Any?, property: Any?, value: T) { | ||
value0 = value | ||
} | ||
|
||
override fun toString(): String { | ||
return value0?.toString() ?: "<uninitialized>" | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
mirai-core-utils/src/commonTest/kotlin/net/mamoe/mirai/utils/ResourceAccessLockTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2019-2021 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/dev/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.utils | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
internal class ResourceAccessLockTest { | ||
@Test | ||
fun testInitializedLockCannotReInit() { | ||
val lock = ResourceAccessLock() | ||
lock.setToInit() | ||
assertFalse { lock.tryToInit() } | ||
} | ||
|
||
@Test | ||
fun testUseFailedIfLockUninitializedOrLocked() { | ||
val lock = ResourceAccessLock() | ||
lock.setToUninitialized() | ||
assertFalse { lock.tryToUse() } | ||
lock.setToLocked() | ||
assertFalse { lock.tryToUse() } | ||
} | ||
|
||
@Test | ||
fun testLockFailedIfUninitialized() { | ||
val lock = ResourceAccessLock() | ||
lock.setToUninitialized() | ||
assertFalse { lock.lockIfNotUsing() } | ||
} | ||
|
||
@Test | ||
fun testLockFailedIfUsing() { | ||
val lock = ResourceAccessLock() | ||
lock.setToInit() | ||
assertTrue { lock.tryToUse() } | ||
assertFalse { lock.lockIfNotUsing() } | ||
} | ||
|
||
@Test | ||
fun testLockUsedIfInitialized() { | ||
val lock = ResourceAccessLock() | ||
lock.setToInit() | ||
assertTrue { lock.tryToUse() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.