-
Notifications
You must be signed in to change notification settings - Fork 257
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
Returning a Future
is not translated into an async Dart function (but can use return types directly without wrapping with a Future)
#1927
Comments
Future
is not translated into an async Dart function
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 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 |
A bit confused:
Where do you want to use them? If I understand correctly, the use case is that, // dart
NFCTransceiver(read_single_block_cb: greet); where Then, in that case, your Rust function just need to return normal things, i.e. |
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! |
Future
is not translated into an async Dart functionFuture
is not translated into an async Dart function (but can use return types directly without wrapping with a Future)
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
The text was updated successfully, but these errors were encountered: