Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/DurationHelper.kt

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