From 2e8b4c48760724bba4957c663441721fc8571e5c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 14:56:22 +0800 Subject: [PATCH 01/12] chore: test --- .../lib/src/rust/api/minimal.dart | 5 + .../lib/src/rust/frb_generated.dart | 201 +++++++++++++++++- .../lib/src/rust/frb_generated.io.dart | 65 ++++++ .../lib/src/rust/frb_generated.web.dart | 65 ++++++ .../dart_minimal/rust/src/api/minimal.rs | 4 + .../dart_minimal/rust/src/frb_generated.rs | 176 ++++++++++++++- .../dart_minimal/test/minimal_test.dart | 7 + 7 files changed, 521 insertions(+), 2 deletions(-) diff --git a/frb_example/dart_minimal/lib/src/rust/api/minimal.dart b/frb_example/dart_minimal/lib/src/rust/api/minimal.dart index db2ee24315..bc90291ef5 100644 --- a/frb_example/dart_minimal/lib/src/rust/api/minimal.dart +++ b/frb_example/dart_minimal/lib/src/rust/api/minimal.dart @@ -8,3 +8,8 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; Future minimalAdder({required int a, required int b}) => RustLib.instance.api.crateApiMinimalMinimalAdder(a: a, b: b); + +Future rustFunction( + {required FutureOr Function(String) dartCallback}) => + RustLib.instance.api + .crateApiMinimalRustFunction(dartCallback: dartCallback); diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart index 1e36e2b0e8..3c9db735ee 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart @@ -58,7 +58,7 @@ class RustLib extends BaseEntrypoint { String get codegenVersion => '2.0.0'; @override - int get rustContentHash => -2119384465; + int get rustContentHash => -957330701; static const kDefaultExternalLibraryLoaderConfig = ExternalLibraryLoaderConfig( @@ -72,6 +72,9 @@ abstract class RustLibApi extends BaseApi { Future crateApiMinimalInitApp(); Future crateApiMinimalMinimalAdder({required int a, required int b}); + + Future crateApiMinimalRustFunction( + {required FutureOr Function(String) dartCallback}); } class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @@ -131,46 +134,242 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: ["a", "b"], ); + @override + Future crateApiMinimalRustFunction( + {required FutureOr Function(String) dartCallback}) { + return handler.executeNormal(NormalTask( + callFfi: (port_) { + final serializer = SseSerializer(generalizedFrbRustBinding); + sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + dartCallback, serializer); + pdeCallFfi(generalizedFrbRustBinding, serializer, + funcId: 3, port: port_); + }, + codec: SseCodec( + decodeSuccessData: sse_decode_unit, + decodeErrorData: null, + ), + constMeta: kCrateApiMinimalRustFunctionConstMeta, + argValues: [dartCallback], + apiImpl: this, + )); + } + + TaskConstMeta get kCrateApiMinimalRustFunctionConstMeta => + const TaskConstMeta( + debugName: "rust_function", + argNames: ["dartCallback"], + ); + + Future Function(int, dynamic) + encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) raw) { + return (callId, rawArg0) async { + final arg0 = dco_decode_String(rawArg0); + + Box? rawOutput; + Box? rawError; + try { + rawOutput = Box(await raw(arg0)); + } catch (e, s) { + rawError = Box(AnyhowException("$e\n\n$s")); + } + + final serializer = SseSerializer(generalizedFrbRustBinding); + assert((rawOutput != null) ^ (rawError != null)); + if (rawOutput != null) { + serializer.buffer.putUint8(0); + sse_encode_String(rawOutput.value, serializer); + } else { + serializer.buffer.putUint8(1); + sse_encode_AnyhowException(rawError!.value, serializer); + } + final output = serializer.intoRaw(); + + generalizedFrbRustBinding.dartFnDeliverOutput( + callId: callId, + ptr: output.ptr, + rustVecLen: output.rustVecLen, + dataLen: output.dataLen); + }; + } + + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return AnyhowException(raw as String); + } + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + throw UnimplementedError(''); + } + + @protected + Object dco_decode_DartOpaque(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return decodeDartOpaque(raw, generalizedFrbRustBinding); + } + + @protected + String dco_decode_String(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return raw as String; + } + @protected int dco_decode_i_32(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return raw as int; } + @protected + Uint8List dco_decode_list_prim_u_8_strict(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return raw as Uint8List; + } + + @protected + int dco_decode_u_8(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return raw as int; + } + @protected void dco_decode_unit(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return; } + @protected + BigInt dco_decode_usize(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return dcoDecodeU64(raw); + } + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var inner = sse_decode_String(deserializer); + return AnyhowException(inner); + } + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var inner = sse_decode_usize(deserializer); + return decodeDartOpaque(inner, generalizedFrbRustBinding); + } + + @protected + String sse_decode_String(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var inner = sse_decode_list_prim_u_8_strict(deserializer); + return utf8.decoder.convert(inner); + } + @protected int sse_decode_i_32(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs return deserializer.buffer.getInt32(); } + @protected + Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var len_ = sse_decode_i_32(deserializer); + return deserializer.buffer.getUint8List(len_); + } + + @protected + int sse_decode_u_8(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + return deserializer.buffer.getUint8(); + } + @protected void sse_decode_unit(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs } + @protected + BigInt sse_decode_usize(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + return deserializer.buffer.getBigUint64(); + } + @protected bool sse_decode_bool(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs return deserializer.buffer.getUint8() != 0; } + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_String(self.message, serializer); + } + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_DartOpaque( + encode_DartFn_Inputs_String_Output_String_AnyhowException(self), + serializer); + } + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_usize( + BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque( + self, portManager.dartHandlerPort, generalizedFrbRustBinding))) + .toUnsigned(64), + serializer); + } + + @protected + void sse_encode_String(String self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_list_prim_u_8_strict(utf8.encoder.convert(self), serializer); + } + @protected void sse_encode_i_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs serializer.buffer.putInt32(self); } + @protected + void sse_encode_list_prim_u_8_strict( + Uint8List self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_i_32(self.length, serializer); + serializer.buffer.putUint8List(self); + } + + @protected + void sse_encode_u_8(int self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + serializer.buffer.putUint8(self); + } + @protected void sse_encode_unit(void self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs } + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + serializer.buffer.putBigUint64(self); + } + @protected void sse_encode_bool(bool self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart index ba09d9752d..c7b624eee0 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart @@ -18,27 +18,92 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw); + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw); + + @protected + Object dco_decode_DartOpaque(dynamic raw); + + @protected + String dco_decode_String(dynamic raw); + @protected int dco_decode_i_32(dynamic raw); + @protected + Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); + + @protected + int dco_decode_u_8(dynamic raw); + @protected void dco_decode_unit(dynamic raw); + @protected + BigInt dco_decode_usize(dynamic raw); + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer); + + @protected + String sse_decode_String(SseDeserializer deserializer); + @protected int sse_decode_i_32(SseDeserializer deserializer); + @protected + Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); + + @protected + int sse_decode_u_8(SseDeserializer deserializer); + @protected void sse_decode_unit(SseDeserializer deserializer); + @protected + BigInt sse_decode_usize(SseDeserializer deserializer); + @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer); + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer); + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer); + + @protected + void sse_encode_String(String self, SseSerializer serializer); + @protected void sse_encode_i_32(int self, SseSerializer serializer); + @protected + void sse_encode_list_prim_u_8_strict( + Uint8List self, SseSerializer serializer); + + @protected + void sse_encode_u_8(int self, SseSerializer serializer); + @protected void sse_encode_unit(void self, SseSerializer serializer); + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer); + @protected void sse_encode_bool(bool self, SseSerializer serializer); } diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart index aa2362f15d..78c426e779 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart @@ -20,27 +20,92 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw); + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw); + + @protected + Object dco_decode_DartOpaque(dynamic raw); + + @protected + String dco_decode_String(dynamic raw); + @protected int dco_decode_i_32(dynamic raw); + @protected + Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); + + @protected + int dco_decode_u_8(dynamic raw); + @protected void dco_decode_unit(dynamic raw); + @protected + BigInt dco_decode_usize(dynamic raw); + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer); + + @protected + String sse_decode_String(SseDeserializer deserializer); + @protected int sse_decode_i_32(SseDeserializer deserializer); + @protected + Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); + + @protected + int sse_decode_u_8(SseDeserializer deserializer); + @protected void sse_decode_unit(SseDeserializer deserializer); + @protected + BigInt sse_decode_usize(SseDeserializer deserializer); + @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer); + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer); + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer); + + @protected + void sse_encode_String(String self, SseSerializer serializer); + @protected void sse_encode_i_32(int self, SseSerializer serializer); + @protected + void sse_encode_list_prim_u_8_strict( + Uint8List self, SseSerializer serializer); + + @protected + void sse_encode_u_8(int self, SseSerializer serializer); + @protected void sse_encode_unit(void self, SseSerializer serializer); + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer); + @protected void sse_encode_bool(bool self, SseSerializer serializer); } diff --git a/frb_example/dart_minimal/rust/src/api/minimal.rs b/frb_example/dart_minimal/rust/src/api/minimal.rs index 517e29973f..05205c1732 100644 --- a/frb_example/dart_minimal/rust/src/api/minimal.rs +++ b/frb_example/dart_minimal/rust/src/api/minimal.rs @@ -8,3 +8,7 @@ pub fn init_app() { pub fn minimal_adder(a: i32, b: i32) -> i32 { a + b } + +pub async fn rust_function(dart_callback: impl Fn(String) -> flutter_rust_bridge::DartFnFuture) { + dart_callback("Tom".to_owned()).await; // Will get `Hello, Tom!` +} \ No newline at end of file diff --git a/frb_example/dart_minimal/rust/src/frb_generated.rs b/frb_example/dart_minimal/rust/src/frb_generated.rs index 62dd178b09..78ed20b06b 100644 --- a/frb_example/dart_minimal/rust/src/frb_generated.rs +++ b/frb_example/dart_minimal/rust/src/frb_generated.rs @@ -37,7 +37,7 @@ flutter_rust_bridge::frb_generated_boilerplate!( default_rust_auto_opaque = RustAutoOpaqueMoi, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0"; -pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -2119384465; +pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -957330701; // Section: executor @@ -114,9 +114,108 @@ fn wire__crate__api__minimal__minimal_adder_impl( }, ) } +fn wire__crate__api__minimal__rust_function_impl( + port_: flutter_rust_bridge::for_generated::MessagePort, + ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, + rust_vec_len_: i32, + data_len_: i32, +) { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "rust_function", + port: Some(port_), + mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, + }, + move || { + let message = unsafe { + flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( + ptr_, + rust_vec_len_, + data_len_, + ) + }; + let mut deserializer = + flutter_rust_bridge::for_generated::SseDeserializer::new(message); + let api_dart_callback = decode_DartFn_Inputs_String_Output_String_AnyhowException( + ::sse_decode(&mut deserializer), + ); + deserializer.end(); + move |context| async move { + transform_result_sse::<_, ()>( + (move || async move { + let output_ok = Result::<_, ()>::Ok({ + crate::api::minimal::rust_function(api_dart_callback).await; + })?; + Ok(output_ok) + })() + .await, + ) + } + }, + ) +} + +// Section: related_funcs + +fn decode_DartFn_Inputs_String_Output_String_AnyhowException( + dart_opaque: flutter_rust_bridge::DartOpaque, +) -> impl Fn(String) -> flutter_rust_bridge::DartFnFuture { + use flutter_rust_bridge::IntoDart; + + async fn body(dart_opaque: flutter_rust_bridge::DartOpaque, arg0: String) -> String { + let args = vec![arg0.into_into_dart().into_dart()]; + let message = FLUTTER_RUST_BRIDGE_HANDLER + .dart_fn_invoke(dart_opaque, args) + .await; + + let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message); + let action = deserializer.cursor.read_u8().unwrap(); + let ans = match action { + 0 => std::result::Result::Ok(::sse_decode(&mut deserializer)), + 1 => std::result::Result::Err( + ::sse_decode(&mut deserializer), + ), + _ => unreachable!(), + }; + deserializer.end(); + let ans = ans.expect("Dart throws exception but Rust side assume it is not failable"); + ans + } + + move |arg0: String| { + flutter_rust_bridge::for_generated::convert_into_dart_fn_future(body( + dart_opaque.clone(), + arg0, + )) + } +} // Section: dart2rust +impl SseDecode for flutter_rust_bridge::for_generated::anyhow::Error { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut inner = ::sse_decode(deserializer); + return flutter_rust_bridge::for_generated::anyhow::anyhow!("{}", inner); + } +} + +impl SseDecode for flutter_rust_bridge::DartOpaque { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut inner = ::sse_decode(deserializer); + return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) }; + } +} + +impl SseDecode for String { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut inner = >::sse_decode(deserializer); + return String::from_utf8(inner).unwrap(); + } +} + impl SseDecode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -124,11 +223,37 @@ impl SseDecode for i32 { } } +impl SseDecode for Vec { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut len_ = ::sse_decode(deserializer); + let mut ans_ = vec![]; + for idx_ in 0..len_ { + ans_.push(::sse_decode(deserializer)); + } + return ans_; + } +} + +impl SseDecode for u8 { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + deserializer.cursor.read_u8().unwrap() + } +} + impl SseDecode for () { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {} } +impl SseDecode for usize { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + deserializer.cursor.read_u64::().unwrap() as _ + } +} + impl SseDecode for bool { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -147,6 +272,7 @@ fn pde_ffi_dispatcher_primary_impl( match func_id { 1 => wire__crate__api__minimal__init_app_impl(port, ptr, rust_vec_len, data_len), 2 => wire__crate__api__minimal__minimal_adder_impl(port, ptr, rust_vec_len, data_len), + 3 => wire__crate__api__minimal__rust_function_impl(port, ptr, rust_vec_len, data_len), _ => unreachable!(), } } @@ -165,6 +291,27 @@ fn pde_ffi_dispatcher_sync_impl( // Section: rust2dart +impl SseEncode for flutter_rust_bridge::for_generated::anyhow::Error { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(format!("{:?}", self), serializer); + } +} + +impl SseEncode for flutter_rust_bridge::DartOpaque { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(self.encode(), serializer); + } +} + +impl SseEncode for String { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + >::sse_encode(self.into_bytes(), serializer); + } +} + impl SseEncode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -172,11 +319,38 @@ impl SseEncode for i32 { } } +impl SseEncode for Vec { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(self.len() as _, serializer); + for item in self { + ::sse_encode(item, serializer); + } + } +} + +impl SseEncode for u8 { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + serializer.cursor.write_u8(self).unwrap(); + } +} + impl SseEncode for () { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {} } +impl SseEncode for usize { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + serializer + .cursor + .write_u64::(self as _) + .unwrap(); + } +} + impl SseEncode for bool { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { diff --git a/frb_example/dart_minimal/test/minimal_test.dart b/frb_example/dart_minimal/test/minimal_test.dart index 9592272552..408e10234a 100644 --- a/frb_example/dart_minimal/test/minimal_test.dart +++ b/frb_example/dart_minimal/test/minimal_test.dart @@ -16,4 +16,11 @@ Future main() async { print('Action: Call rust (after)'); }); print('Action: Configure tests (end)'); + + test('temp', () async { + await rustFunction(dartCallback: (name) { + print("From rust: $name"); + return "Hello, $name"; + }); + }); } From 6a5f38ff295302692877072b4071e6d6794cefd7 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 14:59:07 +0800 Subject: [PATCH 02/12] Revert "chore: test" This reverts commit 2e8b4c48760724bba4957c663441721fc8571e5c. --- .../lib/src/rust/api/minimal.dart | 5 - .../lib/src/rust/frb_generated.dart | 201 +----------------- .../lib/src/rust/frb_generated.io.dart | 65 ------ .../lib/src/rust/frb_generated.web.dart | 65 ------ .../dart_minimal/rust/src/api/minimal.rs | 4 - .../dart_minimal/rust/src/frb_generated.rs | 176 +-------------- .../dart_minimal/test/minimal_test.dart | 7 - 7 files changed, 2 insertions(+), 521 deletions(-) diff --git a/frb_example/dart_minimal/lib/src/rust/api/minimal.dart b/frb_example/dart_minimal/lib/src/rust/api/minimal.dart index bc90291ef5..db2ee24315 100644 --- a/frb_example/dart_minimal/lib/src/rust/api/minimal.dart +++ b/frb_example/dart_minimal/lib/src/rust/api/minimal.dart @@ -8,8 +8,3 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; Future minimalAdder({required int a, required int b}) => RustLib.instance.api.crateApiMinimalMinimalAdder(a: a, b: b); - -Future rustFunction( - {required FutureOr Function(String) dartCallback}) => - RustLib.instance.api - .crateApiMinimalRustFunction(dartCallback: dartCallback); diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart index 3c9db735ee..1e36e2b0e8 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart @@ -58,7 +58,7 @@ class RustLib extends BaseEntrypoint { String get codegenVersion => '2.0.0'; @override - int get rustContentHash => -957330701; + int get rustContentHash => -2119384465; static const kDefaultExternalLibraryLoaderConfig = ExternalLibraryLoaderConfig( @@ -72,9 +72,6 @@ abstract class RustLibApi extends BaseApi { Future crateApiMinimalInitApp(); Future crateApiMinimalMinimalAdder({required int a, required int b}); - - Future crateApiMinimalRustFunction( - {required FutureOr Function(String) dartCallback}); } class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @@ -134,242 +131,46 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: ["a", "b"], ); - @override - Future crateApiMinimalRustFunction( - {required FutureOr Function(String) dartCallback}) { - return handler.executeNormal(NormalTask( - callFfi: (port_) { - final serializer = SseSerializer(generalizedFrbRustBinding); - sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - dartCallback, serializer); - pdeCallFfi(generalizedFrbRustBinding, serializer, - funcId: 3, port: port_); - }, - codec: SseCodec( - decodeSuccessData: sse_decode_unit, - decodeErrorData: null, - ), - constMeta: kCrateApiMinimalRustFunctionConstMeta, - argValues: [dartCallback], - apiImpl: this, - )); - } - - TaskConstMeta get kCrateApiMinimalRustFunctionConstMeta => - const TaskConstMeta( - debugName: "rust_function", - argNames: ["dartCallback"], - ); - - Future Function(int, dynamic) - encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) raw) { - return (callId, rawArg0) async { - final arg0 = dco_decode_String(rawArg0); - - Box? rawOutput; - Box? rawError; - try { - rawOutput = Box(await raw(arg0)); - } catch (e, s) { - rawError = Box(AnyhowException("$e\n\n$s")); - } - - final serializer = SseSerializer(generalizedFrbRustBinding); - assert((rawOutput != null) ^ (rawError != null)); - if (rawOutput != null) { - serializer.buffer.putUint8(0); - sse_encode_String(rawOutput.value, serializer); - } else { - serializer.buffer.putUint8(1); - sse_encode_AnyhowException(rawError!.value, serializer); - } - final output = serializer.intoRaw(); - - generalizedFrbRustBinding.dartFnDeliverOutput( - callId: callId, - ptr: output.ptr, - rustVecLen: output.rustVecLen, - dataLen: output.dataLen); - }; - } - - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return AnyhowException(raw as String); - } - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - throw UnimplementedError(''); - } - - @protected - Object dco_decode_DartOpaque(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return decodeDartOpaque(raw, generalizedFrbRustBinding); - } - - @protected - String dco_decode_String(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return raw as String; - } - @protected int dco_decode_i_32(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return raw as int; } - @protected - Uint8List dco_decode_list_prim_u_8_strict(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return raw as Uint8List; - } - - @protected - int dco_decode_u_8(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return raw as int; - } - @protected void dco_decode_unit(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return; } - @protected - BigInt dco_decode_usize(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return dcoDecodeU64(raw); - } - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var inner = sse_decode_String(deserializer); - return AnyhowException(inner); - } - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var inner = sse_decode_usize(deserializer); - return decodeDartOpaque(inner, generalizedFrbRustBinding); - } - - @protected - String sse_decode_String(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var inner = sse_decode_list_prim_u_8_strict(deserializer); - return utf8.decoder.convert(inner); - } - @protected int sse_decode_i_32(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs return deserializer.buffer.getInt32(); } - @protected - Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var len_ = sse_decode_i_32(deserializer); - return deserializer.buffer.getUint8List(len_); - } - - @protected - int sse_decode_u_8(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - return deserializer.buffer.getUint8(); - } - @protected void sse_decode_unit(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs } - @protected - BigInt sse_decode_usize(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - return deserializer.buffer.getBigUint64(); - } - @protected bool sse_decode_bool(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs return deserializer.buffer.getUint8() != 0; } - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_String(self.message, serializer); - } - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_DartOpaque( - encode_DartFn_Inputs_String_Output_String_AnyhowException(self), - serializer); - } - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_usize( - BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque( - self, portManager.dartHandlerPort, generalizedFrbRustBinding))) - .toUnsigned(64), - serializer); - } - - @protected - void sse_encode_String(String self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_list_prim_u_8_strict(utf8.encoder.convert(self), serializer); - } - @protected void sse_encode_i_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs serializer.buffer.putInt32(self); } - @protected - void sse_encode_list_prim_u_8_strict( - Uint8List self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_i_32(self.length, serializer); - serializer.buffer.putUint8List(self); - } - - @protected - void sse_encode_u_8(int self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - serializer.buffer.putUint8(self); - } - @protected void sse_encode_unit(void self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs } - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - serializer.buffer.putBigUint64(self); - } - @protected void sse_encode_bool(bool self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart index c7b624eee0..ba09d9752d 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart @@ -18,92 +18,27 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw); - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw); - - @protected - Object dco_decode_DartOpaque(dynamic raw); - - @protected - String dco_decode_String(dynamic raw); - @protected int dco_decode_i_32(dynamic raw); - @protected - Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); - - @protected - int dco_decode_u_8(dynamic raw); - @protected void dco_decode_unit(dynamic raw); - @protected - BigInt dco_decode_usize(dynamic raw); - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer); - - @protected - String sse_decode_String(SseDeserializer deserializer); - @protected int sse_decode_i_32(SseDeserializer deserializer); - @protected - Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); - - @protected - int sse_decode_u_8(SseDeserializer deserializer); - @protected void sse_decode_unit(SseDeserializer deserializer); - @protected - BigInt sse_decode_usize(SseDeserializer deserializer); - @protected bool sse_decode_bool(SseDeserializer deserializer); - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer); - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer); - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer); - - @protected - void sse_encode_String(String self, SseSerializer serializer); - @protected void sse_encode_i_32(int self, SseSerializer serializer); - @protected - void sse_encode_list_prim_u_8_strict( - Uint8List self, SseSerializer serializer); - - @protected - void sse_encode_u_8(int self, SseSerializer serializer); - @protected void sse_encode_unit(void self, SseSerializer serializer); - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer); - @protected void sse_encode_bool(bool self, SseSerializer serializer); } diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart index 78c426e779..aa2362f15d 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart @@ -20,92 +20,27 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw); - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw); - - @protected - Object dco_decode_DartOpaque(dynamic raw); - - @protected - String dco_decode_String(dynamic raw); - @protected int dco_decode_i_32(dynamic raw); - @protected - Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); - - @protected - int dco_decode_u_8(dynamic raw); - @protected void dco_decode_unit(dynamic raw); - @protected - BigInt dco_decode_usize(dynamic raw); - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer); - - @protected - String sse_decode_String(SseDeserializer deserializer); - @protected int sse_decode_i_32(SseDeserializer deserializer); - @protected - Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); - - @protected - int sse_decode_u_8(SseDeserializer deserializer); - @protected void sse_decode_unit(SseDeserializer deserializer); - @protected - BigInt sse_decode_usize(SseDeserializer deserializer); - @protected bool sse_decode_bool(SseDeserializer deserializer); - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer); - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer); - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer); - - @protected - void sse_encode_String(String self, SseSerializer serializer); - @protected void sse_encode_i_32(int self, SseSerializer serializer); - @protected - void sse_encode_list_prim_u_8_strict( - Uint8List self, SseSerializer serializer); - - @protected - void sse_encode_u_8(int self, SseSerializer serializer); - @protected void sse_encode_unit(void self, SseSerializer serializer); - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer); - @protected void sse_encode_bool(bool self, SseSerializer serializer); } diff --git a/frb_example/dart_minimal/rust/src/api/minimal.rs b/frb_example/dart_minimal/rust/src/api/minimal.rs index 05205c1732..517e29973f 100644 --- a/frb_example/dart_minimal/rust/src/api/minimal.rs +++ b/frb_example/dart_minimal/rust/src/api/minimal.rs @@ -8,7 +8,3 @@ pub fn init_app() { pub fn minimal_adder(a: i32, b: i32) -> i32 { a + b } - -pub async fn rust_function(dart_callback: impl Fn(String) -> flutter_rust_bridge::DartFnFuture) { - dart_callback("Tom".to_owned()).await; // Will get `Hello, Tom!` -} \ No newline at end of file diff --git a/frb_example/dart_minimal/rust/src/frb_generated.rs b/frb_example/dart_minimal/rust/src/frb_generated.rs index 78ed20b06b..62dd178b09 100644 --- a/frb_example/dart_minimal/rust/src/frb_generated.rs +++ b/frb_example/dart_minimal/rust/src/frb_generated.rs @@ -37,7 +37,7 @@ flutter_rust_bridge::frb_generated_boilerplate!( default_rust_auto_opaque = RustAutoOpaqueMoi, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0"; -pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -957330701; +pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -2119384465; // Section: executor @@ -114,108 +114,9 @@ fn wire__crate__api__minimal__minimal_adder_impl( }, ) } -fn wire__crate__api__minimal__rust_function_impl( - port_: flutter_rust_bridge::for_generated::MessagePort, - ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, - rust_vec_len_: i32, - data_len_: i32, -) { - FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::( - flutter_rust_bridge::for_generated::TaskInfo { - debug_name: "rust_function", - port: Some(port_), - mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, - }, - move || { - let message = unsafe { - flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( - ptr_, - rust_vec_len_, - data_len_, - ) - }; - let mut deserializer = - flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let api_dart_callback = decode_DartFn_Inputs_String_Output_String_AnyhowException( - ::sse_decode(&mut deserializer), - ); - deserializer.end(); - move |context| async move { - transform_result_sse::<_, ()>( - (move || async move { - let output_ok = Result::<_, ()>::Ok({ - crate::api::minimal::rust_function(api_dart_callback).await; - })?; - Ok(output_ok) - })() - .await, - ) - } - }, - ) -} - -// Section: related_funcs - -fn decode_DartFn_Inputs_String_Output_String_AnyhowException( - dart_opaque: flutter_rust_bridge::DartOpaque, -) -> impl Fn(String) -> flutter_rust_bridge::DartFnFuture { - use flutter_rust_bridge::IntoDart; - - async fn body(dart_opaque: flutter_rust_bridge::DartOpaque, arg0: String) -> String { - let args = vec![arg0.into_into_dart().into_dart()]; - let message = FLUTTER_RUST_BRIDGE_HANDLER - .dart_fn_invoke(dart_opaque, args) - .await; - - let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let action = deserializer.cursor.read_u8().unwrap(); - let ans = match action { - 0 => std::result::Result::Ok(::sse_decode(&mut deserializer)), - 1 => std::result::Result::Err( - ::sse_decode(&mut deserializer), - ), - _ => unreachable!(), - }; - deserializer.end(); - let ans = ans.expect("Dart throws exception but Rust side assume it is not failable"); - ans - } - - move |arg0: String| { - flutter_rust_bridge::for_generated::convert_into_dart_fn_future(body( - dart_opaque.clone(), - arg0, - )) - } -} // Section: dart2rust -impl SseDecode for flutter_rust_bridge::for_generated::anyhow::Error { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut inner = ::sse_decode(deserializer); - return flutter_rust_bridge::for_generated::anyhow::anyhow!("{}", inner); - } -} - -impl SseDecode for flutter_rust_bridge::DartOpaque { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut inner = ::sse_decode(deserializer); - return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) }; - } -} - -impl SseDecode for String { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut inner = >::sse_decode(deserializer); - return String::from_utf8(inner).unwrap(); - } -} - impl SseDecode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -223,37 +124,11 @@ impl SseDecode for i32 { } } -impl SseDecode for Vec { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut len_ = ::sse_decode(deserializer); - let mut ans_ = vec![]; - for idx_ in 0..len_ { - ans_.push(::sse_decode(deserializer)); - } - return ans_; - } -} - -impl SseDecode for u8 { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - deserializer.cursor.read_u8().unwrap() - } -} - impl SseDecode for () { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {} } -impl SseDecode for usize { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - deserializer.cursor.read_u64::().unwrap() as _ - } -} - impl SseDecode for bool { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -272,7 +147,6 @@ fn pde_ffi_dispatcher_primary_impl( match func_id { 1 => wire__crate__api__minimal__init_app_impl(port, ptr, rust_vec_len, data_len), 2 => wire__crate__api__minimal__minimal_adder_impl(port, ptr, rust_vec_len, data_len), - 3 => wire__crate__api__minimal__rust_function_impl(port, ptr, rust_vec_len, data_len), _ => unreachable!(), } } @@ -291,27 +165,6 @@ fn pde_ffi_dispatcher_sync_impl( // Section: rust2dart -impl SseEncode for flutter_rust_bridge::for_generated::anyhow::Error { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(format!("{:?}", self), serializer); - } -} - -impl SseEncode for flutter_rust_bridge::DartOpaque { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(self.encode(), serializer); - } -} - -impl SseEncode for String { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - >::sse_encode(self.into_bytes(), serializer); - } -} - impl SseEncode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -319,38 +172,11 @@ impl SseEncode for i32 { } } -impl SseEncode for Vec { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(self.len() as _, serializer); - for item in self { - ::sse_encode(item, serializer); - } - } -} - -impl SseEncode for u8 { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - serializer.cursor.write_u8(self).unwrap(); - } -} - impl SseEncode for () { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {} } -impl SseEncode for usize { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - serializer - .cursor - .write_u64::(self as _) - .unwrap(); - } -} - impl SseEncode for bool { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { diff --git a/frb_example/dart_minimal/test/minimal_test.dart b/frb_example/dart_minimal/test/minimal_test.dart index 408e10234a..9592272552 100644 --- a/frb_example/dart_minimal/test/minimal_test.dart +++ b/frb_example/dart_minimal/test/minimal_test.dart @@ -16,11 +16,4 @@ Future main() async { print('Action: Call rust (after)'); }); print('Action: Configure tests (end)'); - - test('temp', () async { - await rustFunction(dartCallback: (name) { - print("From rust: $name"); - return "Hello, $name"; - }); - }); } From 7f7efe577757a7adb8b5752cb354d7c81c5c392c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 14:59:52 +0800 Subject: [PATCH 03/12] chore: test2 --- frb_example/flutter_via_create/lib/main.dart | 6 + .../lib/src/rust/api/simple.dart | 4 + .../lib/src/rust/frb_generated.dart | 142 +++++++++++++++++- .../lib/src/rust/frb_generated.io.dart | 37 +++++ .../lib/src/rust/frb_generated.web.dart | 37 +++++ .../flutter_via_create/rust/src/api/simple.rs | 8 + .../rust/src/frb_generated.rs | 125 ++++++++++++++- 7 files changed, 357 insertions(+), 2 deletions(-) diff --git a/frb_example/flutter_via_create/lib/main.dart b/frb_example/flutter_via_create/lib/main.dart index 1020cf61bc..844904b48c 100644 --- a/frb_example/flutter_via_create/lib/main.dart +++ b/frb_example/flutter_via_create/lib/main.dart @@ -4,6 +4,12 @@ import 'package:flutter_via_create/src/rust/frb_generated.dart'; Future main() async { await RustLib.init(); + + await rustFunction(dartCallback: (name) { + print("From rust: $name"); + return "Hello, $name"; + }); + runApp(const MyApp()); } diff --git a/frb_example/flutter_via_create/lib/src/rust/api/simple.dart b/frb_example/flutter_via_create/lib/src/rust/api/simple.dart index d02856e156..20698e7c64 100644 --- a/frb_example/flutter_via_create/lib/src/rust/api/simple.dart +++ b/frb_example/flutter_via_create/lib/src/rust/api/simple.dart @@ -8,3 +8,7 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; String greet({required String name}) => RustLib.instance.api.crateApiSimpleGreet(name: name); + +Future rustFunction( + {required FutureOr Function(String) dartCallback}) => + RustLib.instance.api.crateApiSimpleRustFunction(dartCallback: dartCallback); diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart index 9a65a4d024..860f660343 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart @@ -58,7 +58,7 @@ class RustLib extends BaseEntrypoint { String get codegenVersion => '2.0.0'; @override - int get rustContentHash => -1918914929; + int get rustContentHash => 74002365; static const kDefaultExternalLibraryLoaderConfig = ExternalLibraryLoaderConfig( @@ -72,6 +72,9 @@ abstract class RustLibApi extends BaseApi { String crateApiSimpleGreet({required String name}); Future crateApiSimpleInitApp(); + + Future crateApiSimpleRustFunction( + {required FutureOr Function(String) dartCallback}); } class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @@ -128,6 +131,85 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: [], ); + @override + Future crateApiSimpleRustFunction( + {required FutureOr Function(String) dartCallback}) { + return handler.executeNormal(NormalTask( + callFfi: (port_) { + final serializer = SseSerializer(generalizedFrbRustBinding); + sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + dartCallback, serializer); + pdeCallFfi(generalizedFrbRustBinding, serializer, + funcId: 3, port: port_); + }, + codec: SseCodec( + decodeSuccessData: sse_decode_unit, + decodeErrorData: null, + ), + constMeta: kCrateApiSimpleRustFunctionConstMeta, + argValues: [dartCallback], + apiImpl: this, + )); + } + + TaskConstMeta get kCrateApiSimpleRustFunctionConstMeta => const TaskConstMeta( + debugName: "rust_function", + argNames: ["dartCallback"], + ); + + Future Function(int, dynamic) + encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) raw) { + return (callId, rawArg0) async { + final arg0 = dco_decode_String(rawArg0); + + Box? rawOutput; + Box? rawError; + try { + rawOutput = Box(await raw(arg0)); + } catch (e, s) { + rawError = Box(AnyhowException("$e\n\n$s")); + } + + final serializer = SseSerializer(generalizedFrbRustBinding); + assert((rawOutput != null) ^ (rawError != null)); + if (rawOutput != null) { + serializer.buffer.putUint8(0); + sse_encode_String(rawOutput.value, serializer); + } else { + serializer.buffer.putUint8(1); + sse_encode_AnyhowException(rawError!.value, serializer); + } + final output = serializer.intoRaw(); + + generalizedFrbRustBinding.dartFnDeliverOutput( + callId: callId, + ptr: output.ptr, + rustVecLen: output.rustVecLen, + dataLen: output.dataLen); + }; + } + + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return AnyhowException(raw as String); + } + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + throw UnimplementedError(''); + } + + @protected + Object dco_decode_DartOpaque(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return decodeDartOpaque(raw, generalizedFrbRustBinding); + } + @protected String dco_decode_String(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs @@ -152,6 +234,26 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return; } + @protected + BigInt dco_decode_usize(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return dcoDecodeU64(raw); + } + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var inner = sse_decode_String(deserializer); + return AnyhowException(inner); + } + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var inner = sse_decode_usize(deserializer); + return decodeDartOpaque(inner, generalizedFrbRustBinding); + } + @protected String sse_decode_String(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -177,6 +279,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { // Codec=Sse (Serialization based), see doc to use other codecs } + @protected + BigInt sse_decode_usize(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + return deserializer.buffer.getBigUint64(); + } + @protected int sse_decode_i_32(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -189,6 +297,32 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return deserializer.buffer.getUint8() != 0; } + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_String(self.message, serializer); + } + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_DartOpaque( + encode_DartFn_Inputs_String_Output_String_AnyhowException(self), + serializer); + } + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_usize( + BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque( + self, portManager.dartHandlerPort, generalizedFrbRustBinding))) + .toUnsigned(64), + serializer); + } + @protected void sse_encode_String(String self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -214,6 +348,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { // Codec=Sse (Serialization based), see doc to use other codecs } + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + serializer.buffer.putBigUint64(self); + } + @protected void sse_encode_i_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart index e42d470b46..769acab874 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart @@ -18,6 +18,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw); + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw); + + @protected + Object dco_decode_DartOpaque(dynamic raw); + @protected String dco_decode_String(dynamic raw); @@ -30,6 +41,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void dco_decode_unit(dynamic raw); + @protected + BigInt dco_decode_usize(dynamic raw); + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer); + @protected String sse_decode_String(SseDeserializer deserializer); @@ -42,12 +62,26 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_decode_unit(SseDeserializer deserializer); + @protected + BigInt sse_decode_usize(SseDeserializer deserializer); + @protected int sse_decode_i_32(SseDeserializer deserializer); @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer); + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer); + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer); + @protected void sse_encode_String(String self, SseSerializer serializer); @@ -61,6 +95,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_unit(void self, SseSerializer serializer); + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer); + @protected void sse_encode_i_32(int self, SseSerializer serializer); diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart index 1d35986aa6..b84e8f69c5 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart @@ -20,6 +20,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); + @protected + AnyhowException dco_decode_AnyhowException(dynamic raw); + + @protected + FutureOr Function(String) + dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( + dynamic raw); + + @protected + Object dco_decode_DartOpaque(dynamic raw); + @protected String dco_decode_String(dynamic raw); @@ -32,6 +43,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void dco_decode_unit(dynamic raw); + @protected + BigInt dco_decode_usize(dynamic raw); + + @protected + AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); + + @protected + Object sse_decode_DartOpaque(SseDeserializer deserializer); + @protected String sse_decode_String(SseDeserializer deserializer); @@ -44,12 +64,26 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_decode_unit(SseDeserializer deserializer); + @protected + BigInt sse_decode_usize(SseDeserializer deserializer); + @protected int sse_decode_i_32(SseDeserializer deserializer); @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + void sse_encode_AnyhowException( + AnyhowException self, SseSerializer serializer); + + @protected + void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( + FutureOr Function(String) self, SseSerializer serializer); + + @protected + void sse_encode_DartOpaque(Object self, SseSerializer serializer); + @protected void sse_encode_String(String self, SseSerializer serializer); @@ -63,6 +97,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_unit(void self, SseSerializer serializer); + @protected + void sse_encode_usize(BigInt self, SseSerializer serializer); + @protected void sse_encode_i_32(int self, SseSerializer serializer); diff --git a/frb_example/flutter_via_create/rust/src/api/simple.rs b/frb_example/flutter_via_create/rust/src/api/simple.rs index 4360c82ae0..5210c91a1e 100644 --- a/frb_example/flutter_via_create/rust/src/api/simple.rs +++ b/frb_example/flutter_via_create/rust/src/api/simple.rs @@ -1,3 +1,5 @@ +use flutter_rust_bridge::DartFnFuture; + #[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo pub fn greet(name: String) -> String { format!("Hello, {name}!") @@ -8,3 +10,9 @@ pub fn init_app() { // Default utilities - feel free to customize flutter_rust_bridge::setup_default_user_utils(); } + +pub async fn rust_function( + dart_callback: impl Fn(String) -> DartFnFuture, +) { + dart_callback("Tom".to_owned()).await; // Will get `Hello, Tom!` +} diff --git a/frb_example/flutter_via_create/rust/src/frb_generated.rs b/frb_example/flutter_via_create/rust/src/frb_generated.rs index 695eb302d3..3e23776f68 100644 --- a/frb_example/flutter_via_create/rust/src/frb_generated.rs +++ b/frb_example/flutter_via_create/rust/src/frb_generated.rs @@ -37,7 +37,7 @@ flutter_rust_bridge::frb_generated_boilerplate!( default_rust_auto_opaque = RustAutoOpaqueMoi, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0"; -pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -1918914929; +pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = 74002365; // Section: executor @@ -109,9 +109,100 @@ fn wire__crate__api__simple__init_app_impl( }, ) } +fn wire__crate__api__simple__rust_function_impl( + port_: flutter_rust_bridge::for_generated::MessagePort, + ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, + rust_vec_len_: i32, + data_len_: i32, +) { + FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::( + flutter_rust_bridge::for_generated::TaskInfo { + debug_name: "rust_function", + port: Some(port_), + mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, + }, + move || { + let message = unsafe { + flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( + ptr_, + rust_vec_len_, + data_len_, + ) + }; + let mut deserializer = + flutter_rust_bridge::for_generated::SseDeserializer::new(message); + let api_dart_callback = decode_DartFn_Inputs_String_Output_String_AnyhowException( + ::sse_decode(&mut deserializer), + ); + deserializer.end(); + move |context| async move { + transform_result_sse::<_, ()>( + (move || async move { + let output_ok = Result::<_, ()>::Ok({ + crate::api::simple::rust_function(api_dart_callback).await; + })?; + Ok(output_ok) + })() + .await, + ) + } + }, + ) +} + +// Section: related_funcs + +fn decode_DartFn_Inputs_String_Output_String_AnyhowException( + dart_opaque: flutter_rust_bridge::DartOpaque, +) -> impl Fn(String) -> flutter_rust_bridge::DartFnFuture { + use flutter_rust_bridge::IntoDart; + + async fn body(dart_opaque: flutter_rust_bridge::DartOpaque, arg0: String) -> String { + let args = vec![arg0.into_into_dart().into_dart()]; + let message = FLUTTER_RUST_BRIDGE_HANDLER + .dart_fn_invoke(dart_opaque, args) + .await; + + let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message); + let action = deserializer.cursor.read_u8().unwrap(); + let ans = match action { + 0 => std::result::Result::Ok(::sse_decode(&mut deserializer)), + 1 => std::result::Result::Err( + ::sse_decode(&mut deserializer), + ), + _ => unreachable!(), + }; + deserializer.end(); + let ans = ans.expect("Dart throws exception but Rust side assume it is not failable"); + ans + } + + move |arg0: String| { + flutter_rust_bridge::for_generated::convert_into_dart_fn_future(body( + dart_opaque.clone(), + arg0, + )) + } +} // Section: dart2rust +impl SseDecode for flutter_rust_bridge::for_generated::anyhow::Error { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut inner = ::sse_decode(deserializer); + return flutter_rust_bridge::for_generated::anyhow::anyhow!("{}", inner); + } +} + +impl SseDecode for flutter_rust_bridge::DartOpaque { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut inner = ::sse_decode(deserializer); + return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) }; + } +} + impl SseDecode for String { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -144,6 +235,13 @@ impl SseDecode for () { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {} } +impl SseDecode for usize { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + deserializer.cursor.read_u64::().unwrap() as _ + } +} + impl SseDecode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -168,6 +266,7 @@ fn pde_ffi_dispatcher_primary_impl( // Codec=Pde (Serialization + dispatch), see doc to use other codecs match func_id { 2 => wire__crate__api__simple__init_app_impl(port, ptr, rust_vec_len, data_len), + 3 => wire__crate__api__simple__rust_function_impl(port, ptr, rust_vec_len, data_len), _ => unreachable!(), } } @@ -187,6 +286,20 @@ fn pde_ffi_dispatcher_sync_impl( // Section: rust2dart +impl SseEncode for flutter_rust_bridge::for_generated::anyhow::Error { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(format!("{:?}", self), serializer); + } +} + +impl SseEncode for flutter_rust_bridge::DartOpaque { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(self.encode(), serializer); + } +} + impl SseEncode for String { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -216,6 +329,16 @@ impl SseEncode for () { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {} } +impl SseEncode for usize { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + serializer + .cursor + .write_u64::(self as _) + .unwrap(); + } +} + impl SseEncode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { From 5f632bd2459a950a663cd58769ffbc0f95c5f822 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 15:09:37 +0800 Subject: [PATCH 04/12] chore: mv --- frb_dart/lib/src/dart_opaque/_common.dart | 8 -------- frb_dart/lib/src/dart_opaque/dart_opaque.dart | 10 ++++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/frb_dart/lib/src/dart_opaque/_common.dart b/frb_dart/lib/src/dart_opaque/_common.dart index 3e0926b0e2..7dc7c1f788 100644 --- a/frb_dart/lib/src/dart_opaque/_common.dart +++ b/frb_dart/lib/src/dart_opaque/_common.dart @@ -1,5 +1,4 @@ import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; -import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; /// {@macro flutter_rust_bridge.only_for_generated_code} Object decodeDartOpaque( @@ -9,10 +8,3 @@ Object decodeDartOpaque( } return generalizedFrbRustBinding.dartOpaqueRust2DartDecode(raw); } - -/// {@macro flutter_rust_bridge.only_for_generated_code} -PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, - GeneralizedFrbRustBinding generalizedFrbRustBinding) { - return generalizedFrbRustBinding.dartOpaqueDart2RustEncode( - raw, dartHandlerPort); -} diff --git a/frb_dart/lib/src/dart_opaque/dart_opaque.dart b/frb_dart/lib/src/dart_opaque/dart_opaque.dart index 87faeb82c5..0b1671ce33 100644 --- a/frb_dart/lib/src/dart_opaque/dart_opaque.dart +++ b/frb_dart/lib/src/dart_opaque/dart_opaque.dart @@ -1,2 +1,12 @@ +import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; +import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; + export '_common.dart'; export '_io.dart' if (dart.library.js_interop) '_web.dart'; + +/// {@macro flutter_rust_bridge.only_for_generated_code} +PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, + GeneralizedFrbRustBinding generalizedFrbRustBinding) { + return generalizedFrbRustBinding.dartOpaqueDart2RustEncode( + raw, dartHandlerPort); +} From b98ee8d924c345b4e77c6ae91ccff2bf18e6f615 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 15:11:42 +0800 Subject: [PATCH 05/12] more --- frb_dart/lib/src/dart_opaque/_io.dart | 3 ++- frb_dart/lib/src/dart_opaque/_web.dart | 9 +++++++++ frb_dart/lib/src/dart_opaque/dart_opaque.dart | 5 ++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/frb_dart/lib/src/dart_opaque/_io.dart b/frb_dart/lib/src/dart_opaque/_io.dart index 8b13789179..d2a5b39ebb 100644 --- a/frb_dart/lib/src/dart_opaque/_io.dart +++ b/frb_dart/lib/src/dart_opaque/_io.dart @@ -1 +1,2 @@ - +/// {@macro flutter_rust_bridge.internal} +Object prepareDartOpaqueForEncoding(Object raw) => raw; diff --git a/frb_dart/lib/src/dart_opaque/_web.dart b/frb_dart/lib/src/dart_opaque/_web.dart index 8b13789179..8d983c0c3c 100644 --- a/frb_dart/lib/src/dart_opaque/_web.dart +++ b/frb_dart/lib/src/dart_opaque/_web.dart @@ -1 +1,10 @@ +import 'dart:js_util'; +/// {@macro flutter_rust_bridge.internal} +Object prepareDartOpaqueForEncoding(Object raw) { + // #2183 + if (raw is Function) { + return allowInterop(raw); + } + return raw; +} diff --git a/frb_dart/lib/src/dart_opaque/dart_opaque.dart b/frb_dart/lib/src/dart_opaque/dart_opaque.dart index 0b1671ce33..f9faa11c95 100644 --- a/frb_dart/lib/src/dart_opaque/dart_opaque.dart +++ b/frb_dart/lib/src/dart_opaque/dart_opaque.dart @@ -1,6 +1,9 @@ import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; +// ignore: always_use_package_imports +import '_io.dart' if (dart.library.js_interop) '_web.dart'; + export '_common.dart'; export '_io.dart' if (dart.library.js_interop) '_web.dart'; @@ -8,5 +11,5 @@ export '_io.dart' if (dart.library.js_interop) '_web.dart'; PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, GeneralizedFrbRustBinding generalizedFrbRustBinding) { return generalizedFrbRustBinding.dartOpaqueDart2RustEncode( - raw, dartHandlerPort); + prepareDartOpaqueForEncoding(raw), dartHandlerPort); } From e67ae5102c3d1e4eb203f851bf3f2a37f00799c1 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 15:13:51 +0800 Subject: [PATCH 06/12] more --- frb_dart/lib/src/dart_opaque/_common.dart | 10 ++++++++++ frb_dart/lib/src/dart_opaque/_io.dart | 10 ++++++++-- frb_dart/lib/src/dart_opaque/_web.dart | 13 +++++++++++-- frb_dart/lib/src/dart_opaque/dart_opaque.dart | 13 ------------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/frb_dart/lib/src/dart_opaque/_common.dart b/frb_dart/lib/src/dart_opaque/_common.dart index 7dc7c1f788..1bde337672 100644 --- a/frb_dart/lib/src/dart_opaque/_common.dart +++ b/frb_dart/lib/src/dart_opaque/_common.dart @@ -1,4 +1,5 @@ import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; +import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; /// {@macro flutter_rust_bridge.only_for_generated_code} Object decodeDartOpaque( @@ -8,3 +9,12 @@ Object decodeDartOpaque( } return generalizedFrbRustBinding.dartOpaqueRust2DartDecode(raw); } + +/// {@macro flutter_rust_bridge.internal} +PlatformPointer encodeDartOpaqueCommon( + Object raw, + NativePortType dartHandlerPort, + GeneralizedFrbRustBinding generalizedFrbRustBinding) { + return generalizedFrbRustBinding.dartOpaqueDart2RustEncode( + raw, dartHandlerPort); +} diff --git a/frb_dart/lib/src/dart_opaque/_io.dart b/frb_dart/lib/src/dart_opaque/_io.dart index d2a5b39ebb..11fefc12be 100644 --- a/frb_dart/lib/src/dart_opaque/_io.dart +++ b/frb_dart/lib/src/dart_opaque/_io.dart @@ -1,2 +1,8 @@ -/// {@macro flutter_rust_bridge.internal} -Object prepareDartOpaqueForEncoding(Object raw) => raw; +import 'package:flutter_rust_bridge/src/dart_opaque/_common.dart'; +import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; +import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; + +/// {@macro flutter_rust_bridge.only_for_generated_code} +PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, + GeneralizedFrbRustBinding generalizedFrbRustBinding) => + encodeDartOpaqueCommon(raw, dartHandlerPort, generalizedFrbRustBinding); diff --git a/frb_dart/lib/src/dart_opaque/_web.dart b/frb_dart/lib/src/dart_opaque/_web.dart index 8d983c0c3c..0705ac072f 100644 --- a/frb_dart/lib/src/dart_opaque/_web.dart +++ b/frb_dart/lib/src/dart_opaque/_web.dart @@ -1,7 +1,16 @@ import 'dart:js_util'; -/// {@macro flutter_rust_bridge.internal} -Object prepareDartOpaqueForEncoding(Object raw) { +import 'package:flutter_rust_bridge/src/dart_opaque/_common.dart'; +import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; +import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; + +/// {@macro flutter_rust_bridge.only_for_generated_code} +PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, + GeneralizedFrbRustBinding generalizedFrbRustBinding) => + encodeDartOpaqueCommon( + _prepareDartOpaque(raw), dartHandlerPort, generalizedFrbRustBinding); + +Object _prepareDartOpaque(Object raw) { // #2183 if (raw is Function) { return allowInterop(raw); diff --git a/frb_dart/lib/src/dart_opaque/dart_opaque.dart b/frb_dart/lib/src/dart_opaque/dart_opaque.dart index f9faa11c95..87faeb82c5 100644 --- a/frb_dart/lib/src/dart_opaque/dart_opaque.dart +++ b/frb_dart/lib/src/dart_opaque/dart_opaque.dart @@ -1,15 +1,2 @@ -import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart'; -import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart'; - -// ignore: always_use_package_imports -import '_io.dart' if (dart.library.js_interop) '_web.dart'; - export '_common.dart'; export '_io.dart' if (dart.library.js_interop) '_web.dart'; - -/// {@macro flutter_rust_bridge.only_for_generated_code} -PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort, - GeneralizedFrbRustBinding generalizedFrbRustBinding) { - return generalizedFrbRustBinding.dartOpaqueDart2RustEncode( - prepareDartOpaqueForEncoding(raw), dartHandlerPort); -} From 35b5e1aa77618bd06aceb463bd09049425eb8f0e Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 15:39:35 +0800 Subject: [PATCH 07/12] Revert "chore: test2" This reverts commit 7f7efe577757a7adb8b5752cb354d7c81c5c392c. --- frb_example/flutter_via_create/lib/main.dart | 6 - .../lib/src/rust/api/simple.dart | 4 - .../lib/src/rust/frb_generated.dart | 142 +----------------- .../lib/src/rust/frb_generated.io.dart | 37 ----- .../lib/src/rust/frb_generated.web.dart | 37 ----- .../flutter_via_create/rust/src/api/simple.rs | 8 - .../rust/src/frb_generated.rs | 125 +-------------- 7 files changed, 2 insertions(+), 357 deletions(-) diff --git a/frb_example/flutter_via_create/lib/main.dart b/frb_example/flutter_via_create/lib/main.dart index 844904b48c..1020cf61bc 100644 --- a/frb_example/flutter_via_create/lib/main.dart +++ b/frb_example/flutter_via_create/lib/main.dart @@ -4,12 +4,6 @@ import 'package:flutter_via_create/src/rust/frb_generated.dart'; Future main() async { await RustLib.init(); - - await rustFunction(dartCallback: (name) { - print("From rust: $name"); - return "Hello, $name"; - }); - runApp(const MyApp()); } diff --git a/frb_example/flutter_via_create/lib/src/rust/api/simple.dart b/frb_example/flutter_via_create/lib/src/rust/api/simple.dart index 20698e7c64..d02856e156 100644 --- a/frb_example/flutter_via_create/lib/src/rust/api/simple.dart +++ b/frb_example/flutter_via_create/lib/src/rust/api/simple.dart @@ -8,7 +8,3 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; String greet({required String name}) => RustLib.instance.api.crateApiSimpleGreet(name: name); - -Future rustFunction( - {required FutureOr Function(String) dartCallback}) => - RustLib.instance.api.crateApiSimpleRustFunction(dartCallback: dartCallback); diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart index 860f660343..9a65a4d024 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.dart @@ -58,7 +58,7 @@ class RustLib extends BaseEntrypoint { String get codegenVersion => '2.0.0'; @override - int get rustContentHash => 74002365; + int get rustContentHash => -1918914929; static const kDefaultExternalLibraryLoaderConfig = ExternalLibraryLoaderConfig( @@ -72,9 +72,6 @@ abstract class RustLibApi extends BaseApi { String crateApiSimpleGreet({required String name}); Future crateApiSimpleInitApp(); - - Future crateApiSimpleRustFunction( - {required FutureOr Function(String) dartCallback}); } class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @@ -131,85 +128,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { argNames: [], ); - @override - Future crateApiSimpleRustFunction( - {required FutureOr Function(String) dartCallback}) { - return handler.executeNormal(NormalTask( - callFfi: (port_) { - final serializer = SseSerializer(generalizedFrbRustBinding); - sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - dartCallback, serializer); - pdeCallFfi(generalizedFrbRustBinding, serializer, - funcId: 3, port: port_); - }, - codec: SseCodec( - decodeSuccessData: sse_decode_unit, - decodeErrorData: null, - ), - constMeta: kCrateApiSimpleRustFunctionConstMeta, - argValues: [dartCallback], - apiImpl: this, - )); - } - - TaskConstMeta get kCrateApiSimpleRustFunctionConstMeta => const TaskConstMeta( - debugName: "rust_function", - argNames: ["dartCallback"], - ); - - Future Function(int, dynamic) - encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) raw) { - return (callId, rawArg0) async { - final arg0 = dco_decode_String(rawArg0); - - Box? rawOutput; - Box? rawError; - try { - rawOutput = Box(await raw(arg0)); - } catch (e, s) { - rawError = Box(AnyhowException("$e\n\n$s")); - } - - final serializer = SseSerializer(generalizedFrbRustBinding); - assert((rawOutput != null) ^ (rawError != null)); - if (rawOutput != null) { - serializer.buffer.putUint8(0); - sse_encode_String(rawOutput.value, serializer); - } else { - serializer.buffer.putUint8(1); - sse_encode_AnyhowException(rawError!.value, serializer); - } - final output = serializer.intoRaw(); - - generalizedFrbRustBinding.dartFnDeliverOutput( - callId: callId, - ptr: output.ptr, - rustVecLen: output.rustVecLen, - dataLen: output.dataLen); - }; - } - - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return AnyhowException(raw as String); - } - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - throw UnimplementedError(''); - } - - @protected - Object dco_decode_DartOpaque(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return decodeDartOpaque(raw, generalizedFrbRustBinding); - } - @protected String dco_decode_String(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs @@ -234,26 +152,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return; } - @protected - BigInt dco_decode_usize(dynamic raw) { - // Codec=Dco (DartCObject based), see doc to use other codecs - return dcoDecodeU64(raw); - } - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var inner = sse_decode_String(deserializer); - return AnyhowException(inner); - } - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - var inner = sse_decode_usize(deserializer); - return decodeDartOpaque(inner, generalizedFrbRustBinding); - } - @protected String sse_decode_String(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -279,12 +177,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { // Codec=Sse (Serialization based), see doc to use other codecs } - @protected - BigInt sse_decode_usize(SseDeserializer deserializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - return deserializer.buffer.getBigUint64(); - } - @protected int sse_decode_i_32(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -297,32 +189,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return deserializer.buffer.getUint8() != 0; } - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_String(self.message, serializer); - } - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_DartOpaque( - encode_DartFn_Inputs_String_Output_String_AnyhowException(self), - serializer); - } - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_usize( - BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque( - self, portManager.dartHandlerPort, generalizedFrbRustBinding))) - .toUnsigned(64), - serializer); - } - @protected void sse_encode_String(String self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -348,12 +214,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { // Codec=Sse (Serialization based), see doc to use other codecs } - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer) { - // Codec=Sse (Serialization based), see doc to use other codecs - serializer.buffer.putBigUint64(self); - } - @protected void sse_encode_i_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart index 769acab874..e42d470b46 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.io.dart @@ -18,17 +18,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw); - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw); - - @protected - Object dco_decode_DartOpaque(dynamic raw); - @protected String dco_decode_String(dynamic raw); @@ -41,15 +30,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void dco_decode_unit(dynamic raw); - @protected - BigInt dco_decode_usize(dynamic raw); - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer); - @protected String sse_decode_String(SseDeserializer deserializer); @@ -62,26 +42,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_decode_unit(SseDeserializer deserializer); - @protected - BigInt sse_decode_usize(SseDeserializer deserializer); - @protected int sse_decode_i_32(SseDeserializer deserializer); @protected bool sse_decode_bool(SseDeserializer deserializer); - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer); - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer); - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer); - @protected void sse_encode_String(String self, SseSerializer serializer); @@ -95,9 +61,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_unit(void self, SseSerializer serializer); - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer); - @protected void sse_encode_i_32(int self, SseSerializer serializer); diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart index b84e8f69c5..1d35986aa6 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart @@ -20,17 +20,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { required super.portManager, }); - @protected - AnyhowException dco_decode_AnyhowException(dynamic raw); - - @protected - FutureOr Function(String) - dco_decode_DartFn_Inputs_String_Output_String_AnyhowException( - dynamic raw); - - @protected - Object dco_decode_DartOpaque(dynamic raw); - @protected String dco_decode_String(dynamic raw); @@ -43,15 +32,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void dco_decode_unit(dynamic raw); - @protected - BigInt dco_decode_usize(dynamic raw); - - @protected - AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer); - - @protected - Object sse_decode_DartOpaque(SseDeserializer deserializer); - @protected String sse_decode_String(SseDeserializer deserializer); @@ -64,26 +44,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_decode_unit(SseDeserializer deserializer); - @protected - BigInt sse_decode_usize(SseDeserializer deserializer); - @protected int sse_decode_i_32(SseDeserializer deserializer); @protected bool sse_decode_bool(SseDeserializer deserializer); - @protected - void sse_encode_AnyhowException( - AnyhowException self, SseSerializer serializer); - - @protected - void sse_encode_DartFn_Inputs_String_Output_String_AnyhowException( - FutureOr Function(String) self, SseSerializer serializer); - - @protected - void sse_encode_DartOpaque(Object self, SseSerializer serializer); - @protected void sse_encode_String(String self, SseSerializer serializer); @@ -97,9 +63,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_unit(void self, SseSerializer serializer); - @protected - void sse_encode_usize(BigInt self, SseSerializer serializer); - @protected void sse_encode_i_32(int self, SseSerializer serializer); diff --git a/frb_example/flutter_via_create/rust/src/api/simple.rs b/frb_example/flutter_via_create/rust/src/api/simple.rs index 5210c91a1e..4360c82ae0 100644 --- a/frb_example/flutter_via_create/rust/src/api/simple.rs +++ b/frb_example/flutter_via_create/rust/src/api/simple.rs @@ -1,5 +1,3 @@ -use flutter_rust_bridge::DartFnFuture; - #[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo pub fn greet(name: String) -> String { format!("Hello, {name}!") @@ -10,9 +8,3 @@ pub fn init_app() { // Default utilities - feel free to customize flutter_rust_bridge::setup_default_user_utils(); } - -pub async fn rust_function( - dart_callback: impl Fn(String) -> DartFnFuture, -) { - dart_callback("Tom".to_owned()).await; // Will get `Hello, Tom!` -} diff --git a/frb_example/flutter_via_create/rust/src/frb_generated.rs b/frb_example/flutter_via_create/rust/src/frb_generated.rs index 3e23776f68..695eb302d3 100644 --- a/frb_example/flutter_via_create/rust/src/frb_generated.rs +++ b/frb_example/flutter_via_create/rust/src/frb_generated.rs @@ -37,7 +37,7 @@ flutter_rust_bridge::frb_generated_boilerplate!( default_rust_auto_opaque = RustAutoOpaqueMoi, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0"; -pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = 74002365; +pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -1918914929; // Section: executor @@ -109,100 +109,9 @@ fn wire__crate__api__simple__init_app_impl( }, ) } -fn wire__crate__api__simple__rust_function_impl( - port_: flutter_rust_bridge::for_generated::MessagePort, - ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, - rust_vec_len_: i32, - data_len_: i32, -) { - FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::( - flutter_rust_bridge::for_generated::TaskInfo { - debug_name: "rust_function", - port: Some(port_), - mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, - }, - move || { - let message = unsafe { - flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( - ptr_, - rust_vec_len_, - data_len_, - ) - }; - let mut deserializer = - flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let api_dart_callback = decode_DartFn_Inputs_String_Output_String_AnyhowException( - ::sse_decode(&mut deserializer), - ); - deserializer.end(); - move |context| async move { - transform_result_sse::<_, ()>( - (move || async move { - let output_ok = Result::<_, ()>::Ok({ - crate::api::simple::rust_function(api_dart_callback).await; - })?; - Ok(output_ok) - })() - .await, - ) - } - }, - ) -} - -// Section: related_funcs - -fn decode_DartFn_Inputs_String_Output_String_AnyhowException( - dart_opaque: flutter_rust_bridge::DartOpaque, -) -> impl Fn(String) -> flutter_rust_bridge::DartFnFuture { - use flutter_rust_bridge::IntoDart; - - async fn body(dart_opaque: flutter_rust_bridge::DartOpaque, arg0: String) -> String { - let args = vec![arg0.into_into_dart().into_dart()]; - let message = FLUTTER_RUST_BRIDGE_HANDLER - .dart_fn_invoke(dart_opaque, args) - .await; - - let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let action = deserializer.cursor.read_u8().unwrap(); - let ans = match action { - 0 => std::result::Result::Ok(::sse_decode(&mut deserializer)), - 1 => std::result::Result::Err( - ::sse_decode(&mut deserializer), - ), - _ => unreachable!(), - }; - deserializer.end(); - let ans = ans.expect("Dart throws exception but Rust side assume it is not failable"); - ans - } - - move |arg0: String| { - flutter_rust_bridge::for_generated::convert_into_dart_fn_future(body( - dart_opaque.clone(), - arg0, - )) - } -} // Section: dart2rust -impl SseDecode for flutter_rust_bridge::for_generated::anyhow::Error { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut inner = ::sse_decode(deserializer); - return flutter_rust_bridge::for_generated::anyhow::anyhow!("{}", inner); - } -} - -impl SseDecode for flutter_rust_bridge::DartOpaque { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut inner = ::sse_decode(deserializer); - return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) }; - } -} - impl SseDecode for String { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -235,13 +144,6 @@ impl SseDecode for () { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {} } -impl SseDecode for usize { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - deserializer.cursor.read_u64::().unwrap() as _ - } -} - impl SseDecode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -266,7 +168,6 @@ fn pde_ffi_dispatcher_primary_impl( // Codec=Pde (Serialization + dispatch), see doc to use other codecs match func_id { 2 => wire__crate__api__simple__init_app_impl(port, ptr, rust_vec_len, data_len), - 3 => wire__crate__api__simple__rust_function_impl(port, ptr, rust_vec_len, data_len), _ => unreachable!(), } } @@ -286,20 +187,6 @@ fn pde_ffi_dispatcher_sync_impl( // Section: rust2dart -impl SseEncode for flutter_rust_bridge::for_generated::anyhow::Error { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(format!("{:?}", self), serializer); - } -} - -impl SseEncode for flutter_rust_bridge::DartOpaque { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(self.encode(), serializer); - } -} - impl SseEncode for String { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -329,16 +216,6 @@ impl SseEncode for () { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {} } -impl SseEncode for usize { - // Codec=Sse (Serialization based), see doc to use other codecs - fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - serializer - .cursor - .write_u64::(self as _) - .unwrap(); - } -} - impl SseEncode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { From 6c4035fe2eeef38bbcc91a13baaff7c16487bb1e Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 15:42:52 +0800 Subject: [PATCH 08/12] chore: doc --- CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ececfc7722..258f9ac778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Changelog +## 2.1.0 + +* Please refer to https://fzyzcjy.github.io/flutter_rust_bridge/guides/miscellaneous/whats-new for what's changed in V2. +* Add the ability to generate plugins from the CLI tool #2144 (thanks @mcmah309) +* Fix codegen halt when having boxed trait objects #2180 +* Add attribute `#[frb(dart_async)]` #2181 +* Fix Dart closures cannot be encoded when using the new Dart JS interop #2191 +* Expose Rust executor's async runtime for customization #2151 +* Generate methods of Default trait #2150 +* Automatically rename function names to avoid keyword conflict #2150 +* Improve parsing trait impl in third party crates #2150 +* Support more attributes #2140 +* Fix generated use statement pointing to self #2140 +* Fix linter warning avoid_return_types_on_setters #2140 +* Reduce number of generated files #2140 +* Add default_dart_async configuration option #2139 +* Handle root module scenario in pub use transformer #2124 +* Improve hints when fails to parse a struct or enum #2124 + ## 2.0.0 * Please refer to https://fzyzcjy.github.io/flutter_rust_bridge/guides/miscellaneous/whats-new for what's changed in V2. From 79b29891bd2b24d44f0638f74b7ff2071556dfd3 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 16:27:05 +0800 Subject: [PATCH 09/12] chore: tests --- frb_example/pure_dart/test/api/dart_fn_test.dart | 8 ++++---- frb_example/pure_dart/test/api/dart_opaque_sync_test.dart | 4 ++-- frb_example/pure_dart/test/api/dart_opaque_test.dart | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/frb_example/pure_dart/test/api/dart_fn_test.dart b/frb_example/pure_dart/test/api/dart_fn_test.dart index 7ddaac2868..31d37d0092 100644 --- a/frb_example/pure_dart/test/api/dart_fn_test.dart +++ b/frb_example/pure_dart/test/api/dart_fn_test.dart @@ -57,22 +57,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinNormal', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinNormal( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinNormal', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = + final dynamic output = await rustCallDartWithDartOpaqueResultTwinNormal(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinNormal', () async { diff --git a/frb_example/pure_dart/test/api/dart_opaque_sync_test.dart b/frb_example/pure_dart/test/api/dart_opaque_sync_test.dart index f98de33fb8..67bf6f87f4 100644 --- a/frb_example/pure_dart/test/api/dart_opaque_sync_test.dart +++ b/frb_example/pure_dart/test/api/dart_opaque_sync_test.dart @@ -15,8 +15,8 @@ Future main({bool skipRustLibInit = false}) async { test('loopback', () { var syncBack = syncLoopbackTwinNormal(opaque: f); - expect( - identical(syncOptionLoopbackTwinNormal(opaque: syncBack), f), isTrue); + expect((syncOptionLoopbackTwinNormal(opaque: syncBack) as dynamic)(), + 'Test_String'); expect(syncOptionLoopbackTwinNormal(opaque: null), isNull); }); diff --git a/frb_example/pure_dart/test/api/dart_opaque_test.dart b/frb_example/pure_dart/test/api/dart_opaque_test.dart index b63818e184..0cc1914106 100644 --- a/frb_example/pure_dart/test/api/dart_opaque_test.dart +++ b/frb_example/pure_dart/test/api/dart_opaque_test.dart @@ -55,7 +55,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinNormal(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); From 21802f1b3719eaae510c32dde316616d118deb32 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 16:31:28 +0800 Subject: [PATCH 10/12] chore: codegen --- .../pseudo_manual/dart_fn_twin_rust_async_sse_test.dart | 8 ++++---- .../api/pseudo_manual/dart_fn_twin_rust_async_test.dart | 8 ++++---- .../test/api/pseudo_manual/dart_fn_twin_sse_test.dart | 9 +++++---- .../pseudo_manual/dart_opaque_sync_twin_sse_test.dart | 3 ++- .../dart_opaque_twin_rust_async_sse_test.dart | 2 +- .../pseudo_manual/dart_opaque_twin_rust_async_test.dart | 2 +- .../api/pseudo_manual/dart_opaque_twin_sse_test.dart | 2 +- .../pseudo_manual/dart_opaque_twin_sync_sse_test.dart | 2 +- .../api/pseudo_manual/dart_opaque_twin_sync_test.dart | 2 +- frb_example/pure_dart_pde/test/api/dart_fn_test.dart | 8 ++++---- .../pure_dart_pde/test/api/dart_opaque_sync_test.dart | 4 ++-- frb_example/pure_dart_pde/test/api/dart_opaque_test.dart | 2 +- .../api/pseudo_manual/dart_fn_twin_rust_async_test.dart | 8 ++++---- .../pseudo_manual/dart_opaque_twin_rust_async_test.dart | 2 +- .../api/pseudo_manual/dart_opaque_twin_sync_test.dart | 2 +- 15 files changed, 33 insertions(+), 31 deletions(-) diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_sse_test.dart index 900f0bda05..d2c407c1fe 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_sse_test.dart @@ -63,22 +63,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinRustAsyncSse', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinRustAsyncSse( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinRustAsyncSse', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = + final dynamic output = await rustCallDartWithDartOpaqueResultTwinRustAsyncSse(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinRustAsyncSse', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart index 89575845f3..845e518c79 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart @@ -61,22 +61,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinRustAsync', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinRustAsync( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinRustAsync', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = + final dynamic output = await rustCallDartWithDartOpaqueResultTwinRustAsync(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinRustAsync', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_sse_test.dart index c60fce643a..01347f81fb 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_fn_twin_sse_test.dart @@ -59,21 +59,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinSse', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinSse( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinSse', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = await rustCallDartWithDartOpaqueResultTwinSse(callback: () { + final dynamic output = + await rustCallDartWithDartOpaqueResultTwinSse(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinSse', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_sync_twin_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_sync_twin_sse_test.dart index e8048c0369..b6fdb30337 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_sync_twin_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_sync_twin_sse_test.dart @@ -19,7 +19,8 @@ Future main({bool skipRustLibInit = false}) async { test('loopback', () { var syncBack = syncLoopbackTwinSse(opaque: f); - expect(identical(syncOptionLoopbackTwinSse(opaque: syncBack), f), isTrue); + expect((syncOptionLoopbackTwinSse(opaque: syncBack) as dynamic)(), + 'Test_String'); expect(syncOptionLoopbackTwinSse(opaque: null), isNull); }); diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart index be9f3adc7b..57ae369c06 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart @@ -67,7 +67,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinRustAsyncSse(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart index d30079fab4..f7cdcde85a 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart @@ -62,7 +62,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinRustAsync(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart index 68b523ecf9..0cb72ca834 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart @@ -58,7 +58,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinSse(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart index 5f83d9f06a..327d7a7464 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart @@ -60,7 +60,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinSyncSse(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart index dc0e4ec387..717a81f88c 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart @@ -59,7 +59,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinSync(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart_pde/test/api/dart_fn_test.dart b/frb_example/pure_dart_pde/test/api/dart_fn_test.dart index 34987a055b..03ab741842 100644 --- a/frb_example/pure_dart_pde/test/api/dart_fn_test.dart +++ b/frb_example/pure_dart_pde/test/api/dart_fn_test.dart @@ -59,22 +59,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinNormal', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinNormal( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinNormal', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = + final dynamic output = await rustCallDartWithDartOpaqueResultTwinNormal(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinNormal', () async { diff --git a/frb_example/pure_dart_pde/test/api/dart_opaque_sync_test.dart b/frb_example/pure_dart_pde/test/api/dart_opaque_sync_test.dart index eaf4d05364..87b71c148d 100644 --- a/frb_example/pure_dart_pde/test/api/dart_opaque_sync_test.dart +++ b/frb_example/pure_dart_pde/test/api/dart_opaque_sync_test.dart @@ -17,8 +17,8 @@ Future main({bool skipRustLibInit = false}) async { test('loopback', () { var syncBack = syncLoopbackTwinNormal(opaque: f); - expect( - identical(syncOptionLoopbackTwinNormal(opaque: syncBack), f), isTrue); + expect((syncOptionLoopbackTwinNormal(opaque: syncBack) as dynamic)(), + 'Test_String'); expect(syncOptionLoopbackTwinNormal(opaque: null), isNull); }); diff --git a/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart b/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart index a8357e792a..09a07de7c8 100644 --- a/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart +++ b/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart @@ -57,7 +57,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinNormal(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart index 3225419954..22e7c7bd91 100644 --- a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart +++ b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_fn_twin_rust_async_test.dart @@ -63,22 +63,22 @@ Future main({bool skipRustLibInit = false}) async { test('rustCallDartWithDartOpaqueArgTwinRustAsync', () async { final opaque = (String whatever) => 42; - var callbackCalls = []; + var callbackCalls = []; await rustCallDartWithDartOpaqueArgTwinRustAsync( input: opaque, callback: (arg) => callbackCalls.add(arg)); - expect(callbackCalls, [opaque]); + expect(callbackCalls[0]('hello'), 42); }); test('rustCallDartWithDartOpaqueResultTwinRustAsync', () async { final opaque = (String whatever) => 42; var callCount = 0; - final output = + final dynamic output = await rustCallDartWithDartOpaqueResultTwinRustAsync(callback: () { callCount++; return opaque; }); expect(callCount, 1); - expect(output, opaque); + expect(output('hello'), 42); }); test('rustCallDartMultiTimesTwinRustAsync', () async { diff --git a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart index 823e082fbf..016fa07361 100644 --- a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart +++ b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart @@ -64,7 +64,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinRustAsync(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); diff --git a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart index 5398ef3d09..5a4c767aa0 100644 --- a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart +++ b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart @@ -61,7 +61,7 @@ Future main({bool skipRustLibInit = false}) async { final output = await cloneDartOpaqueTwinSync(opaque: opaque); expect(output.length, 10); for (final x in output) { - expect(identical(x, opaque), true); + if (!kIsWeb) expect(identical(x, opaque), true); expect((x as Function)(42), 42 + 1); } }); From 1ea47e7c90325643a41d6c3ec4e13377a3a111a1 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 16:49:22 +0800 Subject: [PATCH 11/12] chore: test --- frb_example/pure_dart/test/api/dart_opaque_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frb_example/pure_dart/test/api/dart_opaque_test.dart b/frb_example/pure_dart/test/api/dart_opaque_test.dart index 0cc1914106..a70bcc34b0 100644 --- a/frb_example/pure_dart/test/api/dart_opaque_test.dart +++ b/frb_example/pure_dart/test/api/dart_opaque_test.dart @@ -21,7 +21,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinNormal(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { From 938a209a83abce80ee1d16ac7dd72cc612187bbc Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 6 Jul 2024 16:50:31 +0800 Subject: [PATCH 12/12] chore: codegen --- .../api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart | 2 +- .../api/pseudo_manual/dart_opaque_twin_rust_async_test.dart | 2 +- .../test/api/pseudo_manual/dart_opaque_twin_sse_test.dart | 2 +- .../test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart | 2 +- .../test/api/pseudo_manual/dart_opaque_twin_sync_test.dart | 2 +- frb_example/pure_dart_pde/test/api/dart_opaque_test.dart | 2 +- .../api/pseudo_manual/dart_opaque_twin_rust_async_test.dart | 2 +- .../test/api/pseudo_manual/dart_opaque_twin_sync_test.dart | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart index 57ae369c06..f9ee647fce 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_sse_test.dart @@ -26,7 +26,7 @@ Future main({bool skipRustLibInit = false}) async { var back2 = await loopBackTwinRustAsyncSse(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart index f7cdcde85a..08ddfd7af1 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart @@ -25,7 +25,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinRustAsync(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart index 0cb72ca834..2828bb5f99 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sse_test.dart @@ -25,7 +25,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinSse(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart index 327d7a7464..19babc2d64 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_sse_test.dart @@ -25,7 +25,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinSyncSse(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart index 717a81f88c..c555f79e03 100644 --- a/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart +++ b/frb_example/pure_dart/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart @@ -25,7 +25,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinSync(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart b/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart index 09a07de7c8..3db2a6d2e4 100644 --- a/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart +++ b/frb_example/pure_dart_pde/test/api/dart_opaque_test.dart @@ -23,7 +23,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinNormal(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart index 016fa07361..c0dd4dcda6 100644 --- a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart +++ b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_rust_async_test.dart @@ -27,7 +27,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinRustAsync(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async { diff --git a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart index 5a4c767aa0..fb3cd4b177 100644 --- a/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart +++ b/frb_example/pure_dart_pde/test/api/pseudo_manual/dart_opaque_twin_sync_test.dart @@ -27,7 +27,7 @@ Future main({bool skipRustLibInit = false}) async { expect(back1(), 'Test_String'); var back2 = await loopBackTwinSync(opaque: back1) as String Function(); expect(back2(), 'Test_String'); - expect(identical(back2, f), isTrue); + if (!kIsWeb) expect(identical(back2, f), isTrue); }); test('drop', () async {