Skip to content

Commit c29e7fd

Browse files
committed
feat: introduce KotlinInstant type for Kotlin bindings
1 parent 81ef371 commit c29e7fd

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

crypto-ffi/bindings/android/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation(kotlin("stdlib-jdk7"))
2121
implementation("${libs.jna.get()}@aar")
2222
implementation(libs.coroutines.core)
23+
implementation(libs.kotlinx.datetime)
2324
implementation("androidx.annotation:annotation:1.9.1")
2425

2526
androidTestImplementation(kotlin("test"))

crypto-ffi/bindings/gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[versions]
22
kotlin = "1.9.25"
33
coroutines = "1.7.3"
4+
kotlinx-datetime = "0.6.1"
45
jna = "5.17.0"
56
assertj = "3.24.2"
67
espresso = "3.6.1"
@@ -29,3 +30,4 @@ espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "esp
2930
android-junit = { module = "androidx.test.ext:junit", version.ref = "android-junit" }
3031
android-tools = { module = "com.android.tools.build:gradle", version.ref = "android-tools" }
3132
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin-gradle" }
33+
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }

crypto-ffi/bindings/jvm/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation(kotlin("stdlib-jdk7"))
2121
implementation(libs.jna)
2222
implementation(libs.coroutines.core)
23+
implementation(libs.kotlinx.datetime)
2324
testImplementation(kotlin("test"))
2425
testImplementation(libs.coroutines.test)
2526
testImplementation(libs.assertj.core)

crypto-ffi/src/timestamp.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Custom timestamp type for UniFFI that maps to kotlinx.datetime.Instant in Kotlin
2+
/// the reason behind it is to unify the timestamp mapping between the JVM/Android bindings
3+
/// and the new Kotlin Multiplatform bindings.
4+
/// this can be removed once we fully migrate to Kotlin Multiplatform and
5+
/// stop generating JVM/Android bindings.
6+
7+
#[cfg(not(target_family = "wasm"))]
8+
use std::time::{Duration, SystemTime};
9+
10+
#[cfg(not(target_family = "wasm"))]
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12+
pub struct KotlinInstant(pub SystemTime);
13+
14+
#[cfg(not(target_family = "wasm"))]
15+
impl KotlinInstant {
16+
17+
pub fn new(time: SystemTime) -> Self {
18+
Self(time)
19+
}
20+
21+
pub fn from_epoch_secs(secs: u64) -> Self {
22+
Self(SystemTime::UNIX_EPOCH + Duration::from_secs(secs))
23+
}
24+
25+
pub fn into_inner(self) -> SystemTime {
26+
self.0
27+
}
28+
}
29+
30+
#[cfg(not(target_family = "wasm"))]
31+
impl From<SystemTime> for KotlinInstant {
32+
fn from(time: SystemTime) -> Self {
33+
Self(time)
34+
}
35+
}
36+
37+
#[cfg(not(target_family = "wasm"))]
38+
impl From<KotlinInstant> for SystemTime {
39+
fn from(instant: KotlinInstant) -> Self {
40+
instant.0
41+
}
42+
}
43+
44+
#[cfg(not(target_family = "wasm"))]
45+
uniffi::custom_type!(KotlinInstant, SystemTime);

crypto-ffi/uniffi-android.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
[bindings.kotlin]
22
android = true
33
android_cleaner = true
4+
5+
[bindings.kotlin.custom_types.KotlinInstant]
6+
imports = [
7+
"kotlinx.datetime.Instant",
8+
"kotlinx.datetime.toKotlinInstant",
9+
"kotlinx.datetime.toJavaInstant",
10+
]
11+
type_name = "Instant"
12+
lift = "{}.toKotlinInstant()"
13+
lower = "{}.toJavaInstant()"

crypto-ffi/uniffi.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
package_name = "com.wire.crypto"
33
cdylib_name = "core_crypto_ffi"
44

5+
[bindings.kotlin.custom_types.KotlinInstant]
6+
imports = [
7+
"kotlinx.datetime.Instant",
8+
"kotlinx.datetime.toKotlinInstant",
9+
"kotlinx.datetime.toJavaInstant",
10+
]
11+
type_name = "Instant"
12+
lift = "{}.toKotlinInstant()"
13+
lower = "{}.toJavaInstant()"
14+
515
[bindings.swift]
616
cdylib_name = "core_crypto_ffi"
717
ffi_module_name = "LibCoreCrypto"

0 commit comments

Comments
 (0)