Skip to content

Support Timestamp and Duration types #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/manual/src/internals/lifting_and_lowering.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Calling this function from foreign language code involves the following steps:
| `f64`/`double` | `double` |
| `boolean` | `int8_t`, either `0` or `1` |
| `string` | `RustBuffer` struct pointing to utf8 bytes |
| `timestamp` | `RustBuffer` struct pointing to a u64 representing seconds and a u32 representing nanoseconds |
| `timestamp` | `RustBuffer` struct pointing to a i64 representing seconds and a u32 representing nanoseconds |
| `duration` | `RustBuffer` struct pointing to a u64 representing seconds and a u32 representing nanoseconds |
| `T?` | `RustBuffer` struct pointing to serialized bytes |
| `sequence<T>` | `RustBuffer` struct pointing to serialized bytes |
Expand Down
16 changes: 16 additions & 0 deletions uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,17 @@ unsafe impl ViaFfi for String {
/// Support for passing timestamp values via the FFI.
///
/// Timestamps values are currently always passed by serializing to a buffer.
///
/// Timestamps are represented on the buffer by an i64 that indicates the
/// direction and the magnitude in seconds of the offset from epoch, and a
/// u32 that indicates the nanosecond portion of the offset magnitude. The
/// nanosecond portion is expected to be between 0 and 999,999,999.
///
/// To build an epoch offset the absolute value of the seconds portion of the
/// offset should be combined with the nanosecond portion. This is because
/// the sign of the seconds portion represents the direction of the offset
/// overall. The sign of the seconds portion can then be used to determine
/// if the total offset should be added to or subtracted from the unix epoch.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks so much for adding these!

unsafe impl ViaFfi for SystemTime {
type FfiType = RustBuffer;

Expand Down Expand Up @@ -406,6 +417,11 @@ unsafe impl ViaFfi for SystemTime {
/// Support for passing duration values via the FFI.
///
/// Duration values are currently always passed by serializing to a buffer.
///
/// Durations are represented on the buffer by a u64 that indicates the
/// magnitude in seconds, and a u32 that indicates the nanosecond portion
/// of the magnitude. The nanosecond portion is expected to be between 0
/// and 999,999,999.
unsafe impl ViaFfi for Duration {
type FfiType = RustBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,30 +228,33 @@ extension Bool: ViaFfi {
extension Date: ViaFfiUsingByteBuffer, ViaFfi {
fileprivate static func read(from buf: Reader) throws -> Self {
let seconds: Int64 = try buf.readInt()
let microseconds: UInt32 = try buf.readInt()
let nanoseconds: UInt32 = try buf.readInt()
if seconds >= 0 {
let delta = Double(seconds) + (Double(microseconds) / 1.0e9)
let delta = Double(seconds) + (Double(nanoseconds) / 1.0e9)
return Date.init(timeIntervalSince1970: delta)
} else {
let delta = Double(-seconds) + (Double(microseconds) / 1.0e9)
return Date.init(timeIntervalSince1970: -delta)
let delta = Double(seconds) - (Double(nanoseconds) / 1.0e9)
return Date.init(timeIntervalSince1970: delta)
}
}

fileprivate func write(into buf: Writer) {
var delta = self.timeIntervalSince1970
var sign: Int64 = 1
if delta < 0 {
// The nanoseconds portion of the epoch offset must always be
// positive, to simplify the calculation we will use the absolute
// value of the offset.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

sign = -1
delta = -delta
}
if delta.rounded(.down) > Double(Int64.max) {
fatalError("Timestamp overflow, exceeds max bounds supported by Uniffi")
}
let seconds = Int64(delta)
let microseconds = UInt32((delta - Double(seconds)) * 1.0e9)
let nanoseconds = UInt32((delta - Double(seconds)) * 1.0e9)
buf.writeInt(sign * seconds)
buf.writeInt(microseconds)
buf.writeInt(nanoseconds)
}
}

Expand All @@ -274,8 +277,8 @@ extension TimeInterval {

fileprivate static func readDuration(from buf: Reader) throws -> Self {
let seconds: UInt64 = try buf.readInt()
let microseconds: UInt32 = try buf.readInt()
return Double(seconds) + (Double(microseconds) / 1.0e9)
let nanoseconds: UInt32 = try buf.readInt()
return Double(seconds) + (Double(nanoseconds) / 1.0e9)
}

fileprivate func writeDuration(into buf: Writer) {
Expand All @@ -288,9 +291,9 @@ extension TimeInterval {
}

let seconds = UInt64(self)
let microseconds = UInt32((self - Double(seconds)) * 1.0e9)
let nanoseconds = UInt32((self - Double(seconds)) * 1.0e9)
buf.writeInt(seconds)
buf.writeInt(microseconds)
buf.writeInt(nanoseconds)
}
}

Expand Down