| 1 |
/** |
| 2 |
* The FfiConverter interface handles converter types to and from the FFI |
| 3 |
* |
| 4 |
* All implementing objects should be public to support external types. When a |
| 5 |
* type is external we need to import it's FfiConverter. |
| 6 |
* |
| 7 |
* @suppress |
| 8 |
*/ |
| 9 |
public interface FfiConverter<KotlinType, FfiType> { |
| 10 |
// Convert an FFI type to a Kotlin type |
| 11 |
fun lift(value: FfiType): KotlinType |
| 12 |
|
| 13 |
// Convert an Kotlin type to an FFI type |
| 14 |
fun lower(value: KotlinType): FfiType |
| 15 |
|
| 16 |
// Read a Kotlin type from a `ByteBuffer` |
| 17 |
fun read(buf: ByteBuffer): KotlinType |
| 18 |
|
| 19 |
// Calculate bytes to allocate when creating a `RustBuffer` |
| 20 |
// |
| 21 |
// This must return at least as many bytes as the write() function will |
| 22 |
// write. It can return more bytes than needed, for example when writing |
| 23 |
// Strings we can't know the exact bytes needed until we the UTF-8 |
| 24 |
// encoding, so we pessimistically allocate the largest size possible (3 |
| 25 |
// bytes per codepoint). Allocating extra bytes is not really a big deal |
| 26 |
// because the `RustBuffer` is short-lived. |
| 27 |
fun allocationSize(value: KotlinType): ULong |
| 28 |
|
| 29 |
// Write a Kotlin type to a `ByteBuffer` |
| 30 |
fun write(value: KotlinType, buf: ByteBuffer) |
| 31 |
|
| 32 |
// Lower a value into a `RustBuffer` |
| 33 |
// |
| 34 |
// This method lowers a value into a `RustBuffer` rather than the normal |
| 35 |
// FfiType. It's used by the callback interface code. Callback interface |
| 36 |
// returns are always serialized into a `RustBuffer` regardless of their |
| 37 |
// normal FFI type. |
| 38 |
fun lowerIntoRustBuffer(value: KotlinType): RustBuffer.ByValue { |
| 39 |
val rbuf = RustBuffer.alloc(allocationSize(value)) |
| 40 |
try { |
| 41 |
val bbuf = rbuf.data!!.getByteBuffer(0, rbuf.capacity).also { |
| 42 |
it.order(ByteOrder.BIG_ENDIAN) |
| 43 |
} |
| 44 |
write(value, bbuf) |
| 45 |
rbuf.writeField("len", bbuf.position().toLong()) |
| 46 |
return rbuf |
| 47 |
} catch (e: Throwable) { |
| 48 |
RustBuffer.free(rbuf) |
| 49 |
throw e |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Lift a value from a `RustBuffer`. |
| 54 |
// |
| 55 |
// This here mostly because of the symmetry with `lowerIntoRustBuffer()`. |
| 56 |
// It's currently only used by the `FfiConverterRustBuffer` class below. |
| 57 |
fun liftFromRustBuffer(rbuf: RustBuffer.ByValue): KotlinType { |
| 58 |
val byteBuf = rbuf.asByteBuffer()!! |
| 59 |
try { |
| 60 |
val item = read(byteBuf) |
| 61 |
if (byteBuf.hasRemaining()) { |
| 62 |
throw RuntimeException("junk remaining in buffer after lifting, something is very wrong!!") |
| 63 |
} |
| 64 |
return item |
| 65 |
} finally { |
| 66 |
RustBuffer.free(rbuf) |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* FfiConverter that uses `RustBuffer` as the FfiType |
| 73 |
* |
| 74 |
* @suppress |
| 75 |
*/ |
| 76 |
public interface FfiConverterRustBuffer<KotlinType>: FfiConverter<KotlinType, RustBuffer.ByValue> { |
| 77 |
override fun lift(value: RustBuffer.ByValue) = liftFromRustBuffer(value) |
| 78 |
override fun lower(value: KotlinType) = lowerIntoRustBuffer(value) |
| 79 |
} |