Skip to content

Commit

Permalink
finalize Duration support
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed May 20, 2024
1 parent 0c243f4 commit 102dcb0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
19 changes: 4 additions & 15 deletions fixtures/duration_type_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@ use core::time::Duration;
use uniffi;

#[uniffi::export]
pub fn add_duration(seconds: i64, nanos: i32) -> Duration {
Duration::new(seconds as u64, nanos as u32)
pub fn make_duration(seconds: u64, nanos: u32) -> Duration {
Duration::new(seconds, nanos)
}

#[uniffi::export]
pub fn get_seconds(duration: Duration) -> u64 {
duration.as_secs() as u64
duration.as_secs()
}

#[uniffi::export]
pub fn get_nanos(duration: Duration) -> u32 {
duration.as_nanos() as u32 % 1_000_000_000
duration.subsec_nanos()
}

// #[cfg(test)]
// mod tests {
// use super::*;

// #[test]
// fn test_add_duration() {
// assert_eq!(get_seconds(add_duration(1, 1)), 1);
// assert_eq!(get_nanos(add_duration(1, 1)), 1);
// }
// }

uniffi::include_scaffolding!("api");
32 changes: 29 additions & 3 deletions fixtures/duration_type_test/test/duration_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ import '../duration_type_test.dart';

void main() {
final api = Api.load();
final duration = api.addDuration(1, 1);
expect(api.getSeconds(duration), equals(1));
expect(api.getNanos(duration), equals(1));
test('rust return value seconds check', () {
final duration = api.makeDuration(5, 0);

expect(duration.inSeconds, 5);
expect(api.getSeconds(duration), 5);
expect(api.getNanos(duration), 0);
});

test('seconds data check from dart', () {
final duration = Duration(seconds: 10);
expect(api.getSeconds(duration), 10);
expect(api.getNanos(duration), 0);
});

test('check nanos/micros', () {
final duration = api.makeDuration(0, 3000);
expect(duration.inSeconds, 0);
expect(duration.inMicroseconds, 3);
expect(api.getSeconds(duration), 0);
expect(api.getNanos(duration), 3000);
});

test('check large values', () {
final duration = api.makeDuration(123456789, 3000000);
expect(duration.inSeconds, 123456789);
expect(duration.inMicroseconds, 123456789003000);
expect(api.getSeconds(duration), 123456789);
expect(api.getNanos(duration), 3000000);
});
}
29 changes: 15 additions & 14 deletions src/gen/primitives/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,34 @@ impl Renderable for DurationCodeType {
quote! {
class FfiConverterDuration {
static Duration lift(Api api, RustBuffer buf) {
final bufList = buf.asUint8List();
final seconds = bufList.buffer.asByteData(bufList.offsetInBytes).getInt64(0);
final nanoseconds = bufList.buffer.asByteData(bufList.offsetInBytes).getInt64(8);
return Duration(seconds: seconds, microseconds: nanoseconds ~/ 1000);
return FfiConverterDuration.read(api, buf.asUint8List()).value;
}

static RustBuffer lower(Api api, Duration value) {
final buf = Uint8List(16);
buf.buffer.asByteData(buf.offsetInBytes).setInt64(0, value.inSeconds);
buf.buffer.asByteData(buf.offsetInBytes).setInt64(8, value.inMicroseconds * 1000);
final buf = Uint8List(12);
FfiConverterDuration.write(api, value, buf);
return toRustBuffer(api, buf);
}

static LiftRetVal<Duration> read(Api api, Uint8List buf) {
final seconds = buf.buffer.asByteData(buf.offsetInBytes).getInt64(0);
final nanoseconds = buf.buffer.asByteData(buf.offsetInBytes).getInt64(8);
return LiftRetVal(Duration(seconds: seconds, microseconds: nanoseconds ~/ 1000), 16);
final bytes = buf.buffer.asByteData(buf.offsetInBytes, 12);
final seconds = bytes.getUint64(0);
final micros = (bytes.getUint32(8) ~/ 1000);
return LiftRetVal(Duration(seconds: seconds, microseconds: micros), 12);
}

static int allocationSize([Duration value = const Duration()]) {
return 16;
return 12;
}

static int write(Api api, Duration value, Uint8List buf) {
buf.buffer.asByteData(buf.offsetInBytes).setInt64(0, value.inSeconds);
buf.buffer.asByteData(buf.offsetInBytes).setInt64(8, value.inMicroseconds * 1000);
return 16;
final bytes = buf.buffer.asByteData(buf.offsetInBytes, 12);
bytes.setUint64(0, value.inSeconds);
final ms = (value.inMicroseconds - (value.inSeconds * 1000000)) * 1000;
if (ms > 0) {
bytes.setUint32(8, ms.toInt());
}
return 12;
}
}
}
Expand Down

0 comments on commit 102dcb0

Please sign in to comment.