1 public object FfiConverterDuration: FfiConverterRustBuffer<java.time.Duration> {
2 override fun read(buf: ByteBuffer): java.time.Duration {
3 // Type mismatch (should be u64) but we check for overflow/underflow below
4 val seconds = buf.getLong()
5 // Type mismatch (should be u32) but we check for overflow/underflow below
6 val nanoseconds = buf.getInt().toLong()
8 throw java.time.DateTimeException("Duration exceeds minimum or maximum value supported by uniffi")
10 if (nanoseconds < 0) {
11 throw java.time.DateTimeException("Duration nanoseconds exceed minimum or maximum supported by uniffi")
13 return java.time.Duration.ofSeconds(seconds, nanoseconds)
16 // 8 bytes for seconds, 4 bytes for nanoseconds
17 override fun allocationSize(value: java.time.Duration) = 12
19 override fun write(value: java.time.Duration, buf: ByteBuffer) {
20 if (value.seconds < 0) {
21 // Rust does not support negative Durations
22 throw IllegalArgumentException("Invalid duration, must be non-negative")
26 // Java docs provide guarantee that nano will always be positive, so this should be impossible
27 // See: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
28 throw IllegalArgumentException("Invalid duration, nano value must be non-negative")
31 // Type mismatch (should be u64) but since Rust doesn't support negative durations we should be OK
32 buf.putLong(value.seconds)
33 // Type mismatch (should be u32) but since values will always be between 0 and 999,999,999 it should be OK
34 buf.putInt(value.nano)