Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/swift/templates/DurationHelper.swift

raw
1 #if swift(>=5.8)
2 @_documentation(visibility: private)
3 #endif
4 fileprivate struct FfiConverterDuration: FfiConverterRustBuffer {
5 typealias SwiftType = TimeInterval
6
7 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> TimeInterval {
8 let seconds: UInt64 = try readInt(&buf)
9 let nanoseconds: UInt32 = try readInt(&buf)
10 return Double(seconds) + (Double(nanoseconds) / 1.0e9)
11 }
12
13 public static func write(_ value: TimeInterval, into buf: inout [UInt8]) {
14 if value.rounded(.down) > Double(Int64.max) {
15 fatalError("Duration overflow, exceeds max bounds supported by Uniffi")
16 }
17
18 if value < 0 {
19 fatalError("Invalid duration, must be non-negative")
20 }
21
22 let seconds = UInt64(value)
23 let nanoseconds = UInt32((value - Double(seconds)) * 1.0e9)
24 writeInt(&buf, seconds)
25 writeInt(&buf, nanoseconds)
26 }
27 }