Skip to content

Commit

Permalink
Merge pull request #37 from chavic/main
Browse files Browse the repository at this point in the history
UniFFI Dart Binding Generator Refactor and Added Async Support
  • Loading branch information
chavic authored Aug 25, 2024
2 parents c7dc633 + 6f7baac commit 46f9f7b
Show file tree
Hide file tree
Showing 26 changed files with 746 additions and 1,055 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
rust:
- stable
- "1.74"
- "1.75"
- nightly
steps:
- name: Checkout sources
Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
matrix:
rust:
- stable
- "1.74"
- "1.75"
- nightly
steps:
- name: Checkout sources
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Dart frontend for UniFFI bindings

Reference: [TODOs](./TODO.md)

## MSRV: 1.74
## MSRV: 1.75

This project must always work on latest stable rust + version before. We are also testing it against 1.1.70.0 , which we consider the Minimum Support Rust Version (MSRV) at this point. Rust lower than that will probably not compile the project.

Expand Down
41 changes: 20 additions & 21 deletions fixtures/arithmetic/test/simple_arithmetic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,67 @@ import 'package:test/test.dart';
import '../simple_arithmetic.dart';

void main() {
final api = Api.load();
test('2 + 2 = 4', () {
expect(api.add(2, 2), 4);
expect(add(2, 2), 4);
});
test('2 * 8 = 16', () {
expect(api.multiply(2, 8), 16);
expect(multiply(2, 8), 16);
});
test('2 / 8 = 0', () {
expect(api.divideChecked(2, 8), 0);
expect(divideChecked(2, 8), 0);
});
test('8 / 0 = null', () {
expect(api.divideChecked(8, 0), null);
expect(divideChecked(8, 0), null);
});
test('8 / 2 = 4', () {
expect(api.divide(8, 2), 4);
expect(divide(8, 2), 4);
});
test('u8', () {
expect(api.addU8(2, 2), 4);
expect(addU8(2, 2), 4);
});
test('u16', () {
expect(api.addU16(2, 2), 4);
expect(addU16(2, 2), 4);
});
test('u64', () {
expect(api.addU64(2, 2), 4);
expect(addU64(2, 2), 4);
});

test('i8', () {
expect(api.addI8(2, 2), 4);
expect(addI8(2, 2), 4);
});
test('i16', () {
expect(api.addI16(2, 2), 4);
expect(addI16(2, 2), 4);
});
test('i32', () {
expect(api.addI32(2, 2), 4);
expect(addI32(2, 2), 4);
});
test('i64', () {
expect(api.addI64(2, 2), 4);
expect(addI64(2, 2), 4);
});
test('f32', () {
expect(api.addF32(2.0, 2.0), 4.0);
expect(addF32(2.0, 2.0), 4.0);
});
test('f64', () {
expect(api.addF64(2.0, 2.9), 4.9);
expect(addF64(2.0, 2.9), 4.9);
});

test('get back u8', () {
expect(api.getBackU8(4), 4);
expect(getBackU8(4), 4);
});
test('get back u16', () {
expect(api.getBackU16(4), 4);
expect(getBackU16(4), 4);
});
test('get back u64', () {
expect(api.getBackU64(4), 4);
expect(getBackU64(4), 4);
});

test('get back i8', () {
expect(api.getBackI8(4), 4);
expect(getBackI8(4), 4);
});
test('get back f32', () {
expect(api.getBackF32(4.0), 4.0);
expect(getBackF32(4.0), 4.0);
});
test('get back f64', () {
expect(api.getBackF64(4.9), 4.9);
expect(getBackF64(4.9), 4.9);
});
}
23 changes: 11 additions & 12 deletions fixtures/duration_type_test/test/duration_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@ import 'package:test/test.dart';
import '../duration_type_test.dart';

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

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

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

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

test('check large values', () {
final duration = api.makeDuration(123456789, 3000000);
final duration = makeDuration(123456789, 3000000);
expect(duration.inSeconds, 123456789);
expect(duration.inMicroseconds, 123456789003000);
expect(api.getSeconds(duration), 123456789);
expect(api.getNanos(duration), 3000000);
expect(getSeconds(duration), 123456789);
expect(getNanos(duration), 3000000);
});
}
1 change: 1 addition & 0 deletions fixtures/futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ crate-type = ["lib", "cdylib"]
[dependencies]
uniffi = { workspace = true, features = ["tokio"]}
tokio = { version = "1.24.1", features = ["time"] }
thiserror = "1.0"

[build-dependencies]
uniffi-dart = { path = "../../", features = ["build"] }
Expand Down
34 changes: 17 additions & 17 deletions fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl TimerFuture {
}
}

// /// Non-blocking timer future.
/// Non-blocking timer future.
pub struct BrokenTimerFuture {
shared_state: Arc<Mutex<SharedState>>,
}
Expand Down Expand Up @@ -152,29 +152,29 @@ pub async fn sleep(ms: u16) -> bool {
}

// Our error.
// #[derive(uniffi::Error, Debug)]
// pub enum MyError {
// Foo,
// }

// // An async function that can throw.
// // An async function that can throw.
// #[uniffi::export]
// pub async fn fallible_me(do_fail: bool) -> Result<u8, MyError> {
// if do_fail {
// Err(MyError::Foo)
// } else {
// Ok(42)
// }
// }
#[derive(thiserror::Error, uniffi::Error, Debug)]
pub enum MyError {
#[error("Foo")]
Foo,
}

// An async function that can throw.
#[uniffi::export]
pub async fn fallible_me(do_fail: bool) -> Result<u8, MyError> {
if do_fail {
Err(MyError::Foo)
} else {
Ok(42)
}
}

#[uniffi::export(async_runtime = "tokio")]
pub async fn say_after_with_tokio(ms: u16, who: String) -> String {
tokio::time::sleep(Duration::from_millis(ms.into())).await;
format!("Hello, {who} (with Tokio)!")
}

#[derive(uniffi::Record, Clone)]
#[derive(uniffi::Record)]
pub struct MyRecord {
pub a: String,
pub b: u32,
Expand Down
Loading

0 comments on commit 46f9f7b

Please sign in to comment.