Skip to content

Commit

Permalink
Merge pull request #2294 from fzyzcjy/feat/12568
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Sep 12, 2024
2 parents 5608d3d + 2e0afc0 commit 7a1092c
Show file tree
Hide file tree
Showing 412 changed files with 4,310 additions and 2,454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,45 @@ impl<'a> WireRustCodecDcoGeneratorEncoderTrait for DelegateWireRustCodecDcoGener
// frb-coverage:ignore-start
fn generate_impl_into_dart(&self) -> Option<String> {
// frb-coverage:ignore-end
if let MirTypeDelegate::PrimitiveEnum(MirTypeDelegatePrimitiveEnum { mir, .. }) = &self.mir
{
let src = mir.get(self.context.mir_pack);
let (name, self_path) =
parse_wrapper_name_into_dart_name_and_self_path(&src.name, &src.wrapper_name);
match &self.mir {
MirTypeDelegate::PrimitiveEnum(MirTypeDelegatePrimitiveEnum { mir, .. }) => {
let src = mir.get(self.context.mir_pack);
let (name, self_path) =
parse_wrapper_name_into_dart_name_and_self_path(&src.name, &src.wrapper_name);

let self_ref = generate_enum_access_object_core(mir, "self".to_owned(), self.context);
let variants = src
.variants()
.iter()
.enumerate()
.map(|(idx, variant)| {
format!("{}::{} => {}.into_dart(),", self_path, variant.name, idx)
})
.collect_vec()
.join("\n");
let self_ref =
generate_enum_access_object_core(mir, "self".to_owned(), self.context);
let variants = src
.variants()
.iter()
.enumerate()
.map(|(idx, variant)| {
format!("{}::{} => {}.into_dart(),", self_path, variant.name, idx)
})
.collect_vec()
.join("\n");

let body = format!(
"match {self_ref} {{
let body = format!(
"match {self_ref} {{
{variants}
_ => unreachable!(),
}}"
);
);

return Some(
generate_impl_into_dart(&name, &body)
+ &generate_impl_into_into_dart(&src.name.rust_style(), &src.wrapper_name),
);
Some(
generate_impl_into_dart(&name, &body)
+ &generate_impl_into_into_dart(&src.name.rust_style(), &src.wrapper_name),
)
}
MirTypeDelegate::CustomSerDes(inner) => {
let name = inner.info.cleared_rust_api_type();
let wrapper_name = format!("FrbWrapper<{name}>");
Some(
generate_impl_into_dart(&wrapper_name, "unimplemented!()")
+ &generate_impl_into_into_dart(&name, &Some(wrapper_name.clone())),
)
}
_ => None,
}
None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ impl<'a> WireRustGeneratorMiscTrait for DelegateWireRustGenerator<'a> {
// frb-coverage:ignore-start
fn wrapper_struct_name(&self) -> Option<String> {
// frb-coverage:ignore-end
if let MirTypeDelegate::PrimitiveEnum(enu) = &self.mir {
WireRustGenerator::new(enu.mir.clone(), self.context).wrapper_struct_name()
} else {
None
match &self.mir {
MirTypeDelegate::PrimitiveEnum(enu) => {
WireRustGenerator::new(enu.mir.clone(), self.context).wrapper_struct_name()
}
MirTypeDelegate::CustomSerDes(inner) => Some(format!(
"FrbWrapper<{}>",
inner.info.cleared_rust_api_type()
)),
_ => None,
}
}

Expand Down
11 changes: 11 additions & 0 deletions frb_codegen/src/library/codegen/ir/mir/custom_ser_des.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::codegen::ir::mir::ty::MirType;
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
use crate::utils::namespace::NamespacedName;

crate::mir! {
Expand All @@ -15,3 +16,13 @@ pub struct MirCustomSerDesHalf {
pub rust_function: NamespacedName,
}
}

impl MirCustomSerDes {
pub(crate) fn cleared_rust_api_type(&self) -> String {
if let MirType::RustAutoOpaqueImplicit(ty) = &*self.rust_api_type {
ty.raw.string.with_original_lifetime().to_owned()
} else {
self.rust_api_type.rust_api_type()
}
}
}
29 changes: 15 additions & 14 deletions frb_codegen/src/library/codegen/parser/mir/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ pub(crate) fn parse(

let mut type_parser = TypeParser::new_from_pack(ir_pack);

let trait_impls = trait_impl::parse(
&hir_flat.trait_impls,
&mut type_parser,
config
.rust_input_namespace_pack
.rust_output_path_namespace
.clone(),
config.default_stream_sink_codec,
config.default_rust_opaque_codec,
config.enable_lifetime,
config.type_64bit_int,
parse_mode,
)?;

// NOTE: Should parse this as early as possible
let custom_ser_des_infos = custom_ser_des::parse(
&hir_flat.functions,
&mut type_parser,
Expand All @@ -65,6 +52,20 @@ pub(crate) fn parse(
.custom_ser_des_infos
.extend(custom_ser_des_infos);

let trait_impls = trait_impl::parse(
&hir_flat.trait_impls,
&mut type_parser,
config
.rust_input_namespace_pack
.rust_output_path_namespace
.clone(),
config.default_stream_sink_codec,
config.default_rust_opaque_codec,
config.enable_lifetime,
config.type_64bit_int,
parse_mode,
)?;

let (funcs_all, funcs_skip) = function::parse(
config,
&hir_flat.functions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::codegen::ir::mir::custom_ser_des::MirCustomSerDes;
use crate::codegen::ir::mir::ty::delegate::{MirTypeDelegate, MirTypeDelegateCustomSerDes};
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::parser::ty::unencodable::SplayedSegment;
Expand All @@ -11,11 +12,18 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
) -> anyhow::Result<Option<MirType>> {
// use HashMap etc later if too slow; here we use filter to remain flexibility of filtering strategy
Ok((self.inner.custom_ser_des_infos.iter())
.find(|info| info.rust_api_type.rust_api_type() == last_segment.0)
.find(|info| compute_matcher_types(info).contains(&last_segment.0.to_owned()))
.map(|info| {
MirType::Delegate(MirTypeDelegate::CustomSerDes(MirTypeDelegateCustomSerDes {
info: info.to_owned(),
}))
}))
}
}

fn compute_matcher_types(info: &MirCustomSerDes) -> Vec<String> {
vec![
info.rust_api_type.rust_api_type(),
info.cleared_rust_api_type(),
]
}
1 change: 1 addition & 0 deletions frb_example/pure_dart/flutter_rust_bridge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dart_type_rename:
Box<dyn Any + Send + Sync + 'static>: BoxAnyMyDartTypeRename

rust_preamble: "// test for rust_preamble"
dart_preamble: "import 'dart:io';"

# To help developing flutter_rust_bridge; normal users usually do not set this
stop_on_error: true
Expand Down
12 changes: 12 additions & 0 deletions frb_example/pure_dart/frb_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -3590,6 +3590,16 @@ void frbgen_frb_example_pure_dart_wire__crate__api__constructor__constructor_tra

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__constructor__constructor_translatable_sync_struct_twin_normal_new(void);

void frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__func_using_ipv4_addr(int64_t port_,
uint8_t *ptr_,
int32_t rust_vec_len_,
int32_t data_len_);

void frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__func_using_non_opaque_struct_containing_ipv4_addr(int64_t port_,
uint8_t *ptr_,
int32_t rust_vec_len_,
int32_t data_len_);

void frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__function_using_type_with_custom_serializer(int64_t port_,
uint8_t *ptr_,
int32_t rust_vec_len_,
Expand Down Expand Up @@ -16161,6 +16171,8 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__constructor__ConstructorOpaqueSyncStructTwinNormal_new);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__constructor__constructor_translatable_struct_twin_normal_new);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__constructor__constructor_translatable_sync_struct_twin_normal_new);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__func_using_ipv4_addr);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__func_using_non_opaque_struct_containing_ipv4_addr);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__custom_ser_des__function_using_type_with_custom_serializer);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__customization__check_init_done);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__customization__init_app);
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import 'frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/array.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/async_misc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/async_spawn.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/attribute.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/benchmark_misc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/casted_primitive.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/chrono_type.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/comment.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/constructor.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
31 changes: 31 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/custom_ser_des.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// These types are ignored because they are not used by any `pub` functions: `MessageWithCustomSerializerTwinNormal`
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `fmt`

Future<int> functionUsingTypeWithCustomSerializer({required int arg}) =>
RustLib.instance.api
.crateApiCustomSerDesFunctionUsingTypeWithCustomSerializer(arg: arg);

Future<InternetAddress> funcUsingIpv4Addr({required InternetAddress arg}) =>
RustLib.instance.api.crateApiCustomSerDesFuncUsingIpv4Addr(arg: arg);

Future<NonOpaqueStructContainingIpv4Addr>
funcUsingNonOpaqueStructContainingIpv4Addr(
{required NonOpaqueStructContainingIpv4Addr arg}) =>
RustLib.instance.api
.crateApiCustomSerDesFuncUsingNonOpaqueStructContainingIpv4Addr(
arg: arg);

class NonOpaqueStructContainingIpv4Addr {
final InternetAddress inner;

const NonOpaqueStructContainingIpv4Addr({
required this.inner,
});

@override
int get hashCode => inner.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is NonOpaqueStructContainingIpv4Addr &&
runtimeType == other.runtimeType &&
inner == other.inner;
}
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/customization.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/dart_code.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/dart_dynamic.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/dart_fn.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/dart_opaque.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
2 changes: 2 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/dart_opaque_sync.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.3.0.

import 'dart:io';

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
Expand Down
Loading

0 comments on commit 7a1092c

Please sign in to comment.