Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / kotlin / templates / DurationHelper.kt
blob4237c6f9a8cc1d82c47dad76420ce67b2f73b0a4
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()
7         if (seconds < 0) {
8             throw java.time.DateTimeException("Duration exceeds minimum or maximum value supported by uniffi")
9         }
10         if (nanoseconds < 0) {
11             throw java.time.DateTimeException("Duration nanoseconds exceed minimum or maximum supported by uniffi")
12         }
13         return java.time.Duration.ofSeconds(seconds, nanoseconds)
14     }
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")
23         }
25         if (value.nano < 0) {
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")
29         }
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)
35     }