Last active
December 6, 2024 01:05
-
-
Save efrohnhoefer/5b8f15b2aca533334c37ce7304fadca6 to your computer and use it in GitHub Desktop.
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
class Test { | |
fun <reified T : Parcelable> ByteString.toParcelable(): T { | |
val parcel = Parcel.obtain() | |
val byteArray = toByteArray() | |
parcel.unmarshall(byteArray, 0, byteArray.size) | |
parcel.setDataPosition(0) | |
val rtn = parcel.readParcelable<T>(null)!! | |
parcel.recycle() | |
return rtn | |
} | |
fun Parcelable.toByteString(): ByteString { | |
val parcel = Parcel.obtain() | |
parcel.writeParcelable(this, 0) | |
val byteArray = parcel.marshall() | |
parcel.recycle() | |
return byteArray.toByteString() | |
} | |
@Parcelize | |
data class MyClass(val data: Int) : Parcelable | |
fun test() { | |
val myClass = MyClass(123).toByteString().toParcelable<MyClass>() | |
assert(myClass.data == 123) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment