Skip to content

Commit

Permalink
refactor: extract
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Dec 1, 2023
1 parent 6674740 commit 9500223
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 269 deletions.
7 changes: 2 additions & 5 deletions frb_example/pure_dart/frb_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -2966,8 +2966,6 @@ void wire_simple_adder_twin_rust_async(int64_t port_, int32_t a, int32_t b);

WireSyncReturn wire_simple_adder_twin_sync(int32_t a, int32_t b);

void wire_func_stream_realistic_twin_rust_async(int64_t port_, struct wire_list_prim_u_8 *arg);

void wire_func_stream_return_error_twin_rust_async(int64_t port_);

void wire_func_stream_return_panic_twin_rust_async(int64_t port_);
Expand Down Expand Up @@ -3091,8 +3089,6 @@ WireSyncReturn wire_sync_run_opaque_twin_normal(struct wire_RustOpaque_non_send_

void wire_simple_adder_twin_normal(int64_t port_, int32_t a, int32_t b);

void wire_func_stream_realistic_twin_normal(int64_t port_, struct wire_list_prim_u_8 *arg);

void wire_func_stream_return_error_twin_normal(int64_t port_);

void wire_func_stream_return_panic_twin_normal(int64_t port_);
Expand All @@ -3107,6 +3103,8 @@ void wire_handle_stream_sink_at_2_twin_normal(int64_t port_, uint32_t key, uint3

void wire_handle_stream_sink_at_3_twin_normal(int64_t port_, uint32_t key, uint32_t max);

void wire_func_stream_realistic_twin_normal(int64_t port_, struct wire_list_prim_u_8 *arg);

void wire_func_struct_with_one_field_twin_normal(int64_t port_,
struct wire_struct_with_one_field_twin_normal *arg);

Expand Down Expand Up @@ -4442,7 +4440,6 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) wire_func_return_unit_twin_rust_async);
dummy_var ^= ((int64_t) (void*) wire_func_return_unit_twin_sync);
dummy_var ^= ((int64_t) (void*) wire_func_stream_realistic_twin_normal);
dummy_var ^= ((int64_t) (void*) wire_func_stream_realistic_twin_rust_async);
dummy_var ^= ((int64_t) (void*) wire_func_stream_return_error_twin_normal);
dummy_var ^= ((int64_t) (void*) wire_func_stream_return_error_twin_rust_async);
dummy_var ^= ((int64_t) (void*) wire_func_stream_return_panic_twin_normal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import '../../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Stream<String> funcStreamRealisticTwinRustAsync(
{required String arg, dynamic hint}) =>
RustLib.instance.api.funcStreamRealisticTwinRustAsync(arg: arg, hint: hint);

Stream<String> funcStreamReturnErrorTwinRustAsync({dynamic hint}) =>
RustLib.instance.api.funcStreamReturnErrorTwinRustAsync(hint: hint);

Expand Down
4 changes: 0 additions & 4 deletions frb_example/pure_dart/lib/src/rust/api/stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint}) =>
RustLib.instance.api.funcStreamRealisticTwinNormal(arg: arg, hint: hint);

Stream<String> funcStreamReturnErrorTwinNormal({dynamic hint}) =>
RustLib.instance.api.funcStreamReturnErrorTwinNormal(hint: hint);

Expand Down
8 changes: 8 additions & 0 deletions frb_example/pure_dart/lib/src/rust/api/stream_misc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// ignore_for_file: invalid_use_of_internal_member, unused_import

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

Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint}) =>
RustLib.instance.api.funcStreamRealisticTwinNormal(arg: arg, hint: hint);
76 changes: 26 additions & 50 deletions frb_example/pure_dart/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import 'api/rust_opaque.dart';
import 'api/rust_opaque_sync.dart';
import 'api/simple.dart';
import 'api/stream.dart';
import 'api/stream_misc.dart';
import 'api/structure.dart';
import 'api/tuple.dart';
import 'api/type_alias.dart';
Expand Down Expand Up @@ -1782,9 +1783,6 @@ abstract class RustLibApi extends BaseApi {

int simpleAdderTwinSync({required int a, required int b, dynamic hint});

Stream<String> funcStreamRealisticTwinRustAsync(
{required String arg, dynamic hint});

Stream<String> funcStreamReturnErrorTwinRustAsync({dynamic hint});

Stream<String> funcStreamReturnPanicTwinRustAsync({dynamic hint});
Expand Down Expand Up @@ -1942,9 +1940,6 @@ abstract class RustLibApi extends BaseApi {
Future<int> simpleAdderTwinNormal(
{required int a, required int b, dynamic hint});

Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint});

Stream<String> funcStreamReturnErrorTwinNormal({dynamic hint});

Stream<String> funcStreamReturnPanicTwinNormal({dynamic hint});
Expand All @@ -1964,6 +1959,9 @@ abstract class RustLibApi extends BaseApi {
Stream<LogTwinNormal> handleStreamSinkAt3TwinNormal(
{required int key, required int max, dynamic hint});

Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint});

Future<StructWithOneFieldTwinNormal> funcStructWithOneFieldTwinNormal(
{required StructWithOneFieldTwinNormal arg, dynamic hint});

Expand Down Expand Up @@ -14616,28 +14614,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["a", "b"],
);

@override
Stream<String> funcStreamRealisticTwinRustAsync(
{required String arg, dynamic hint}) {
var arg0 = api2wire_String(arg);
return handler.executeStream(StreamTask(
callFfi: (port_) =>
wire.wire_func_stream_realistic_twin_rust_async(port_, arg0),
parseSuccessData: _wire2api_String,
parseErrorData: null,
constMeta: kFuncStreamRealisticTwinRustAsyncConstMeta,
argValues: [arg],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kFuncStreamRealisticTwinRustAsyncConstMeta =>
const TaskConstMeta(
debugName: "func_stream_realistic_twin_rust_async",
argNames: ["arg"],
);

@override
Stream<String> funcStreamReturnErrorTwinRustAsync({dynamic hint}) {
return handler.executeStream(StreamTask(
Expand Down Expand Up @@ -15826,28 +15802,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["a", "b"],
);

@override
Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint}) {
var arg0 = api2wire_String(arg);
return handler.executeStream(StreamTask(
callFfi: (port_) =>
wire.wire_func_stream_realistic_twin_normal(port_, arg0),
parseSuccessData: _wire2api_String,
parseErrorData: null,
constMeta: kFuncStreamRealisticTwinNormalConstMeta,
argValues: [arg],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kFuncStreamRealisticTwinNormalConstMeta =>
const TaskConstMeta(
debugName: "func_stream_realistic_twin_normal",
argNames: ["arg"],
);

@override
Stream<String> funcStreamReturnErrorTwinNormal({dynamic hint}) {
return handler.executeStream(StreamTask(
Expand Down Expand Up @@ -15998,6 +15952,28 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["key", "max"],
);

@override
Stream<String> funcStreamRealisticTwinNormal(
{required String arg, dynamic hint}) {
var arg0 = api2wire_String(arg);
return handler.executeStream(StreamTask(
callFfi: (port_) =>
wire.wire_func_stream_realistic_twin_normal(port_, arg0),
parseSuccessData: _wire2api_String,
parseErrorData: null,
constMeta: kFuncStreamRealisticTwinNormalConstMeta,
argValues: [arg],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kFuncStreamRealisticTwinNormalConstMeta =>
const TaskConstMeta(
debugName: "func_stream_realistic_twin_normal",
argNames: ["arg"],
);

@override
Future<StructWithOneFieldTwinNormal> funcStructWithOneFieldTwinNormal(
{required StructWithOneFieldTwinNormal arg, dynamic hint}) {
Expand Down
55 changes: 19 additions & 36 deletions frb_example/pure_dart/lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import 'api/rust_opaque.dart';
import 'api/rust_opaque_sync.dart';
import 'api/simple.dart';
import 'api/stream.dart';
import 'api/stream_misc.dart';
import 'api/structure.dart';
import 'api/tuple.dart';
import 'api/type_alias.dart';
Expand Down Expand Up @@ -15046,24 +15047,6 @@ class RustLibWire implements BaseWire {
late final _wire_simple_adder_twin_sync = _wire_simple_adder_twin_syncPtr
.asFunction<WireSyncReturn Function(int, int)>();

void wire_func_stream_realistic_twin_rust_async(
int port_,
ffi.Pointer<wire_list_prim_u_8> arg,
) {
return _wire_func_stream_realistic_twin_rust_async(
port_,
arg,
);
}

late final _wire_func_stream_realistic_twin_rust_asyncPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(ffi.Int64, ffi.Pointer<wire_list_prim_u_8>)>>(
'wire_func_stream_realistic_twin_rust_async');
late final _wire_func_stream_realistic_twin_rust_async =
_wire_func_stream_realistic_twin_rust_asyncPtr
.asFunction<void Function(int, ffi.Pointer<wire_list_prim_u_8>)>();

void wire_func_stream_return_error_twin_rust_async(
int port_,
) {
Expand Down Expand Up @@ -16040,24 +16023,6 @@ class RustLibWire implements BaseWire {
late final _wire_simple_adder_twin_normal = _wire_simple_adder_twin_normalPtr
.asFunction<void Function(int, int, int)>();

void wire_func_stream_realistic_twin_normal(
int port_,
ffi.Pointer<wire_list_prim_u_8> arg,
) {
return _wire_func_stream_realistic_twin_normal(
port_,
arg,
);
}

late final _wire_func_stream_realistic_twin_normalPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(ffi.Int64, ffi.Pointer<wire_list_prim_u_8>)>>(
'wire_func_stream_realistic_twin_normal');
late final _wire_func_stream_realistic_twin_normal =
_wire_func_stream_realistic_twin_normalPtr
.asFunction<void Function(int, ffi.Pointer<wire_list_prim_u_8>)>();

void wire_func_stream_return_error_twin_normal(
int port_,
) {
Expand Down Expand Up @@ -16183,6 +16148,24 @@ class RustLibWire implements BaseWire {
_wire_handle_stream_sink_at_3_twin_normalPtr
.asFunction<void Function(int, int, int)>();

void wire_func_stream_realistic_twin_normal(
int port_,
ffi.Pointer<wire_list_prim_u_8> arg,
) {
return _wire_func_stream_realistic_twin_normal(
port_,
arg,
);
}

late final _wire_func_stream_realistic_twin_normalPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(ffi.Int64, ffi.Pointer<wire_list_prim_u_8>)>>(
'wire_func_stream_realistic_twin_normal');
late final _wire_func_stream_realistic_twin_normal =
_wire_func_stream_realistic_twin_normalPtr
.asFunction<void Function(int, ffi.Pointer<wire_list_prim_u_8>)>();

void wire_func_struct_with_one_field_twin_normal(
int port_,
ffi.Pointer<wire_struct_with_one_field_twin_normal> arg,
Expand Down
22 changes: 8 additions & 14 deletions frb_example/pure_dart/lib/src/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import 'api/rust_opaque.dart';
import 'api/rust_opaque_sync.dart';
import 'api/simple.dart';
import 'api/stream.dart';
import 'api/stream_misc.dart';
import 'api/structure.dart';
import 'api/tuple.dart';
import 'api/type_alias.dart';
Expand Down Expand Up @@ -5567,10 +5568,6 @@ class RustLibWire extends BaseWire {
wire_simple_adder_twin_sync(int a, int b) =>
wasmModule.wire_simple_adder_twin_sync(a, b);

void wire_func_stream_realistic_twin_rust_async(
NativePortType port_, String arg) =>
wasmModule.wire_func_stream_realistic_twin_rust_async(port_, arg);

void wire_func_stream_return_error_twin_rust_async(NativePortType port_) =>
wasmModule.wire_func_stream_return_error_twin_rust_async(port_);

Expand Down Expand Up @@ -5791,10 +5788,6 @@ class RustLibWire extends BaseWire {
void wire_simple_adder_twin_normal(NativePortType port_, int a, int b) =>
wasmModule.wire_simple_adder_twin_normal(port_, a, b);

void wire_func_stream_realistic_twin_normal(
NativePortType port_, String arg) =>
wasmModule.wire_func_stream_realistic_twin_normal(port_, arg);

void wire_func_stream_return_error_twin_normal(NativePortType port_) =>
wasmModule.wire_func_stream_return_error_twin_normal(port_);

Expand All @@ -5820,6 +5813,10 @@ class RustLibWire extends BaseWire {
NativePortType port_, int key, int max) =>
wasmModule.wire_handle_stream_sink_at_3_twin_normal(port_, key, max);

void wire_func_stream_realistic_twin_normal(
NativePortType port_, String arg) =>
wasmModule.wire_func_stream_realistic_twin_normal(port_, arg);

void wire_func_struct_with_one_field_twin_normal(
NativePortType port_, List<dynamic> arg) =>
wasmModule.wire_func_struct_with_one_field_twin_normal(port_, arg);
Expand Down Expand Up @@ -7730,9 +7727,6 @@ class RustLibWasmModule implements WasmModule {
external dynamic /* flutter_rust_bridge::support::WireSyncReturn */
wire_simple_adder_twin_sync(int a, int b);

external void wire_func_stream_realistic_twin_rust_async(
NativePortType port_, String arg);

external void wire_func_stream_return_error_twin_rust_async(
NativePortType port_);

Expand Down Expand Up @@ -7900,9 +7894,6 @@ class RustLibWasmModule implements WasmModule {
external void wire_simple_adder_twin_normal(
NativePortType port_, int a, int b);

external void wire_func_stream_realistic_twin_normal(
NativePortType port_, String arg);

external void wire_func_stream_return_error_twin_normal(NativePortType port_);

external void wire_func_stream_return_panic_twin_normal(NativePortType port_);
Expand All @@ -7921,6 +7912,9 @@ class RustLibWasmModule implements WasmModule {
external void wire_handle_stream_sink_at_3_twin_normal(
NativePortType port_, int key, int max);

external void wire_func_stream_realistic_twin_normal(
NativePortType port_, String arg);

external void wire_func_struct_with_one_field_twin_normal(
NativePortType port_, List<dynamic> arg);

Expand Down
1 change: 1 addition & 0 deletions frb_example/pure_dart/rust/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod rust_opaque;
pub mod rust_opaque_sync;
pub mod simple;
pub mod stream;
pub mod stream_misc;
pub mod structure;
pub mod tuple;
pub mod type_alias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,6 @@

use anyhow::anyhow;
use flutter_rust_bridge::{spawn, StreamSink};
use log::info;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;

pub async fn func_stream_realistic_twin_rust_async(sink: StreamSink<String>, arg: String) {
info!("handle_stream_realistic arg={}", arg);

let cnt = Arc::new(AtomicI32::new(0));

// just to show that, you can send data to sink even in other threads
let cnt2 = cnt.clone();
let sink2 = sink.clone();
spawn!(|| {
for i in 0..5 {
let old_cnt = cnt2.fetch_add(1, Ordering::SeqCst);
let msg = format!("(thread=child, i={i}, old_cnt={old_cnt})");
format!("send data to sink msg={msg}");
let _ = sink2.add(msg);
sleep(Duration::from_millis(100));
}

if cnt2.load(Ordering::SeqCst) == 10 {
sink2.close();
}
});

for i in 0..5 {
let old_cnt = cnt.fetch_add(1, Ordering::SeqCst);
let msg = format!("(thread=normal, i={i}, old_cnt={old_cnt})");
format!("send data to sink msg={msg}");
let _ = sink.add(msg);
sleep(Duration::from_millis(50));
}

if cnt.load(Ordering::SeqCst) == 10 {
sink.close();
}
}

pub async fn func_stream_return_error_twin_rust_async(
_sink: StreamSink<String>,
Expand Down
Loading

0 comments on commit 9500223

Please sign in to comment.