Skip to content

Commit

Permalink
fix: api_fill_to_wire for tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Jun 17, 2023
1 parent 104d0c4 commit 46e75be
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 38 deletions.
47 changes: 36 additions & 11 deletions frb_codegen/src/generator/dart/ty_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,50 @@ use crate::ir::*;
use crate::target::Acc;
use crate::type_dart_generator_struct;

use super::TypeStructRefGenerator;

type_dart_generator_struct!(TypeRecordGenerator, IrTypeRecord);

impl TypeDartGeneratorTrait for TypeRecordGenerator<'_> {
fn api2wire_body(&self) -> Acc<Option<String>> {
TypeStructRefGenerator {
ir: self.ir.inner.clone(),
context: self.context.clone(),
let values = self
.ir
.values
.iter()
.enumerate()
.map(|(idx, ty)| format!("api2wire_{}(raw.${})", ty.safe_ident(), idx + 1))
.collect::<Vec<_>>()
.join(",");
Acc {
wasm: Some(format!("return [{values}];")),
..Default::default()
}
.api2wire_body()
}

fn api_fill_to_wire_body(&self) -> Option<String> {
TypeStructRefGenerator {
ir: self.ir.inner.clone(),
context: self.context.clone(),
}
.api_fill_to_wire_body()
let ir = self.ir.inner.get(self.context.ir_file);
let values = ir
.fields
.iter()
.enumerate()
.map(|(idx, field)| {
if field.ty.is_struct() {
format!(
"_api_fill_to_wire_{}(apiObj.${}, wireObj.{});",
field.ty.safe_ident(),
idx + 1,
field.name.rust_style(),
)
} else {
format!(
"wireObj.{} = api2wire_{}(apiObj.${});",
field.name.rust_style(),
field.ty.safe_ident(),
idx + 1
)
}
})
.collect::<Vec<_>>()
.join("\n");
Some(values)
}

fn wire2api_body(&self) -> String {
Expand Down
5 changes: 0 additions & 5 deletions frb_codegen/src/generator/rust/ty_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ type_rust_generator_struct!(TypeRecordGenerator, IrTypeRecord);

impl TypeRustGeneratorTrait for TypeRecordGenerator<'_> {
fn wire2api_body(&self) -> Acc<Option<String>> {
// TypeStructRefGenerator {
// ir: self.ir.inner.clone(),
// context: self.context.clone(),
// }
// .wire2api_body()
let ir = self.ir.inner.get(self.context.ir_file);
let len = ir.fields.len();
let values: Acc<Vec<_>> = ir
Expand Down
3 changes: 1 addition & 2 deletions frb_codegen/src/ir/ty_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ impl IrTypeTrait for IrTypeRecord {
let values = self
.values
.iter()
.enumerate()
.map(|(idx, ty)| format!("{} field{idx}", ty.dart_api_type()))
.map(IrType::dart_api_type)
.collect::<Vec<_>>()
.join(",");
if self.values.len() == 1 {
Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart/dart/lib/bridge_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,11 @@ abstract class FlutterRustBridgeExampleSingleBlockTest {

FlutterRustBridgeTaskConstMeta get kTestStructWithEnumConstMeta;

Future<(String field0, int field1)> testTuple({(String field0, int field1)? value, dynamic hint});
Future<(String, int)> testTuple({(String, int)? value, dynamic hint});

FlutterRustBridgeTaskConstMeta get kTestTupleConstMeta;

Future<void> testTuple2({required List<(String field0, int field1)> value, dynamic hint});
Future<void> testTuple2({required List<(String, int)> value, dynamic hint});

FlutterRustBridgeTaskConstMeta get kTestTuple2ConstMeta;

Expand Down
6 changes: 3 additions & 3 deletions frb_example/pure_dart/dart/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,7 @@ class FlutterRustBridgeExampleSingleBlockTestImpl implements FlutterRustBridgeEx
argNames: ["se"],
);

Future<(String field0, int field1)> testTuple({(String field0, int field1)? value, dynamic hint}) {
Future<(String, int)> testTuple({(String, int)? value, dynamic hint}) {
var arg0 = _platform.api2wire_opt_box_autoadd___record__String_i32(value);
return _platform.executeNormal(FlutterRustBridgeTask(
callFfi: (port_) => _platform.inner.wire_test_tuple(port_, arg0),
Expand All @@ -2485,7 +2485,7 @@ class FlutterRustBridgeExampleSingleBlockTestImpl implements FlutterRustBridgeEx
argNames: ["value"],
);

Future<void> testTuple2({required List<(String field0, int field1)> value, dynamic hint}) {
Future<void> testTuple2({required List<(String, int)> value, dynamic hint}) {
var arg0 = _platform.api2wire_list___record__String_i32(value);
return _platform.executeNormal(FlutterRustBridgeTask(
callFfi: (port_) => _platform.inner.wire_test_tuple_2(port_, arg0),
Expand Down Expand Up @@ -2847,7 +2847,7 @@ class FlutterRustBridgeExampleSingleBlockTestImpl implements FlutterRustBridgeEx
return raw as Uint8List;
}

(String field0, int field1) _wire2api___record__String_i32(dynamic raw) {
(String, int) _wire2api___record__String_i32(dynamic raw) {
final arr = raw as List<dynamic>;
if (arr.length != 2) {
throw Exception('Expected 2 elements, got ${arr.length}');
Expand Down
18 changes: 8 additions & 10 deletions frb_example/pure_dart/dart/lib/bridge_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
ffi.Pointer<wire___record__String_i32> api2wire_box_autoadd___record__String_i32((String field0, int field1) raw) {
ffi.Pointer<wire___record__String_i32> api2wire_box_autoadd___record__String_i32((String, int) raw) {
final ptr = inner.new_box_autoadd___record__String_i32_0();
_api_fill_to_wire___record__String_i32(raw, ptr.ref);
return ptr;
Expand Down Expand Up @@ -599,8 +599,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
ffi.Pointer<wire_list___record__String_i32> api2wire_list___record__String_i32(
List<(String field0, int field1)> raw) {
ffi.Pointer<wire_list___record__String_i32> api2wire_list___record__String_i32(List<(String, int)> raw) {
final ans = inner.new_list___record__String_i32_0(raw.length);
for (var i = 0; i < raw.length; ++i) {
_api_fill_to_wire___record__String_i32(raw[i], ans.ref.ptr[i]);
Expand Down Expand Up @@ -697,8 +696,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
ffi.Pointer<wire___record__String_i32> api2wire_opt_box_autoadd___record__String_i32(
(String field0, int field1)? raw) {
ffi.Pointer<wire___record__String_i32> api2wire_opt_box_autoadd___record__String_i32((String, int)? raw) {
return raw == null ? ffi.nullptr : api2wire_box_autoadd___record__String_i32(raw);
}

Expand Down Expand Up @@ -901,9 +899,9 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
wireObj.ptr = apiObj.shareOrMove();
}

void _api_fill_to_wire___record__String_i32((String field0, int field1) apiObj, wire___record__String_i32 wireObj) {
wireObj.field0 = api2wire_String(apiObj.field0);
wireObj.field1 = api2wire_i32(apiObj.field1);
void _api_fill_to_wire___record__String_i32((String, int) apiObj, wire___record__String_i32 wireObj) {
wireObj.field0 = api2wire_String(apiObj.$1);
wireObj.field1 = api2wire_i32(apiObj.$2);
}

void _api_fill_to_wire_a(A apiObj, wire_A wireObj) {
Expand Down Expand Up @@ -984,7 +982,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

void _api_fill_to_wire_box_autoadd___record__String_i32(
(String field0, int field1) apiObj, ffi.Pointer<wire___record__String_i32> wireObj) {
(String, int) apiObj, ffi.Pointer<wire___record__String_i32> wireObj) {
_api_fill_to_wire___record__String_i32(apiObj, wireObj.ref);
}

Expand Down Expand Up @@ -1398,7 +1396,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

void _api_fill_to_wire_opt_box_autoadd___record__String_i32(
(String field0, int field1)? apiObj, ffi.Pointer<wire___record__String_i32> wireObj) {
(String, int)? apiObj, ffi.Pointer<wire___record__String_i32> wireObj) {
if (apiObj != null) _api_fill_to_wire_box_autoadd___record__String_i32(apiObj, wireObj);
}

Expand Down
10 changes: 5 additions & 5 deletions frb_example/pure_dart/dart/lib/bridge_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
List<dynamic> api2wire___record__String_i32((String field0, int field1) raw) {
return [api2wire_String(raw.field0), api2wire_i32(raw.field1)];
List<dynamic> api2wire___record__String_i32((String, int) raw) {
return [api2wire_String(raw.$1), api2wire_i32(raw.$2)];
}

@protected
Expand Down Expand Up @@ -220,7 +220,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
List<dynamic> api2wire_box_autoadd___record__String_i32((String field0, int field1) raw) {
List<dynamic> api2wire_box_autoadd___record__String_i32((String, int) raw) {
return api2wire___record__String_i32(raw);
}

Expand Down Expand Up @@ -674,7 +674,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
List<dynamic> api2wire_list___record__String_i32(List<(String field0, int field1)> raw) {
List<dynamic> api2wire_list___record__String_i32(List<(String, int)> raw) {
return raw.map(api2wire___record__String_i32).toList();
}

Expand Down Expand Up @@ -801,7 +801,7 @@ class FlutterRustBridgeExampleSingleBlockTestPlatform
}

@protected
List<dynamic>? api2wire_opt_box_autoadd___record__String_i32((String field0, int field1)? raw) {
List<dynamic>? api2wire_opt_box_autoadd___record__String_i32((String, int)? raw) {
return raw == null ? null : api2wire_box_autoadd___record__String_i32(raw);
}

Expand Down

0 comments on commit 46e75be

Please sign in to comment.