| 1 |
// This is a helper for safely working with byte buffers returned from the Rust code. |
| 2 |
// A rust-owned buffer is represented by its capacity, its current length, and a |
| 3 |
// pointer to the underlying data. |
| 4 |
|
| 5 |
/** |
| 6 |
* @suppress |
| 7 |
*/ |
| 8 |
@Structure.FieldOrder("capacity", "len", "data") |
| 9 |
open class RustBuffer : Structure() { |
| 10 |
// Note: `capacity` and `len` are actually `ULong` values, but JVM only supports signed values. |
| 11 |
// When dealing with these fields, make sure to call `toULong()`. |
| 12 |
@JvmField var capacity: Long = 0 |
| 13 |
@JvmField var len: Long = 0 |
| 14 |
@JvmField var data: Pointer? = null |
| 15 |
|
| 16 |
class ByValue: RustBuffer(), Structure.ByValue |
| 17 |
class ByReference: RustBuffer(), Structure.ByReference |
| 18 |
|
| 19 |
internal fun setValue(other: RustBuffer) { |
| 20 |
capacity = other.capacity |
| 21 |
len = other.len |
| 22 |
data = other.data |
| 23 |
} |
| 24 |
|
| 25 |
companion object { |
| 26 |
internal fun alloc(size: ULong = 0UL) = uniffiRustCall() { status -> |
| 27 |
// Note: need to convert the size to a `Long` value to make this work with JVM. |
| 28 |
UniffiLib.INSTANCE.{{ ci.ffi_rustbuffer_alloc().name() }}(size.toLong(), status) |
| 29 |
}.also { |
| 30 |
if(it.data == null) { |
| 31 |
throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})") |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
internal fun create(capacity: ULong, len: ULong, data: Pointer?): RustBuffer.ByValue { |
| 36 |
var buf = RustBuffer.ByValue() |
| 37 |
buf.capacity = capacity.toLong() |
| 38 |
buf.len = len.toLong() |
| 39 |
buf.data = data |
| 40 |
return buf |
| 41 |
} |
| 42 |
|
| 43 |
internal fun free(buf: RustBuffer.ByValue) = uniffiRustCall() { status -> |
| 44 |
UniffiLib.INSTANCE.{{ ci.ffi_rustbuffer_free().name() }}(buf, status) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
@Suppress("TooGenericExceptionThrown") |
| 49 |
fun asByteBuffer() = |
| 50 |
this.data?.getByteBuffer(0, this.len.toLong())?.also { |
| 51 |
it.order(ByteOrder.BIG_ENDIAN) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* The equivalent of the `*mut RustBuffer` type. |
| 57 |
* Required for callbacks taking in an out pointer. |
| 58 |
* |
| 59 |
* Size is the sum of all values in the struct. |
| 60 |
* |
| 61 |
* @suppress |
| 62 |
*/ |
| 63 |
class RustBufferByReference : ByReference(16) { |
| 64 |
/** |
| 65 |
* Set the pointed-to `RustBuffer` to the given value. |
| 66 |
*/ |
| 67 |
fun setValue(value: RustBuffer.ByValue) { |
| 68 |
// NOTE: The offsets are as they are in the C-like struct. |
| 69 |
val pointer = getPointer() |
| 70 |
pointer.setLong(0, value.capacity) |
| 71 |
pointer.setLong(8, value.len) |
| 72 |
pointer.setPointer(16, value.data) |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get a `RustBuffer.ByValue` from this reference. |
| 77 |
*/ |
| 78 |
fun getValue(): RustBuffer.ByValue { |
| 79 |
val pointer = getPointer() |
| 80 |
val value = RustBuffer.ByValue() |
| 81 |
value.writeField("capacity", pointer.getLong(0)) |
| 82 |
value.writeField("len", pointer.getLong(8)) |
| 83 |
value.writeField("data", pointer.getLong(16)) |
| 84 |
|
| 85 |
return value |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// This is a helper for safely passing byte references into the rust code. |
| 90 |
// It's not actually used at the moment, because there aren't many things that you |
| 91 |
// can take a direct pointer to in the JVM, and if we're going to copy something |
| 92 |
// then we might as well copy it into a `RustBuffer`. But it's here for API |
| 93 |
// completeness. |
| 94 |
|
| 95 |
@Structure.FieldOrder("len", "data") |
| 96 |
internal open class ForeignBytes : Structure() { |
| 97 |
@JvmField var len: Int = 0 |
| 98 |
@JvmField var data: Pointer? = null |
| 99 |
|
| 100 |
class ByValue : ForeignBytes(), Structure.ByValue |
| 101 |
} |