Skip to content
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

feat: Add support for async/Future #1409

Merged
merged 132 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
bf53393
feat: `FnMetadata` and `MethodMetadata` have an `is_async` attribute.
Hywan Nov 3, 2022
4c450ce
feat: `Function` and `Method` have an `is_async` attribute.
Hywan Nov 3, 2022
efb19ff
feat: Welcome `FfiFuture`.
Hywan Nov 3, 2022
1bf1e84
test: Let's introduce the `future` fixture.
Hywan Nov 3, 2022
505c253
feat: Let's experiment with Python and asyncio.
Hywan Nov 7, 2022
47eb221
feat: Implement our own `Future` API in Python, and continue the Rust…
Hywan Nov 10, 2022
f2ad621
feat: Add scaffolding and ComponentInterface and FFI support for Future.
Hywan Nov 10, 2022
9125e52
feat: Define a new `FfiType::FutureWaker` “type”.
Hywan Nov 10, 2022
ffab20d
feat: Passing `RustFuture` over FFI to be polled.
Hywan Nov 11, 2022
88c12d6
feat: Generate the last glue part.
Hywan Nov 11, 2022
85f010e
chore: Rename an FFI function.
Hywan Nov 11, 2022
2354be5
!debug
Hywan Nov 14, 2022
e3e19d2
!debug
Hywan Nov 14, 2022
18f759a
!cleanup Need a real commit message
Hywan Nov 14, 2022
50ba8c7
feat: Simplify `RustFuture` “padding” in Python.
Hywan Nov 14, 2022
34d87c0
feat: Call Python event_loop with `call_soon_threadsafe.
Hywan Nov 16, 2022
e1f654e
feat: `async` function returns a `Box<RustFuture>`.
Hywan Nov 16, 2022
56c7a0b
feat: Drop the Rust Future from Python.
Hywan Nov 16, 2022
14e36ef
chore: Clean up.
Hywan Nov 17, 2022
ba60a91
feat: Update `RustFuture` to be generic over the `Future`'s `Output`.
Hywan Nov 17, 2022
665cac7
!temp
Hywan Nov 21, 2022
763a9c6
feat: Allow `RustFuture::poll` to return the actual result.
Hywan Nov 21, 2022
b22e3f1
chore: Remove a useless unsafe impl of `Send` for `RustBuffer`.
Hywan Nov 22, 2022
26fd256
feat: Lift the result of a reay future in Python.
Hywan Nov 22, 2022
2ad07c9
feaft: Implement an async waker in Python so that Rust has room to dr…
Hywan Nov 22, 2022
c54212d
chore: `FFIType::(Pointer|Future|FutureWaker)` are no longer necessary.
Hywan Nov 22, 2022
147820f
doc: Write missing documentation.
Hywan Nov 22, 2022
497da07
feat: Use `Arc::increment_strong_count` to replace `ManuallyDrop` + `…
Hywan Nov 22, 2022
92c34bc
doc: Write missing documentation.
Hywan Nov 22, 2022
136af12
feat: The `Future` output does not need to be `Send`
Hywan Nov 23, 2022
3d5c49f
fix: `int` cannot cast a `ctypes.c_byte`, so lets just rely on dynami…
Hywan Nov 23, 2022
b19d621
chore: Remove useless newlines.
Hywan Nov 23, 2022
8c4be6e
test: Adding more async functions!
Hywan Nov 23, 2022
0111cf3
test: Add tests for Python.
Hywan Nov 23, 2022
42163ef
doc: Add examples.
Hywan Nov 23, 2022
e9933d4
test: Add a test for a future that is already ready.
Hywan Nov 23, 2022
0274a58
chore: Rename the examples directory for Python.
Hywan Nov 23, 2022
26f4850
chore: Remove an artifact.
Hywan Nov 23, 2022
4aab503
feat: Make `RustFuture.poll` private.
Hywan Nov 23, 2022
8e56077
feat: Simplify code a little bit.
Hywan Nov 23, 2022
a611ed4
feat: `RustFuture` on Python size is an opaque type.
Hywan Nov 23, 2022
630bc32
feat: Add `async` support for object methods.
Hywan Nov 23, 2022
270e923
test: Test `async` methods.
Hywan Nov 23, 2022
7d7469a
chore: Simplify code by removing a closure.
Hywan Nov 24, 2022
0415db7
feat: The `waker` can receive an environment.
Hywan Nov 24, 2022
e190fdf
feat: Add `async` API in `.h` for Swift.
Hywan Nov 24, 2022
8ddd0f1
!debug
Hywan Nov 30, 2022
3da52d0
feat: First valid Swift implementation that seems to work \o/
Hywan Dec 5, 2022
6b37cf8
feat: Add `async` support object methods for Swift.
Hywan Dec 7, 2022
214f766
chore: Ignore build artifact.
Hywan Dec 7, 2022
c7079c8
chore: Format code.
Hywan Dec 7, 2022
86c83f1
doc: Write proper example for Swift and async.
Hywan Dec 7, 2022
ae85b55
test: Test `async` in Swift.
Hywan Dec 7, 2022
888359b
fix: Add missing nullability type specifier for `…_poll` functions.
Hywan Dec 7, 2022
bf97f58
fix: FFI functions for async code returns an `Option<Box<RustFuture<T…
Hywan Dec 7, 2022
33a6ef3
doc: Improve documentation.
Hywan Dec 7, 2022
2e80dff
doc: Improve documentation of `RustFuture` and other types.
Hywan Dec 12, 2022
96ad445
test: Make the code safer and test it!
Hywan Dec 12, 2022
73798c9
doc: Fix a typo.
Hywan Dec 12, 2022
dad3950
doc: Clarifies some part of the doc.
Hywan Dec 12, 2022
d983837
doc: Clarifies some part of the doc.
Hywan Dec 12, 2022
7ef5ff0
doc: Add comments.
Hywan Dec 12, 2022
35765ab
feat: Simplify the code a little bit.
Hywan Dec 12, 2022
a71df05
feat: First (incomplete) support for async in Kotlin.
Hywan Dec 15, 2022
e7b8c3a
feat: Complete support for async (func only) in Kotlin.
Hywan Dec 16, 2022
c6175ab
doc: Add a better demo.
Hywan Dec 16, 2022
df12c3b
chore: Add missing EOL newline.
Hywan Dec 16, 2022
3d7e723
doc: Mimic the other foreign languages demo.
Hywan Dec 19, 2022
67d7875
feat: Use the current coroutine scope instead of `GlobalScope` to lau…
Hywan Dec 19, 2022
67361b1
chore: Address feedbacks.
Hywan Dec 19, 2022
1eeb899
feat: `RustFutureWakerEnvironment` in Kotlin is now a class.
Hywan Dec 19, 2022
0b44d1f
feat: Support async methods in Kotlin.
Hywan Dec 19, 2022
48ed0bd
test: Install `kotlinx-coroutines-core-jvm.jar`.
Hywan Dec 19, 2022
c5ccd97
test: Add tests for async in Kotlin.
Hywan Dec 21, 2022
9f3d4a7
chore: Simplify an example.
Hywan Dec 21, 2022
c8c20e0
Merge branch 'main' into feat-async-with-fallible
Hywan Dec 21, 2022
212a34d
feat: `RustFuture` can return `()`.
Hywan Dec 21, 2022
39c33b7
feat: `RustFuture` which returns `()` works in Python.
Hywan Dec 21, 2022
8cb8c81
feat: `RustFuture` which returns `()` works in Python.
Hywan Dec 21, 2022
b8b0985
feat: Async function returning `()` works in Swift.
Hywan Dec 21, 2022
b5942dd
test: Async function can return `()` in Swift.
Hywan Dec 21, 2022
248b473
feat: Async method returning `()` works in Swift.
Hywan Dec 22, 2022
d966c84
feat: Async functions returning `()` work in Kotlin.
Hywan Dec 22, 2022
c2a285e
feat: Async methods returning `()` work in Kotlin.
Hywan Dec 22, 2022
215307f
chore: Clean up.
Hywan Dec 22, 2022
cf69bd2
proc-macro: Generalize parsing helpers
jplatte Dec 22, 2022
a163678
proc-macro: Add support for running async fn's in tokio's runtime
jplatte Dec 22, 2022
170c8fe
proc-macro: Stop wrapping &mut Pin<_> in another layer of Pin
jplatte Dec 22, 2022
f982269
doc: Fix intra-link.
Hywan Dec 22, 2022
3f7dd89
feat: Remove `mem::transmute` in `RustFuture`s waker.
Hywan Dec 22, 2022
1cb13f2
Merge pull request #1 from matrix-org/jplatte/async-tokio
Hywan Dec 22, 2022
cafbad4
doc: Fix a comment.
Hywan Dec 22, 2022
082bd68
proc-macro: Silence UnwindSafe error without Mutex
jplatte Dec 22, 2022
f791a89
Merge pull request #2 from matrix-org/jplatte/assert-unwind-safe
Hywan Dec 22, 2022
8e33c07
test: Test an async function that uses Tokio as the async runtime.
Hywan Jan 9, 2023
60fabef
feat: `RustFuture` now always holds a `Result<T, E>` to support to th…
Hywan Jan 11, 2023
ae68a98
feat: Swift support async _`throws`_ function!
Hywan Jan 12, 2023
bc3db63
chore: Oops.
Hywan Jan 12, 2023
2f4308e
feat: Swift supports async _`throws`_ methods!
Hywan Jan 12, 2023
7dfc984
feat: Kotlin supports async _`throws`_ functions and methods!
Hywan Jan 12, 2023
1525f5f
feat: Python supports async _`throws`_ functions and methods!
Hywan Jan 12, 2023
d9a71e4
Merge branch 'main' into feat-async
Hywan Jan 16, 2023
4e07f23
chore: Finalize the merge with `main`.
Hywan Jan 16, 2023
59c13db
chore: Finalize the merge (bis).
Hywan Jan 16, 2023
5c89db3
test: New test case for an async function returning a record.
Hywan Jan 16, 2023
0b15785
doc: Update documentation.
Hywan Jan 16, 2023
1602eef
chore: Add missing EOL newline.
Hywan Jan 16, 2023
71f8760
chore: Add missing EOL newline.
Hywan Jan 16, 2023
1963aca
feat: Resolve the async waker-re-entering problem in Kotlin.
Hywan Jan 19, 2023
50759a0
fix: Use a `Semaphore` to avoid `wake` re-entry.
Hywan Jan 23, 2023
0ab0345
doc: Improve the `README.md` of `fixtures/futures/`.
Hywan Jan 23, 2023
497e335
test: Changes `secs` to `ms` in futures tests.
Hywan Jan 23, 2023
e23ae1c
test: Changes `secs` to `ms` in futures tests.
Hywan Jan 23, 2023
ef5cd68
chore: Clean up empty lines.
Hywan Jan 23, 2023
44118aa
chore: Create the `async_func` and `async_meth` macros in Kotlin.
Hywan Jan 23, 2023
8c9327d
doc: Improve documentation of `RustFuture`.
Hywan Jan 23, 2023
0837d3a
feat: Add generic `FfiDefault` implementation for `Option<T>`.
Hywan Jan 23, 2023
fafbfd8
feat: Remove the `RustFutureForeignWakerEnvironment` type alias.
Hywan Jan 23, 2023
57f8fa0
Merge branch 'main' into feat-async
Hywan Feb 13, 2023
802aeac
chore(fixtures): Use our own bindgen bin.
Hywan Feb 13, 2023
e42febf
doc(uniffi): Use the correct function name.
Hywan Feb 13, 2023
926135a
doc(uniffi-bindgen): Improve a comment.
Hywan Feb 13, 2023
828a6bd
fix: Fix a merge issue.
Hywan Feb 13, 2023
2ab5c10
test: Diable Kotlin future test suite for now.
Hywan Feb 13, 2023
a12768e
Revert "test: Diable Kotlin future test suite for now."
Hywan Feb 14, 2023
0d4e3a6
Only import async-related functionality if there are async founctions
bendk Feb 16, 2023
1701412
fix(docker) Remove a double `-o` in a `curl` command.
Hywan Feb 20, 2023
8d30f0b
test: Disable Kotlin tests for `fixtures/futures/`.
Hywan Feb 20, 2023
53af6b3
chore: Fix code formatting.
Hywan Feb 20, 2023
4d6cd03
test: Disable Swift tests for `fixtures/futures/.
Hywan Feb 20, 2023
cca6274
test: Update Swift to 5.5.
Hywan Feb 20, 2023
1ad12f0
Merge branch 'main' into feat-async
Hywan Feb 27, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ members = [
"fixtures/simple-fns",
"fixtures/simple-iface",
"fixtures/swift-omit-labels",
"fixtures/futures",
]

resolver = "2"
14 changes: 12 additions & 2 deletions docker/Dockerfile-build
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ RUN rm rust-toolchain.toml

RUN mkdir -p /tmp/setup-swift \
&& cd /tmp/setup-swift \
&& curl -o swift.tar.gz https://swift.org/builds/swift-5.2.4-release/ubuntu1804/swift-5.2.4-RELEASE/swift-5.2.4-RELEASE-ubuntu18.04.tar.gz \
&& curl -o swift.tar.gz https://download.swift.org/swift-5.5-release/ubuntu1804/swift-5.5-RELEASE/swift-5.5-RELEASE-ubuntu18.04.tar.gz
# XXX TODO: should check a sha256sum or something here...
&& tar -xzf swift.tar.gz \
&& sudo mv swift-5.2.4-RELEASE-ubuntu18.04 /opt/swift \
&& sudo mv swift-5.5-RELEASE-ubuntu18.04 /opt/swift \
&& echo "export PATH=\"\$PATH:/opt/swift/usr/bin\"" >> /home/circleci/.bashrc \
&& echo "export PATH=\"\$PATH:/opt/swift/usr/bin\"" >> /home/circleci/.profile \
&& cd ../ \
Expand All @@ -75,4 +75,14 @@ RUN mkdir -p /tmp/setup-jna \
&& cd ../ \
&& rm -rf ./setup-jna

RUN mkdir -p /tmp/setup-kotlinx \
&& cd /tmp/setup-kotlinx \
&& curl -o kotlinx-coroutines-core-jvm.jar https://repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.6.4/kotlinx-coroutines-core-jvm-1.6.4.jar \
# XXX TODO: should check a sha256sum or something here...
Hywan marked this conversation as resolved.
Show resolved Hide resolved
&& sudo mv kotlinx-coroutines-core-jvm.jar /opt \
&& echo "export CLASSPATH=\"\$CLASSPATH:/opt/kotlinx-coroutines-core-jvm.jar\"" >> /home/circleci/.bashrc \
&& echo "export CLASSPATH=\"\$CLASSPATH:/opt/kotlinx-coroutines-core-jvm.jar\"" >> /home/circleci/.profile \
&& cd ../ \
&& rm -rf ./setup-kotlinx

RUN sudo gem install ffi --no-document
1 change: 1 addition & 0 deletions docker/Dockerfile-build.checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
df97bf2200a9af87bd773ec620ec7ebd605cf5ef65a9bc3101cbb0b385d02d447584b17e2bc8a3e8601ed3e5dfb00e903286dece6bfa5885f24c9bbfac1663fb Dockerfile-build
25 changes: 25 additions & 0 deletions fixtures/futures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "uniffi-fixture-futures"
version = "0.21.0"
authors = ["Ivan Enderlin <ivan@mnt.io>"]
edition = "2021"
license = "MPL-2.0"
publish = false

[lib]
name = "uniffi_futures"
crate-type = ["lib", "cdylib"]

[[bin]]
name = "uniffi-fixtures-futures"
path = "src/bin.rs"

[dependencies]
uniffi = { path = "../../uniffi", features = ["tokio", "cli"] }
tokio = { version = "1.24.1", features = ["time"] }

[build-dependencies]
uniffi = { path = "../../uniffi", features = ["build"] }

[dev-dependencies]
uniffi = { path = "../../uniffi", features = ["bindgen-tests"] }
39 changes: 39 additions & 0 deletions fixtures/futures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# A basic test for uniffi components

This test covers async functions and methods. It also provides examples.

## Run the tests

Simply use `cargo`:

```sh
$ cargo test
```

It is possible to filter by test names, like `cargo test -- swift` to only run
Swift's tests.

## Run the examples

At the time of writing, each `examples/*` directory has a `Makefile`. They are
mostly designed for Unix-ish systems, sorry for that.

To run the examples, first `uniffi` must be compiled:

```sh
$ cargo build --release -p uniffi`
```

Then, each `Makefile` has 2 targets: `build` and `run`:

```sh
$ # Build the examples.
$ make build
$
$ # Run the example.
$ make run
```

One note for `examples/kotlin/`, some JAR files must be present, so please
run `make install-jar` first: It will just download the appropriated JAR files
directly inside the directory from Maven.
3 changes: 3 additions & 0 deletions fixtures/futures/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::generate_scaffolding("./src/uniffi_futures.udl").unwrap();
}
4 changes: 4 additions & 0 deletions fixtures/futures/examples/kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*.jar
/uniffi
/libuniffi_futures.dylib
/*.log
13 changes: 13 additions & 0 deletions fixtures/futures/examples/kotlin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
install-jar:
curl https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.8.0/jna-5.8.0.jar -o jna.jar
curl https://repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.6.4/kotlinx-coroutines-core-jvm-1.6.4.jar -o kotlinx-coroutines-core-jvm.jar

build:
cargo build --release
../../../../target/release/uniffi-fixtures-futures generate --lib-file ../../../../target/release/libuniffi_futures.dylib ../../src/uniffi_futures.udl --language kotlin --out-dir .
rm -f libuniffi_futures.dylib
ln -s ../../../../target/release/libuniffi_futures.dylib libuniffi_futures.dylib
kotlinc -cp kotlinx-coroutines-core-jvm.jar:jna.jar -include-runtime -d demo.jar uniffi/fixture/futures/*.kt demo.kt

run:
java -cp demo.jar:kotlinx-coroutines-core-jvm.jar:jna.jar my.demo.DemoKt
41 changes: 41 additions & 0 deletions fixtures/futures/examples/kotlin/demo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package my.demo

import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.system.*
import uniffi.fixture.futures.*

fun main() = runBlocking {
println("Let's start!\n")

println("Wait 2secs before greeting you, dear public!")

var time = measureTimeMillis {
val result = sayAfter(2000U, "You")
println("result: ${result}")
}

println("[in ${time / 1000.toDouble()}sec]")

println("\nWouah, 'tired. Let's sleep for 3secs!")

time = measureTimeMillis {
sleep(3000U)
}

println("[in ${time / 1000.toDouble()}sec]")

println("\nIs it really blocking? Nah. Let's greet Alice and Bob after resp. 2secs and 3secs _concurrently_!")

time = measureTimeMillis {
val alice = async { sayAfter(2000U, "Alice") }
val bob = async { sayAfter(3000U, "Bob") }

println("alice: ${alice.await()}")
println("bob: ${bob.await()}")
}

println("[in ${time / 1000.toDouble()}sec]")

println("\nSee, it took 3secs, not 5secs!")
}
3 changes: 3 additions & 0 deletions fixtures/futures/examples/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
libuniffi_futures.*
uniffi_futures.py
8 changes: 8 additions & 0 deletions fixtures/futures/examples/python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build:
cargo build --release
../../../../target/release/uniffi-fixtures-futures generate --lib-file ../../../../target/release/libuniffi_futures.dylib ../../src/uniffi_futures.udl --language python --out-dir .
rm -f libuniffi_futures.dylib
ln -s ../../../../target/release/libuniffi_futures.dylib libuniffi_futures.dylib

run:
python3 demo.py
37 changes: 37 additions & 0 deletions fixtures/futures/examples/python/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from uniffi_futures import sleep, say_after, void
import asyncio
import time

def show_time():
print(f"[time {time.strftime('%X')}]")

async def main():
print("Let's start!\n")

print('Wait 2secs before greeting you, dear public!\n')

show_time()
result = await say_after(2000, 'You')
print(f'result: {result}')
show_time()

print("\nWouah, 'tired. Let's sleep for 3secs!\n")

show_time()
await sleep(3000)
show_time()

print("\nIs it really blocking? Nah. Let's greet Alice and Bob after resp. 2secs and 3secs _concurrently_!\n")

alice = asyncio.create_task(say_after(2000, 'Alice'))
bob = asyncio.create_task(say_after(3000, 'Bob'))
show_time()
result_alice = await alice
result_bob = await bob
print(f'result_alice: {result_alice}')
print(f'result_bob: {result_bob}')

show_time()
print("\nSee, it took 3secs, not 5secs!")

asyncio.run(main())
2 changes: 2 additions & 0 deletions fixtures/futures/examples/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.build
uniffi_futures*
19 changes: 19 additions & 0 deletions fixtures/futures/examples/swift/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
build:
cargo build --release
../../../../target/release/uniffi-fixtures-futures generate --lib-file ../../../../target/release/libuniffi_futures.dylib ../../src/uniffi_futures.udl --language swift --out-dir ../../../../target/release/
swiftc \
-module-name uniffi_futures \
-emit-library -o ../../../../target/release/libuniffi_futures.dylib \
-emit-module -emit-module-path ../../../../target/release/ \
-parse-as-library \
-L ../../../../target/release/ \
-luniffi_futures \
-Xcc -fmodule-map-file=../../../../target/release/uniffi_futuresFFI.modulemap \
../../../../target/release/uniffi_futures.swift
# There is no equivalent to `-Xcc -fmodule-map-file` in `Package.swift`. The
# only solution is to rename the modulemap to… `module.modulemap`, and boom,
# magic.
cp ../../../../target/release/uniffi_futuresFFI.modulemap ../../../../target/release/module.modulemap

run:
swift run
22 changes: 22 additions & 0 deletions fixtures/futures/examples/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "demo",
platforms: [.macOS(.v12)],
dependencies: [],
targets: [
.executableTarget(
name: "demo",
dependencies: [],
swiftSettings: [
.unsafeFlags(["-I", "../../../../target/release"])
],
linkerSettings: [
.linkedLibrary("uniffi_futures"),
.unsafeFlags(["-L../../../../target/release/"])
]),
]
)
42 changes: 42 additions & 0 deletions fixtures/futures/examples/swift/Sources/demo/Demo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import uniffi_futures
import Foundation // to get `Date` and `DateFormatter`

func showTime() {
let now = Date()
let formatter = DateFormatter()
formatter.timeStyle = .medium
print("[time \(formatter.string(from: now))]")
}

@main
struct Testing {
static func main() async throws {
print("Let's start!\n")

print("Wait 2secs before greeting you, dear public!\n")

showTime()
let result = await sayAfter(ms: 2000, who: "You")
print("result: \(result)")
showTime()

print("\nWouha, 'tired. Let's sleep for 3secs!\n")

showTime()
let _ = await sleep(ms: 3000)
showTime()

print("\nIs it really blocking? Nah. Let's greet Alice and Bob after resp. 2secs and 3secs _concurrently_!\n")

async let alice = sayAfter(ms: 2000, who: "Alice")
async let bob = sayAfter(ms: 3000, who: "Bob")

showTime()
let (result_alice, result_bob) = await (alice, bob)
print("result_alice: \(result_alice)")
print("result_bob: \(result_bob)")
showTime()

print("\nSee, it tooks 3secs, not 5secs!\n")
}
}
3 changes: 3 additions & 0 deletions fixtures/futures/src/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::uniffi_bindgen_main()
}
Loading