Closed vhdirk closed 2 weeks ago
Good point! Yes it was not considered before; but this should not be very hard to fix - just modify the parser slightly. Feel free to PR for it, alternatively I will fix it in the next batch or so!
(For completeness) Current workaround: Just use async
keyword.
Btw I am curious, is there a reason to use Future instead of async?
Yes, there is: I want to store the callbacks in a struct for later use:
#[frb(opaque)]
pub struct NFCTransceiver {
pub read_single_block_cb: Box<
dyn Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
>,
pub write_single_block_cb: Box<
dyn Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
>,
pub custom_command_cb: Box<
dyn Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
>,
}
impl NFCTransceiver {
#[frb(sync)]
pub fn new(
read_single_block_cb: impl Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
write_single_block_cb: impl Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
custom_command_cb: impl Fn(u8, u32, Vec<u8>) -> DartFnFuture<NFCResult>
+ 'static
+ Send
+ Sync,
) -> Self {
Self {
read_single_block_cb: Box::new(read_single_block_cb),
write_single_block_cb: Box::new(write_single_block_cb),
custom_command_cb: Box::new(custom_command_cb),
}
}
}
impl NFC for NFCTransceiver {
...
}
And you can't do that with the async keyword
And you can't do that with the async keyword
A bit confused:
pub fn greet(name: String) -> DartFnFuture<Result<String, u32>> {
future::ready(Ok(format!("Hello, {name}!")))
}
pub fn hey(name: String) -> Pin<Box<dyn Future<Output = Result<String, u32>> + Send + 'static>> {
Box::pin(future::ready(Ok(format!("Hello, {name}!"))))
}
pub fn yo(name: String) -> DartFnFuture<String> {
future::ready(format!("Hello, {name}!"))
}
#[frb(sync)]
pub fn hallo(name: String) -> DartFnFuture<String> {
future::ready(format!("Hello, {name}!"))
}
pub async fn rust_function(dart_callback: impl Fn(String) -> DartFnFuture<Result<String, u32>>) {
dart_callback(Ok("Tom".to_owned())).await; // Will get `Hello, Tom!`
}
Where do you want to use them? If I understand correctly, the use case is that,
// dart
NFCTransceiver(read_single_block_cb: greet);
where greet
is a Dart function that is auto generated from the greet
rust function.
Then, in that case, your Rust function just need to return normal things, i.e. pub fn greet() -> Result<String, u32> {}
So does the one above solve your question? If so, I guess there is no need to worry about explicit Future types.
Close because of inactivity, but feel free to reopen if this does not solve your problem!
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.
Describe the bug
Functions that explicitly return a
Future
rather than just beingasync
aren't recognized as async functions. They just take the return type at face value and make it opaque.Steps to reproduce
I made some examples here: https://github.com/vhdirk/flutter_rust_bridge/blob/future-result/frb_example/dart_minimal/rust/src/api/minimal.rs. There are 4 variations:
greet
: return aDartFnFuture<Result<String, u32>>
. Gets translated intoFuture<DartFnFutureResultStringU32>
hey
: unaliasDartFnFuture
, returnPin<Box<dyn Future<Output = Result<String, u32>> + Send + 'static>>
. Gets translated intoFuture<PinBoxFutureOutputResultStringU32>
yo
: simpler return type. just aDartFnFuture<String>
. Translated intoFuture<DartFnFutureString>
frb(sync)
. Translates toDartFnFutureString
.Logs
Expected behavior
Functions returning a
Future
should be interpreted asasync
Generated binding code
OS
Debian Trixie
Version of
flutter_rust_bridge_codegen
master
Flutter info
Version of
clang++
16.0.6
Additional context
No response